diff options
Diffstat (limited to 'content/posts/2021-08-01-linux-cheatsheet.md')
| -rw-r--r-- | content/posts/2021-08-01-linux-cheatsheet.md | 141 |
1 files changed, 35 insertions, 106 deletions
diff --git a/content/posts/2021-08-01-linux-cheatsheet.md b/content/posts/2021-08-01-linux-cheatsheet.md index 1645eaf..8c0d125 100644 --- a/content/posts/2021-08-01-linux-cheatsheet.md +++ b/content/posts/2021-08-01-linux-cheatsheet.md | |||
| @@ -5,7 +5,7 @@ date: 2021-08-01 | |||
| 5 | draft: false | 5 | draft: false |
| 6 | --- | 6 | --- |
| 7 | 7 | ||
| 8 | ##### Generate SSH key | 8 | **Generate SSH key** |
| 9 | 9 | ||
| 10 | ```bash | 10 | ```bash |
| 11 | ssh-keygen -t ed25519 -C "your_email@example.com" | 11 | ssh-keygen -t ed25519 -C "your_email@example.com" |
| @@ -16,9 +16,7 @@ ssh-keygen -t rsa -b 4096 -C "your_email@example.com" | |||
| 16 | 16 | ||
| 17 | Note: By default SSH keys get stored to `/home/<username>/.ssh/` folder. | 17 | Note: By default SSH keys get stored to `/home/<username>/.ssh/` folder. |
| 18 | 18 | ||
| 19 | 19 | **Login to host via SSH** | |
| 20 | |||
| 21 | ##### Login to host via SSH | ||
| 22 | 20 | ||
| 23 | ```bash | 21 | ```bash |
| 24 | # connect to host as your local username | 22 | # connect to host as your local username |
| @@ -31,9 +29,7 @@ ssh <user>@<host> | |||
| 31 | ssh -p <port> <user>@<host> | 29 | ssh -p <port> <user>@<host> |
| 32 | ``` | 30 | ``` |
| 33 | 31 | ||
| 34 | 32 | **Execute command on a server through SSH** | |
| 35 | |||
| 36 | ##### Execute command on a server through SSH | ||
| 37 | 33 | ||
| 38 | ```bash | 34 | ```bash |
| 39 | # execute one command | 35 | # execute one command |
| @@ -43,66 +39,50 @@ ssh root@100.100.100.100 "ls /root" | |||
| 43 | ssh root@100.100.100.100 "cd /root;touch file.txt" | 39 | ssh root@100.100.100.100 "cd /root;touch file.txt" |
| 44 | ``` | 40 | ``` |
| 45 | 41 | ||
| 46 | 42 | **Displays currently logged in users in the system** | |
| 47 | |||
| 48 | ##### Displays currently logged in users in the system | ||
| 49 | 43 | ||
| 50 | ```bash | 44 | ```bash |
| 51 | w | 45 | w |
| 52 | ``` | 46 | ``` |
| 53 | 47 | ||
| 54 | 48 | **Displays Linux system information** | |
| 55 | |||
| 56 | ##### Displays Linux system information | ||
| 57 | 49 | ||
| 58 | ```bash | 50 | ```bash |
| 59 | uname | 51 | uname |
| 60 | ``` | 52 | ``` |
| 61 | 53 | ||
| 62 | 54 | **Displays kernel release information** | |
| 63 | |||
| 64 | ##### Displays kernel release information | ||
| 65 | 55 | ||
| 66 | ```bash | 56 | ```bash |
| 67 | uname -r | 57 | uname -r |
| 68 | ``` | 58 | ``` |
| 69 | 59 | ||
| 70 | 60 | **Shows the system hostname** | |
| 71 | |||
| 72 | ##### Shows the system hostname | ||
| 73 | 61 | ||
| 74 | ```bash | 62 | ```bash |
| 75 | hostname | 63 | hostname |
| 76 | ``` | 64 | ``` |
| 77 | 65 | ||
| 78 | 66 | **Shows system reboot history** | |
| 79 | |||
| 80 | ##### Shows system reboot history | ||
| 81 | 67 | ||
| 82 | ```bash | 68 | ```bash |
| 83 | last reboot | 69 | last reboot |
| 84 | ``` | 70 | ``` |
| 85 | 71 | ||
| 86 | 72 | **Displays information about the user** | |
| 87 | |||
| 88 | ##### Displays information about the user | ||
| 89 | 73 | ||
| 90 | ```bash | 74 | ```bash |
| 91 | sudo apt install finger | 75 | sudo apt install finger |
| 92 | finger <username> | 76 | finger <username> |
| 93 | ``` | 77 | ``` |
| 94 | 78 | ||
| 95 | 79 | **Displays IP addresses and all the network interfaces** | |
| 96 | |||
| 97 | ##### Displays IP addresses and all the network interfaces | ||
| 98 | 80 | ||
| 99 | ```bash | 81 | ```bash |
| 100 | ip addr show | 82 | ip addr show |
| 101 | ``` | 83 | ``` |
| 102 | 84 | ||
| 103 | 85 | **Downloads a file from an online source** | |
| 104 | |||
| 105 | ##### Downloads a file from an online source | ||
| 106 | 86 | ||
| 107 | ```bash | 87 | ```bash |
| 108 | wget https://example.com/example.tgz | 88 | wget https://example.com/example.tgz |
| @@ -110,9 +90,7 @@ wget https://example.com/example.tgz | |||
| 110 | 90 | ||
| 111 | Note: If URL contains ?, & enclose the URL in double quotes. | 91 | Note: If URL contains ?, & enclose the URL in double quotes. |
| 112 | 92 | ||
| 113 | 93 | **Compress a file with gzip** | |
| 114 | |||
| 115 | ##### Compress a file with gzip | ||
| 116 | 94 | ||
| 117 | ```bash | 95 | ```bash |
| 118 | # will not keep the original file | 96 | # will not keep the original file |
| @@ -122,9 +100,7 @@ gzip file.txt | |||
| 122 | gzip --keep file.txt | 100 | gzip --keep file.txt |
| 123 | ``` | 101 | ``` |
| 124 | 102 | ||
| 125 | 103 | **Interactive disk usage analyzer** | |
| 126 | |||
| 127 | ##### Interactive disk usage analyzer | ||
| 128 | 104 | ||
| 129 | ```bash | 105 | ```bash |
| 130 | sudo apt install ncdu | 106 | sudo apt install ncdu |
| @@ -133,9 +109,7 @@ ncdu | |||
| 133 | ncdu <path/to/directory> | 109 | ncdu <path/to/directory> |
| 134 | ``` | 110 | ``` |
| 135 | 111 | ||
| 136 | 112 | **Install Node.js using the Node Version Manager** | |
| 137 | |||
| 138 | ##### Install Node.js using the Node Version Manager | ||
| 139 | 113 | ||
| 140 | ```bash | 114 | ```bash |
| 141 | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash | 115 | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash |
| @@ -144,9 +118,7 @@ source ~/.bashrc | |||
| 144 | nvm install v13 | 118 | nvm install v13 |
| 145 | ``` | 119 | ``` |
| 146 | 120 | ||
| 147 | 121 | **Too long; didn't read** | |
| 148 | |||
| 149 | ##### Too long; didn't read | ||
| 150 | 122 | ||
| 151 | ```bash | 123 | ```bash |
| 152 | npm install -g tldr | 124 | npm install -g tldr |
| @@ -154,17 +126,13 @@ npm install -g tldr | |||
| 154 | tldr tar | 126 | tldr tar |
| 155 | ``` | 127 | ``` |
| 156 | 128 | ||
| 157 | 129 | **Combine all Nginx access logs to one big log file** | |
| 158 | |||
| 159 | ##### Combine all Nginx access logs to one big log file | ||
| 160 | 130 | ||
| 161 | ```bash | 131 | ```bash |
| 162 | zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log | 132 | zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log |
| 163 | ``` | 133 | ``` |
| 164 | 134 | ||
| 165 | 135 | **Set up Redis server** | |
| 166 | |||
| 167 | ##### Set up Redis server | ||
| 168 | 136 | ||
| 169 | ```bash | 137 | ```bash |
| 170 | sudo apt install redis-server redis-tools | 138 | sudo apt install redis-server redis-tools |
| @@ -180,9 +148,7 @@ redis-cli get mykey | |||
| 180 | redis-cli | 148 | redis-cli |
| 181 | ``` | 149 | ``` |
| 182 | 150 | ||
| 183 | 151 | **Generate statistics of your webserver** | |
| 184 | |||
| 185 | ##### Generate statistics of your webserver | ||
| 186 | 152 | ||
| 187 | ```bash | 153 | ```bash |
| 188 | sudo apt install goaccess | 154 | sudo apt install goaccess |
| @@ -206,50 +172,38 @@ goaccess \ | |||
| 206 | rm /var/log/nginx/access-all.log | 172 | rm /var/log/nginx/access-all.log |
| 207 | ``` | 173 | ``` |
| 208 | 174 | ||
| 209 | 175 | **Search for a given pattern in files** | |
| 210 | |||
| 211 | ##### Search for a given pattern in files | ||
| 212 | 176 | ||
| 213 | ```bash | 177 | ```bash |
| 214 | grep -r ‘pattern’ files | 178 | grep -r ‘pattern’ files |
| 215 | ``` | 179 | ``` |
| 216 | 180 | ||
| 217 | 181 | **Find proccess ID for a specific program** | |
| 218 | |||
| 219 | ##### Find proccess ID for a specific program | ||
| 220 | 182 | ||
| 221 | ```bash | 183 | ```bash |
| 222 | pgrep nginx | 184 | pgrep nginx |
| 223 | ``` | 185 | ``` |
| 224 | 186 | ||
| 225 | 187 | **Print name of current/working directory** | |
| 226 | |||
| 227 | ##### Print name of current/working directory | ||
| 228 | 188 | ||
| 229 | ```bash | 189 | ```bash |
| 230 | pwd | 190 | pwd |
| 231 | ``` | 191 | ``` |
| 232 | 192 | ||
| 233 | 193 | **Creates a blank new file** | |
| 234 | |||
| 235 | ##### Creates a blank new file | ||
| 236 | 194 | ||
| 237 | ```bash | 195 | ```bash |
| 238 | touch newfile.txt | 196 | touch newfile.txt |
| 239 | ``` | 197 | ``` |
| 240 | 198 | ||
| 241 | 199 | **Displays first lines in a file** | |
| 242 | |||
| 243 | ##### Displays first lines in a file | ||
| 244 | 200 | ||
| 245 | ```bash | 201 | ```bash |
| 246 | # -n <x> presents the number of lines (10 by default) | 202 | # -n <x> presents the number of lines (10 by default) |
| 247 | head -n 20 somefile.txt | 203 | head -n 20 somefile.txt |
| 248 | ``` | 204 | ``` |
| 249 | 205 | ||
| 250 | 206 | **Displays last lines in a file** | |
| 251 | |||
| 252 | ##### Displays last lines in a file | ||
| 253 | 207 | ||
| 254 | ```bash | 208 | ```bash |
| 255 | # -n <x> presents the number of lines (10 by default) | 209 | # -n <x> presents the number of lines (10 by default) |
| @@ -259,17 +213,13 @@ tail -n 20 somefile.txt | |||
| 259 | tail -f somefile.txt | 213 | tail -f somefile.txt |
| 260 | ``` | 214 | ``` |
| 261 | 215 | ||
| 262 | 216 | **Count lines in a file** | |
| 263 | |||
| 264 | ##### Count lines in a file | ||
| 265 | 217 | ||
| 266 | ```bash | 218 | ```bash |
| 267 | wc -l somefile.txt | 219 | wc -l somefile.txt |
| 268 | ``` | 220 | ``` |
| 269 | 221 | ||
| 270 | 222 | **Find all instances of the file** | |
| 271 | |||
| 272 | ##### Find all instances of the file | ||
| 273 | 223 | ||
| 274 | ```bash | 224 | ```bash |
| 275 | sudo apt install mlocate | 225 | sudo apt install mlocate |
| @@ -277,49 +227,37 @@ sudo apt install mlocate | |||
| 277 | locate somefile.txt | 227 | locate somefile.txt |
| 278 | ``` | 228 | ``` |
| 279 | 229 | ||
| 280 | 230 | **Find file names that begin with ‘index’ in /home folder** | |
| 281 | |||
| 282 | ##### Find file names that begin with ‘index’ in /home folder | ||
| 283 | 231 | ||
| 284 | ```bash | 232 | ```bash |
| 285 | find /home/ -name "index" | 233 | find /home/ -name "index" |
| 286 | ``` | 234 | ``` |
| 287 | 235 | ||
| 288 | 236 | **Find files larger than 100MB in the home folder** | |
| 289 | |||
| 290 | ##### Find files larger than 100MB in the home folder | ||
| 291 | 237 | ||
| 292 | ```bash | 238 | ```bash |
| 293 | find /home -size +100M | 239 | find /home -size +100M |
| 294 | ``` | 240 | ``` |
| 295 | 241 | ||
| 296 | 242 | **Displays block devices related information** | |
| 297 | |||
| 298 | ##### Displays block devices related information | ||
| 299 | 243 | ||
| 300 | ```bash | 244 | ```bash |
| 301 | lsblk | 245 | lsblk |
| 302 | ``` | 246 | ``` |
| 303 | 247 | ||
| 304 | 248 | **Displays free space on mounted systems** | |
| 305 | |||
| 306 | ##### Displays free space on mounted systems | ||
| 307 | 249 | ||
| 308 | ```bash | 250 | ```bash |
| 309 | df -h | 251 | df -h |
| 310 | ``` | 252 | ``` |
| 311 | 253 | ||
| 312 | 254 | **Displays free and used memory in the system** | |
| 313 | |||
| 314 | ##### Displays free and used memory in the system | ||
| 315 | 255 | ||
| 316 | ```bash | 256 | ```bash |
| 317 | free -h | 257 | free -h |
| 318 | ``` | 258 | ``` |
| 319 | 259 | ||
| 320 | 260 | **Displays all active listening ports** | |
| 321 | |||
| 322 | ##### Displays all active listening ports | ||
| 323 | 261 | ||
| 324 | ```bash | 262 | ```bash |
| 325 | sudo apt install net-tools | 263 | sudo apt install net-tools |
| @@ -327,30 +265,21 @@ sudo apt install net-tools | |||
| 327 | netstat -pnltu | 265 | netstat -pnltu |
| 328 | ``` | 266 | ``` |
| 329 | 267 | ||
| 330 | 268 | **Kill a process violently** | |
| 331 | |||
| 332 | ##### Kill a process violently | ||
| 333 | 269 | ||
| 334 | ```bash | 270 | ```bash |
| 335 | kill -9 <pid> | 271 | kill -9 <pid> |
| 336 | ``` | 272 | ``` |
| 337 | 273 | ||
| 338 | 274 | **List files opened by user** | |
| 339 | |||
| 340 | ##### List files opened by user | ||
| 341 | 275 | ||
| 342 | ```bash | 276 | ```bash |
| 343 | lsof -u <user> | 277 | lsof -u <user> |
| 344 | ``` | 278 | ``` |
| 345 | 279 | ||
| 346 | 280 | **Execute "df -h", showing periodic updates** | |
| 347 | |||
| 348 | ##### Execute "df -h", showing periodic updates | ||
| 349 | 281 | ||
| 350 | ```bash | 282 | ```bash |
| 351 | # -n 1 means every second | 283 | # -n 1 means every second |
| 352 | watch -n 1 df -h | 284 | watch -n 1 df -h |
| 353 | ``` | 285 | ``` |
| 354 | |||
| 355 | |||
| 356 | |||
