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