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