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