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