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