diff options
Diffstat (limited to 'posts/2021-08-01-linux-cheatsheet.md')
| -rw-r--r-- | posts/2021-08-01-linux-cheatsheet.md | 397 |
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 | --- | ||
| 2 | Title: List of essential Linux commands for server management | ||
| 3 | Description: List of essential Linux commands for server management | ||
| 4 | Slug: linux-cheatsheet | ||
| 5 | Listing: true | ||
| 6 | Created: 2021-08-01 | ||
| 7 | Tags: [] | ||
| 8 | --- | ||
| 9 | |||
| 10 | **Table of contents** | ||
| 11 | |||
| 12 | 1. [Generate SSH key](#generate-ssh-key) | ||
| 13 | 2. [Login to host via SSH](#login-to-host-via-ssh) | ||
| 14 | 3. [Execute command on a server through SSH](#execute-command-on-a-server-through-ssh) | ||
| 15 | 4. [Displays currently logged in users in the system](#displays-currently-logged-in-users-in-the-system) | ||
| 16 | 5. [Displays Linux system information](#displays-linux-system-information) | ||
| 17 | 6. [Displays kernel release information](#displays-kernel-release-information) | ||
| 18 | 7. [Shows the system hostname](#shows-the-system-hostname) | ||
| 19 | 8. [Shows system reboot history](#shows-system-reboot-history) | ||
| 20 | 9. [Displays information about the user](#displays-information-about-the-user) | ||
| 21 | 10. [Displays IP addresses and all the network interfaces](#displays-ip-addresses-and-all-the-network-interfaces) | ||
| 22 | 11. [Downloads a file from an online source](#downloads-a-file-from-an-online-source) | ||
| 23 | 12. [Compress a file with gzip](#compress-a-file-with-gzip) | ||
| 24 | 13. [Interactive disk usage analyzer](#interactive-disk-usage-analyzer) | ||
| 25 | 14. [Install Node.js using the Node Version Manager](#install-nodejs-using-the-node-version-manager) | ||
| 26 | 15. [Too long; didn't read](#too-long-didnt-read) | ||
| 27 | 16. [Combine all Nginx access logs to one big log file](#combine-all-nginx-access-logs-to-one-big-log-file) | ||
| 28 | 17. [Set up Redis server](#set-up-redis-server) | ||
| 29 | 18. [Generate statistics of your webserver](#generate-statistics-of-your-webserver) | ||
| 30 | 19. [Search for a given pattern in files](#search-for-a-given-pattern-in-files) | ||
| 31 | 20. [Find proccess ID for a specific program](#find-proccess-id-for-a-specific-program) | ||
| 32 | 21. [Print name of current/working directory](#print-name-of-currentworking-directory) | ||
| 33 | 22. [Creates a blank new file](#creates-a-blank-new-file) | ||
| 34 | 23. [Displays first lines in a file](#displays-first-lines-in-a-file) | ||
| 35 | 24. [Displays last lines in a file](#displays-last-lines-in-a-file) | ||
| 36 | 25. [Count lines in a file](#count-lines-in-a-file) | ||
| 37 | 26. [Find all instances of the file](#find-all-instances-of-the-file) | ||
| 38 | 27. [Find file names that begin with ‘index’ in /home folder](#find-file-names-that-begin-with-index-in-home-folder) | ||
| 39 | 28. [Find files larger than 100MB in the home folder](#find-files-larger-than-100mb-in-the-home-folder) | ||
| 40 | 29. [Displays block devices related information](#displays-block-devices-related-information) | ||
| 41 | 30. [Displays free space on mounted systems](#displays-free-space-on-mounted-systems) | ||
| 42 | 31. [Displays free and used memory in the system](#displays-free-and-used-memory-in-the-system) | ||
| 43 | 32. [Displays all active listening ports](#displays-all-active-listening-ports) | ||
| 44 | 33. [Kill a process violently](#kill-a-process-violently) | ||
| 45 | 34. [List files opened by user](#list-files-opened-by-user) | ||
| 46 | 35. [Execute "df -h", showing periodic updates](#execute-df--h-showing-periodic-updates) | ||
| 47 | |||
| 48 | |||
| 49 | ##### Generate SSH key | ||
| 50 | |||
| 51 | ```bash | ||
| 52 | ssh-keygen -t ed25519 -C "your_email@example.com" | ||
| 53 | |||
| 54 | # when no support for Ed25519 present | ||
| 55 | ssh-keygen -t rsa -b 4096 -C "your_email@example.com" | ||
| 56 | ``` | ||
| 57 | |||
| 58 | Note: 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 | ||
| 66 | ssh host | ||
| 67 | |||
| 68 | # connect to host as user | ||
| 69 | ssh <user>@<host> | ||
| 70 | |||
| 71 | # connect to host using port | ||
| 72 | ssh -p <port> <user>@<host> | ||
| 73 | ``` | ||
| 74 | |||
| 75 | |||
| 76 | |||
| 77 | ##### Execute command on a server through SSH | ||
| 78 | |||
| 79 | ```bash | ||
| 80 | # execute one command | ||
| 81 | ssh root@100.100.100.100 "ls /root" | ||
| 82 | |||
| 83 | # execute many commands | ||
| 84 | ssh 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 | ||
| 92 | w | ||
| 93 | ``` | ||
| 94 | |||
| 95 | |||
| 96 | |||
| 97 | ##### Displays Linux system information | ||
| 98 | |||
| 99 | ```bash | ||
| 100 | uname | ||
| 101 | ``` | ||
| 102 | |||
| 103 | |||
| 104 | |||
| 105 | ##### Displays kernel release information | ||
| 106 | |||
| 107 | ```bash | ||
| 108 | uname -r | ||
| 109 | ``` | ||
| 110 | |||
| 111 | |||
| 112 | |||
| 113 | ##### Shows the system hostname | ||
| 114 | |||
| 115 | ```bash | ||
| 116 | hostname | ||
| 117 | ``` | ||
| 118 | |||
| 119 | |||
| 120 | |||
| 121 | ##### Shows system reboot history | ||
| 122 | |||
| 123 | ```bash | ||
| 124 | last reboot | ||
| 125 | ``` | ||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | ##### Displays information about the user | ||
| 130 | |||
| 131 | ```bash | ||
| 132 | sudo apt install finger | ||
| 133 | finger <username> | ||
| 134 | ``` | ||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | ##### Displays IP addresses and all the network interfaces | ||
| 139 | |||
| 140 | ```bash | ||
| 141 | ip addr show | ||
| 142 | ``` | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | ##### Downloads a file from an online source | ||
| 147 | |||
| 148 | ```bash | ||
| 149 | wget https://example.com/example.tgz | ||
| 150 | ``` | ||
| 151 | |||
| 152 | Note: 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 | ||
| 160 | gzip file.txt | ||
| 161 | |||
| 162 | # will keep the original file | ||
| 163 | gzip --keep file.txt | ||
| 164 | ``` | ||
| 165 | |||
| 166 | |||
| 167 | |||
| 168 | ##### Interactive disk usage analyzer | ||
| 169 | |||
| 170 | ```bash | ||
| 171 | sudo apt install ncdu | ||
| 172 | |||
| 173 | ncdu | ||
| 174 | ncdu <path/to/directory> | ||
| 175 | ``` | ||
| 176 | |||
| 177 | |||
| 178 | |||
| 179 | ##### Install Node.js using the Node Version Manager | ||
| 180 | |||
| 181 | ```bash | ||
| 182 | curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash | ||
| 183 | source ~/.bashrc | ||
| 184 | |||
| 185 | nvm install v13 | ||
| 186 | ``` | ||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | ##### Too long; didn't read | ||
| 191 | |||
| 192 | ```bash | ||
| 193 | npm install -g tldr | ||
| 194 | |||
| 195 | tldr tar | ||
| 196 | ``` | ||
| 197 | |||
| 198 | |||
| 199 | |||
| 200 | ##### Combine all Nginx access logs to one big log file | ||
| 201 | |||
| 202 | ```bash | ||
| 203 | zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log | ||
| 204 | ``` | ||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | ##### Set up Redis server | ||
| 209 | |||
| 210 | ```bash | ||
| 211 | sudo apt install redis-server redis-tools | ||
| 212 | |||
| 213 | # check if server is running | ||
| 214 | sudo service redis status | ||
| 215 | |||
| 216 | # set and get a key value | ||
| 217 | redis-cli set mykey myvalue | ||
| 218 | redis-cli get mykey | ||
| 219 | |||
| 220 | # interactive shell | ||
| 221 | redis-cli | ||
| 222 | ``` | ||
| 223 | |||
| 224 | |||
| 225 | |||
| 226 | ##### Generate statistics of your webserver | ||
| 227 | |||
| 228 | ```bash | ||
| 229 | sudo apt install goaccess | ||
| 230 | |||
| 231 | # check if installed | ||
| 232 | goaccess -v | ||
| 233 | |||
| 234 | # combine logs | ||
| 235 | zcat -f /var/log/nginx/access.log* > /var/log/nginx/access-all.log | ||
| 236 | |||
| 237 | # export to single html | ||
| 238 | goaccess \ | ||
| 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 | ||
| 247 | rm /var/log/nginx/access-all.log | ||
| 248 | ``` | ||
| 249 | |||
| 250 | |||
| 251 | |||
| 252 | ##### Search for a given pattern in files | ||
| 253 | |||
| 254 | ```bash | ||
| 255 | grep -r ‘pattern’ files | ||
| 256 | ``` | ||
| 257 | |||
| 258 | |||
| 259 | |||
| 260 | ##### Find proccess ID for a specific program | ||
| 261 | |||
| 262 | ```bash | ||
| 263 | pgrep nginx | ||
| 264 | ``` | ||
| 265 | |||
| 266 | |||
| 267 | |||
| 268 | ##### Print name of current/working directory | ||
| 269 | |||
| 270 | ```bash | ||
| 271 | pwd | ||
| 272 | ``` | ||
| 273 | |||
| 274 | |||
| 275 | |||
| 276 | ##### Creates a blank new file | ||
| 277 | |||
| 278 | ```bash | ||
| 279 | touch 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) | ||
| 288 | head -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) | ||
| 297 | tail -n 20 somefile.txt | ||
| 298 | |||
| 299 | # -f follows the changes in file (doesn't closes) | ||
| 300 | tail -f somefile.txt | ||
| 301 | ``` | ||
| 302 | |||
| 303 | |||
| 304 | |||
| 305 | ##### Count lines in a file | ||
| 306 | |||
| 307 | ```bash | ||
| 308 | wc -l somefile.txt | ||
| 309 | ``` | ||
| 310 | |||
| 311 | |||
| 312 | |||
| 313 | ##### Find all instances of the file | ||
| 314 | |||
| 315 | ```bash | ||
| 316 | sudo apt install mlocate | ||
| 317 | |||
| 318 | locate somefile.txt | ||
| 319 | ``` | ||
| 320 | |||
| 321 | |||
| 322 | |||
| 323 | ##### Find file names that begin with ‘index’ in /home folder | ||
| 324 | |||
| 325 | ```bash | ||
| 326 | find /home/ -name "index" | ||
| 327 | ``` | ||
| 328 | |||
| 329 | |||
| 330 | |||
| 331 | ##### Find files larger than 100MB in the home folder | ||
| 332 | |||
| 333 | ```bash | ||
| 334 | find /home -size +100M | ||
| 335 | ``` | ||
| 336 | |||
| 337 | |||
| 338 | |||
| 339 | ##### Displays block devices related information | ||
| 340 | |||
| 341 | ```bash | ||
| 342 | lsblk | ||
| 343 | ``` | ||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | ##### Displays free space on mounted systems | ||
| 348 | |||
| 349 | ```bash | ||
| 350 | df -h | ||
| 351 | ``` | ||
| 352 | |||
| 353 | |||
| 354 | |||
| 355 | ##### Displays free and used memory in the system | ||
| 356 | |||
| 357 | ```bash | ||
| 358 | free -h | ||
| 359 | ``` | ||
| 360 | |||
| 361 | |||
| 362 | |||
| 363 | ##### Displays all active listening ports | ||
| 364 | |||
| 365 | ```bash | ||
| 366 | sudo apt install net-tools | ||
| 367 | |||
| 368 | netstat -pnltu | ||
| 369 | ``` | ||
| 370 | |||
| 371 | |||
| 372 | |||
| 373 | ##### Kill a process violently | ||
| 374 | |||
| 375 | ```bash | ||
| 376 | kill -9 <pid> | ||
| 377 | ``` | ||
| 378 | |||
| 379 | |||
| 380 | |||
| 381 | ##### List files opened by user | ||
| 382 | |||
| 383 | ```bash | ||
| 384 | lsof -u <user> | ||
| 385 | ``` | ||
| 386 | |||
| 387 | |||
| 388 | |||
| 389 | ##### Execute "df -h", showing periodic updates | ||
| 390 | |||
| 391 | ```bash | ||
| 392 | # -n 1 means every second | ||
| 393 | watch -n 1 df -h | ||
| 394 | ``` | ||
| 395 | |||
| 396 | |||
| 397 | |||
