|
| 1 | +--- |
| 2 | +layout: "@layouts/MDLayout.astro" |
| 3 | +title: Linux Cheatsheet |
| 4 | +description: Common Linux commands. |
| 5 | +keywords: ["cheatsheet", "linux"] |
| 6 | + |
| 7 | +indexTitle: Linux |
| 8 | +--- |
| 9 | + |
| 10 | +## Dummy Values |
| 11 | + |
| 12 | +Unless stated otherwise, we use the following strings for the examples below: |
| 13 | + |
| 14 | +- **User:** `simshadows` |
| 15 | +- **Group:** `mygroup` |
| 16 | +- **Arbitrary File:** `myfile.txt` |
| 17 | +- **Arbitrary Directory:** `mydir` |
| 18 | +- *Other values such as IP addresses will be implicitly assumed to be example values.* |
| 19 | + |
| 20 | +## General |
| 21 | + |
| 22 | +### bash and Common CLI Utilities |
| 23 | + |
| 24 | +*See [`bash` Cheatsheet.](/c/cheatsheets/bash/)* |
| 25 | + |
| 26 | +### SSH and Remote Access |
| 27 | + |
| 28 | +Client commands: |
| 29 | + |
| 30 | +```bash |
| 31 | +# Open remote terminal |
| 32 | +ssh 192.168.0.2 |
| 33 | + |
| 34 | +# Generate `ed25519` key |
| 35 | +ssh-keygen -t ed25519 -C "simshadows@myuri 1970-01-01" |
| 36 | +# Check your public key |
| 37 | +cat ~/.ssh/id_ed25519.pub |
| 38 | + |
| 39 | +# Copy your default key to SSH server |
| 40 | +ssh-copy-id 192.168.0.2 |
| 41 | +# Copy a key to SSH server with more specificity (`-p` is the server port) |
| 42 | +ssh-copy-id -i ~/.ssh/id_ed25519.pub -p 8765 simshadows@192.168.0.2 |
| 43 | +``` |
| 44 | + |
| 45 | +Server commands: |
| 46 | + |
| 47 | +```bash |
| 48 | +# Check accepted public keys |
| 49 | +cat ~/.ssh/authorized_keys |
| 50 | +``` |
| 51 | + |
| 52 | +*Also see [Other Common Tasks](./more/).* |
| 53 | + |
| 54 | +## Admin |
| 55 | + |
| 56 | +### Package/Distro/Hypervisor |
| 57 | + |
| 58 | +#### Debian |
| 59 | + |
| 60 | +```bash |
| 61 | +# Full system update |
| 62 | +## Step 1 |
| 63 | +apt update |
| 64 | +## Step 2 |
| 65 | +apt upgrade |
| 66 | +## Step 3 |
| 67 | +apt full-upgrade |
| 68 | + |
| 69 | +# Install package `nmap` |
| 70 | +apt install nmap |
| 71 | + |
| 72 | +# Search for packages with keyword `nmap` |
| 73 | +apt search nmap |
| 74 | +``` |
| 75 | + |
| 76 | +#### Arch Linux |
| 77 | + |
| 78 | +```bash |
| 79 | +# Full system update |
| 80 | +## Step 1: Update keyring |
| 81 | +pacman -Sy --needed archlinux-keyring |
| 82 | +## Step 2: Update all packages |
| 83 | +pacman -Syu |
| 84 | + |
| 85 | +# Install package `gvim` |
| 86 | +pacman -Syu gvim |
| 87 | +``` |
| 88 | + |
| 89 | +#### Windows WSL |
| 90 | + |
| 91 | +See [this article](https://learn.microsoft.com/en-us/windows/wsl/basic-commands) for more commands. |
| 92 | + |
| 93 | +```bash |
| 94 | +# Start WSL |
| 95 | +wsl |
| 96 | +# Start WSL as user `root` |
| 97 | +wsl -u root |
| 98 | +``` |
| 99 | + |
| 100 | +### User Management |
| 101 | + |
| 102 | +```bash |
| 103 | +# List users (each line is a user) |
| 104 | +cat /etc/passwd |
| 105 | +# List groups (each line is a group, GID, and its members) |
| 106 | +cat /etc/group |
| 107 | + |
| 108 | +# Create new user |
| 109 | +useradd -m -s /bin/bash simshadows |
| 110 | + |
| 111 | +# Change password for a user |
| 112 | +passwd simshadows |
| 113 | + |
| 114 | +# Append group `sudo` to user |
| 115 | +usermod -a -G sudo simshadows |
| 116 | + |
| 117 | +# Update sudoers file |
| 118 | +visudo |
| 119 | + |
| 120 | +# Read user uid |
| 121 | +id -u simshadows |
| 122 | +``` |
| 123 | + |
| 124 | +### Disk Management |
| 125 | + |
| 126 | +```bash |
| 127 | +# Listing things out |
| 128 | +fdisk -l |
| 129 | +lsblk -o +MODEL,SERIAL |
| 130 | +mount |
| 131 | + |
| 132 | +# Manage partition table for disk `/dev/sda` |
| 133 | +fdisk /dev/sda |
| 134 | + |
| 135 | +# Format partition `/dev/sda1` as ext4 |
| 136 | +mkfs.ext4 /dev/sda1 |
| 137 | +# Format partition `/dev/sda1` as FAT32 |
| 138 | +mkfs.fat -F32 /dev/sda1 |
| 139 | + |
| 140 | +# Mount partition `/dev/sda1` to `/mnt/foobar` |
| 141 | +mount /dev/sda1 /mnt/foobar |
| 142 | +# Umount `/mnt/foobar` |
| 143 | +umount /mnt/foobar |
| 144 | + |
| 145 | +# Print all SMART info for disk `/dev/sda` |
| 146 | +smartctl /dev/sda -a |
| 147 | +``` |
| 148 | + |
| 149 | +Common `fdisk` commands: |
| 150 | + |
| 151 | +### Filesystem Permissions |
| 152 | + |
| 153 | +```bash |
| 154 | +# View permissions |
| 155 | +ls -la |
| 156 | + |
| 157 | +# Change file owner |
| 158 | +chown simshadows myfile.txt |
| 159 | +# Change file group |
| 160 | +chgrp mygroup myfile.txt |
| 161 | + |
| 162 | +# Set file permissions to `rx-r-xr--` (octal mode) |
| 163 | +chmod 654 myfile.txt |
| 164 | +# rwx 7 | -wx 3 |
| 165 | +# rw- 6 | -w- 2 |
| 166 | +# r-x 5 | --x 1 |
| 167 | +# r-- 4 | --- 0 |
| 168 | + |
| 169 | +# Set `x` for user, group, and other (symbolic mode) |
| 170 | +chmod a+x myfile.txt |
| 171 | +# Same effect as `a+x` except the bits set by the umask are not affected |
| 172 | +chmod +x myfile.txt |
| 173 | +# Set `x` for user, set `r` for group and other, and remove `w` for other |
| 174 | +chmod u+x,go+r,o-w myfile.txt |
| 175 | +# u User | - Remove | r Read |
| 176 | +# g Group | + Add | w Write |
| 177 | +# o Other | = (see `man chmod`) | x Execute |
| 178 | +# a All | | |
| 179 | + |
| 180 | +# View ACL |
| 181 | +getfacl myfile.txt |
| 182 | +getfacl mydir |
| 183 | + |
| 184 | +# TODO: What about setting ACL values? |
| 185 | +# TODO: What about setuid, setguid, and sticky bit? |
| 186 | +``` |
| 187 | + |
| 188 | +### Network Management |
| 189 | + |
| 190 | +```bash |
| 191 | +ip a |
| 192 | + |
| 193 | +# List all open ports (numeric addresses) |
| 194 | +ss -ln |
| 195 | +# List all open TCP/UDP ports (numeric addresses) |
| 196 | +ss -tuln |
| 197 | + |
| 198 | +# Check route table |
| 199 | +ip r |
| 200 | +# It's short for `ip route show`. Manpage `man ip route`. |
| 201 | +# Default gateway starts with `default via`. |
| 202 | +``` |
| 203 | + |
| 204 | +### Network Auditing |
| 205 | + |
| 206 | +```bash |
| 207 | +ping -c 5 8.8.8.8 |
| 208 | +traceroute 8.8.8.8 |
| 209 | +``` |
| 210 | + |
| 211 | +`nmap` (we use `192.168.0.2` and `192.168.0.0/24` for these examples): |
| 212 | + |
| 213 | +```bash |
| 214 | +# Host discovery |
| 215 | +nmap -sn 192.168.0.0/24 |
| 216 | + |
| 217 | +# TCP scan (SYN --> SYN ACK --> ACK) |
| 218 | +nmap -sT 192.168.0.0/24 |
| 219 | +# TCP scan (SYN --> SYN ACK) (Use this if you're having firewall issues.) |
| 220 | +nmap -sS 192.168.0.0/24 |
| 221 | +# UDP scan |
| 222 | +nmap -sU 192.168.0.0/24 |
| 223 | + |
| 224 | +# What OS is a host running? (Also does other things.) |
| 225 | +nmap -A 192.168.0.2 |
| 226 | + |
| 227 | +# Full TCP scan and `-A` on a host. Assume the host exists. |
| 228 | +nmap -sT -p0- -A -Pn -T4 192.168.0.2 |
| 229 | + |
| 230 | +# Run all vulnerability scan scripts on a host |
| 231 | +nmap --script vuln 192.168.0.2 |
| 232 | +``` |
| 233 | + |
| 234 | +Common subnet masks: |
| 235 | + |
| 236 | +<table class="general" style="font-family: monospace;"> |
| 237 | +<thead> |
| 238 | +<tr> |
| 239 | + <th>IPv4 Subnet Mask</th> |
| 240 | + <th>Prefix</th> |
| 241 | + <th>Bits</th> |
| 242 | +</tr> |
| 243 | +</thead> |
| 244 | +<tbody> |
| 245 | +<tr> |
| 246 | + <td>255.255.255.255</td> |
| 247 | + <td>/32</td> |
| 248 | + <td>0</td> |
| 249 | +</tr> |
| 250 | +<tr> |
| 251 | + <td>255.255.255.240</td> |
| 252 | + <td>/28</td> |
| 253 | + <td>4</td> |
| 254 | +</tr> |
| 255 | +<tr> |
| 256 | + <td>255.255.255.0</td> |
| 257 | + <td>/24</td> |
| 258 | + <td>8</td> |
| 259 | +</tr> |
| 260 | +</tbody> |
| 261 | +</table> |
| 262 | + |
| 263 | +### systemd |
| 264 | + |
| 265 | +The examples below use a placeholder unit name `foobar.service`. |
| 266 | + |
| 267 | +```bash |
| 268 | +# Start unit now, and always auto-start on boot |
| 269 | +systemctl enable --now foobar.service |
| 270 | +# Restart unit |
| 271 | +systemctl restart foobar.service |
| 272 | +# Check the status of a unit |
| 273 | +systemctl status foobar.service |
| 274 | + |
| 275 | +# User unit files are located at: |
| 276 | +# ~/.config/systemd/user/ |
| 277 | +# Add `--user` to operate on them like: |
| 278 | +systemctl --user enable --now foobar.service |
| 279 | +``` |
| 280 | + |
| 281 | +## More References |
| 282 | + |
| 283 | +- My Arch Linux [setup guide](https://github.com/simshadows/sims-dotfiles/tree/master/system-setup-scripts-and-guides/arch-linux) and [setup script](https://github.com/simshadows/sims-dotfiles/blob/master/system-setup-scripts-and-guides/arch-linux/stage3.sh) |
| 284 | + |
0 commit comments