aboutsummaryrefslogtreecommitdiff
path: root/posts/2021-08-01-linux-cheatsheet.md
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2021-08-01 20:25:28 +0200
committerMitja Felicijan <mitja.felicijan@gmail.com>2021-08-01 20:25:28 +0200
commit52bfdaea11ec4ff5a8bc9b97fb86a2777b8b4511 (patch)
tree2633d957bbe65d25f172865dd46a01c099614a11 /posts/2021-08-01-linux-cheatsheet.md
parent370fac0530b38ead76817b0b247a450a8c102f40 (diff)
downloadmitjafelicijan.com-52bfdaea11ec4ff5a8bc9b97fb86a2777b8b4511.tar.gz
new post
Diffstat (limited to 'posts/2021-08-01-linux-cheatsheet.md')
-rw-r--r--posts/2021-08-01-linux-cheatsheet.md364
1 files changed, 364 insertions, 0 deletions
diff --git a/posts/2021-08-01-linux-cheatsheet.md b/posts/2021-08-01-linux-cheatsheet.md
new file mode 100644
index 0000000..6e28016
--- /dev/null
+++ b/posts/2021-08-01-linux-cheatsheet.md
@@ -0,0 +1,364 @@
1---
2Title: List of essential Linux commands for server management
3Description: List of essential Linux commands for server management
4Slug: linux-cheatsheet
5Listing: true
6Created: 2021-08-01
7Tags: []
8---
9
10***For Debian and Ubuntu servers***
11
12
13
14
15
16##### Generate SSH key
17
18```bash
19ssh-keygen -t ed25519 -C "your_email@example.com"
20
21# when no support for Ed25519 present
22ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
23```
24
25Note: By default SSH keys get stored to `/home/<username>/.ssh/` folder.
26
27
28
29##### Login to host via SSH
30
31```bash
32# connect to host as your local username
33ssh host
34
35# connect to host as user
36ssh <user>@<host>
37
38# connect to host using port
39ssh -p <port> <user>@<host>
40```
41
42
43
44##### Execute command on a server through SSH
45
46```bash
47# execute one command
48ssh root@100.100.100.100 "ls /root"
49
50# execute many commands
51ssh root@100.100.100.100 "cd /root;touch file.txt"
52```
53
54
55
56##### Displays currently logged in users in the system
57
58```bash
59w
60```
61
62
63
64##### Displays Linux system information
65
66```bash
67uname
68```
69
70
71
72##### Displays kernel release information
73
74```bash
75uname -r
76```
77
78
79
80##### Shows the system hostname
81
82```bash
83hostname
84```
85
86
87
88##### Shows system reboot history
89
90```bash
91last reboot
92```
93
94
95
96##### Displays information about the user
97
98```bash
99sudo apt install finger
100finger <username>
101```
102
103
104
105##### Displays IP addresses and all the network interfaces
106
107```bash
108ip addr show
109```
110
111
112
113##### Downloads a file from an online source
114
115```bash
116wget https://example.com/example.tgz
117```
118
119Note: If URL contains ?, & enclose the URL in double quotes.
120
121
122
123##### Compress a file with gzip
124
125```bash
126# will not keep the original file
127gzip file.txt
128
129# will keep the original file
130gzip --keep file.txt
131```
132
133
134
135##### Interactive disk usage analyzer
136
137```bash
138sudo apt install ncdu
139
140ncdu
141ncdu <path/to/directory>
142```
143
144
145
146##### Install Node.js using the Node Version Manager
147
148```bash
149curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
150source ~/.bashrc
151
152nvm install v13
153```
154
155
156
157##### Too long; didn't read
158
159```bash
160npm install -g tldr
161
162tldr tar
163```
164
165
166
167##### Combine all Nginx access logs to one big log file
168
169```bash
170zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
171```
172
173
174
175##### Set up Redis server
176
177```bash
178sudo apt install redis-server redis-tools
179
180# check if server is running
181sudo service redis status
182
183# set and get a key value
184redis-cli set mykey myvalue
185redis-cli get mykey
186
187# interactive shell
188redis-cli
189```
190
191
192
193##### Generate statistics of your webserver
194
195```bash
196sudo apt install goaccess
197
198# check if installed
199goaccess -v
200
201# combine logs
202zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log
203
204# export to single html
205goaccess \
206 --log-file=/var/log/nginx/access-all.log \
207 --log-format=COMBINED \
208 --exclude-ip=0.0.0.0 \
209 --ignore-crawlers \
210 --real-os \
211 --output=/var/www/html/stats.html
212
213# cleanup afterwards
214rm /var/log/nginx/access-all.log
215```
216
217
218
219##### Search for a given pattern in files
220
221```bash
222grep -r ‘pattern’ files
223```
224
225
226
227##### Find proccess ID for a specific program
228
229```bash
230pgrep nginx
231```
232
233
234
235##### Print name of current/working directory
236
237```bash
238pwd
239```
240
241
242
243##### Creates a blank new file
244
245```bash
246touch newfile.txt
247```
248
249
250
251##### Displays first lines in a file
252
253```bash
254# -n <x> presents the number of lines (10 by default)
255head -n 20 somefile.txt
256```
257
258
259
260##### Displays last lines in a file
261
262```bash
263# -n <x> presents the number of lines (10 by default)
264tail -n 20 somefile.txt
265
266# -f follows the changes in file (doesn't closes)
267tail -f somefile.txt
268```
269
270
271
272##### Count lines in a file
273
274```bash
275wc -l somefile.txt
276```
277
278
279
280##### Find all instances of the file
281
282```bash
283sudo apt install mlocate
284
285locate somefile.txt
286```
287
288
289
290##### Find file names that begin with ‘index’ in /home folder
291
292```bash
293find /home/ -name "index"
294```
295
296
297
298##### Find files larger than 100MB in the home folder
299
300```bash
301find /home -size +10000M
302```
303
304
305
306##### Displays block devices related information
307
308```bash
309lsblk
310```
311
312
313
314##### Displays free space on mounted systems
315
316```bash
317df -h
318```
319
320
321
322##### Displays free and used memory in the system
323
324```bash
325free -h
326```
327
328
329
330##### Displays all active listening ports
331
332```bash
333sudo apt install net-tools
334
335netstat -pnltu
336```
337
338
339
340##### Kill a process violently
341
342```bash
343kill -9 <pid>
344```
345
346
347
348##### List files opened by user
349
350```bash
351lsof -u <user>
352```
353
354
355
356##### Execute "df -h", showing periodic updates
357
358```bash
359# -n 1 means every second
360watch -n 1 df -h
361```
362
363
364