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