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