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