aboutsummaryrefslogtreecommitdiff
path: root/posts/2021-08-01-linux-cheatsheet.md
blob: cd9ccedecb0e0c998c7bb11d3b4bfd64d3026dea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
---
Title: List of essential Linux commands for server management
Description: List of essential Linux commands for server management
Slug: linux-cheatsheet
Listing: true
Created: 2021-08-01
Tags: []
---

**Table of contents**

1. [Generate SSH key](#generate-ssh-key)
2. [Login to host via SSH](#login-to-host-via-ssh)
3. [Execute command on a server through SSH](#execute-command-on-a-server-through-ssh)
4. [Displays currently logged in users in the system](#displays-currently-logged-in-users-in-the-system)
5. [Displays Linux system information](#displays-linux-system-information)
6. [Displays kernel release information](#displays-kernel-release-information)
7. [Shows the system hostname](#shows-the-system-hostname)
8. [Shows system reboot history](#shows-system-reboot-history)
9. [Displays information about the user](#displays-information-about-the-user)
10. [Displays IP addresses and all the network interfaces](#displays-ip-addresses-and-all-the-network-interfaces)
11. [Downloads a file from an online source](#downloads-a-file-from-an-online-source)
12. [Compress a file with gzip](#compress-a-file-with-gzip)
13. [Interactive disk usage analyzer](#interactive-disk-usage-analyzer)
14. [Install Node.js using the Node Version Manager](#install-nodejs-using-the-node-version-manager)
15. [Too long; didn't read](#too-long-didnt-read)
16. [Combine all Nginx access logs to one big log file](#combine-all-nginx-access-logs-to-one-big-log-file)
17. [Set up Redis server](#set-up-redis-server)
18. [Generate statistics of your webserver](#generate-statistics-of-your-webserver)
19. [Search for a given pattern in files](#search-for-a-given-pattern-in-files)
20. [Find proccess ID for a specific program](#find-proccess-id-for-a-specific-program)
21. [Print name of current/working directory](#print-name-of-currentworking-directory)
22. [Creates a blank new file](#creates-a-blank-new-file)
23. [Displays first lines in a file](#displays-first-lines-in-a-file)
24. [Displays last lines in a file](#displays-last-lines-in-a-file)
25. [Count lines in a file](#count-lines-in-a-file)
26. [Find all instances of the file](#find-all-instances-of-the-file)
27. [Find file names that begin with ‘index’ in /home folder](#find-file-names-that-begin-with-index-in-home-folder)
28. [Find files larger than 100MB in the home folder](#find-files-larger-than-100mb-in-the-home-folder)
29. [Displays block devices related information](#displays-block-devices-related-information)
30. [Displays free space on mounted systems](#displays-free-space-on-mounted-systems)
31. [Displays free and used memory in the system](#displays-free-and-used-memory-in-the-system)
32. [Displays all active listening ports](#displays-all-active-listening-ports)
33. [Kill a process violently](#kill-a-process-violently)
34. [List files opened by user](#list-files-opened-by-user)
35. [Execute "df -h", showing periodic updates](#execute-df--h-showing-periodic-updates)


##### Generate SSH key

```bash
ssh-keygen -t ed25519 -C "your_email@example.com"

# when no support for Ed25519 present
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
```

Note: By default SSH keys get stored to `/home/<username>/.ssh/` folder.



##### Login to host via SSH

```bash
# connect to host as your local username
ssh host

# connect to host as user
ssh <user>@<host>

# connect to host using port
ssh -p <port> <user>@<host>
```



##### Execute command on a server through SSH

```bash
# execute one command
ssh root@100.100.100.100 "ls /root"

# execute many commands
ssh root@100.100.100.100 "cd /root;touch file.txt"
```



##### Displays currently logged in users in the system

```bash
w
```



##### Displays Linux system information

```bash
uname
```



##### Displays kernel release information

```bash
uname -r
```



##### Shows the system hostname

```bash
hostname
```



##### Shows system reboot history

```bash
last reboot
```



##### Displays information about the user

```bash
sudo apt install finger
finger <username>
```



##### Displays IP addresses and all the network interfaces

```bash
ip addr show
```



##### Downloads a file from an online source

```bash
wget https://example.com/example.tgz
```

Note: If URL contains ?, & enclose the URL in double quotes.



##### Compress a file with gzip

```bash
# will not keep the original file
gzip file.txt

# will keep the original file
gzip --keep file.txt
```



##### Interactive disk usage analyzer

```bash
sudo apt install ncdu

ncdu
ncdu <path/to/directory>
```



##### Install Node.js using the Node Version Manager

```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
source ~/.bashrc

nvm install v13
```



##### Too long; didn't read

```bash
npm install -g tldr

tldr tar
```



##### Combine all Nginx access logs to one big log file

```bash
zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
```



##### Set up Redis server

```bash
sudo apt install redis-server redis-tools

# check if server is running
sudo service redis status

# set and get a key value
redis-cli set mykey myvalue
redis-cli get mykey

# interactive shell
redis-cli
```



##### Generate statistics of your webserver

```bash
sudo apt install goaccess

# check if installed
goaccess -v

# combine logs
zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log

# export to single html
goaccess \
  --log-file=/var/log/nginx/access-all.log \
  --log-format=COMBINED \
  --exclude-ip=0.0.0.0 \
  --ignore-crawlers \
  --real-os \
  --output=/var/www/html/stats.html

# cleanup afterwards
rm /var/log/nginx/access-all.log
```



##### Search for a given pattern in files

```bash
grep -r ‘pattern’ files
```



##### Find proccess ID for a specific program

```bash
pgrep nginx
```



##### Print name of current/working directory

```bash
pwd
```



##### Creates a blank new file

```bash
touch newfile.txt
```



##### Displays first lines in a file

```bash
# -n <x> presents the number of lines (10 by default)
head -n 20 somefile.txt
```



##### Displays last lines in a file

```bash
# -n <x> presents the number of lines (10 by default)
tail -n 20 somefile.txt

# -f follows the changes in file (doesn't closes)
tail -f somefile.txt
```



##### Count lines in a file

```bash
wc -l somefile.txt
```



##### Find all instances of the file

```bash
sudo apt install mlocate

locate somefile.txt
```



##### Find file names that begin with ‘index’ in /home folder

```bash
find /home/ -name "index"
```



##### Find files larger than 100MB in the home folder

```bash
find /home -size +100M
```



##### Displays block devices related information

```bash
lsblk
```



##### Displays free space on mounted systems

```bash
df -h
```



##### Displays free and used memory in the system

```bash
free -h
```



##### Displays all active listening ports

```bash
sudo apt install net-tools

netstat -pnltu
```



##### Kill a process violently

```bash
kill -9 <pid>
```



##### List files opened by user

```bash
lsof -u <user>
```



##### Execute "df -h", showing periodic updates

```bash
# -n 1 means every second
watch -n 1 df -h
```