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