Linux Survival Basics
You don’t need to know all of Linux. Nobody knows all of Linux. The people who say they do are lying, or they wrote it, and those people are tired too.
What follows is the subset that keeps servers alive. It assumes a Debian/Ubuntu-ish box, because that’s what you’ll get handed nine times out of ten. Where RHEL-family distros differ, I’ll mention it with the enthusiasm it deserves.
Where am I, and what have I done
Section titled “Where am I, and what have I done”pwd # where you arels -lah # what's here, including the hidden stuff that's actually the problemcd - # go back to where you just were. You will use this constantly.whoami # who you are. Check this before you rm anything.hostname # which server you are on. Check this before you rm ANYTHING.Files and the art of not destroying them
Section titled “Files and the art of not destroying them”cp -a src/ dest/ # -a keeps perms, ownership, timestamps. Plain cp doesn't.mv old new # rename. No undo.rm -rf thing/ # delete. No undo. No trash can. No mercy.ln -s /real/path link # symlink. Target first, link name second. You'll get it backwards once.Before any rm -rf with a variable in it, echo the command first. rm -rf "$DIR/"* with an empty $DIR is how people end up learning about backups in the worst possible way.
Finding things:
find /etc -name "*.conf" -mtime -1 # configs changed in the last day (i.e. "who touched this")grep -rn "listen" /etc/nginx/ # recursive search with line numbersgrep -rIl "password" /opt/app # list files containing the word. You'll find some. You won't like it.Permissions
Section titled “Permissions”ls -l gives you something like -rw-r--r-- 1 www-data www-data. That’s type, then owner / group / everyone, then owner, then group.
chmod 640 file # owner rw, group r, everyone nothingchmod +x script.sh # make it runnablechown -R app:app /srv/appThe numbers: read = 4, write = 2, execute = 1. Add them up. 755 for directories and scripts, 644 for normal files, 600 for anything secret.
Directories need x to be entered. A directory with r but no x lets you list names but not open anything inside. Every few years this confuses me all over again.
Users and sudo
Section titled “Users and sudo”sudo -i # root shell. Use sparingly, exit promptly.sudo -u app bash # become the service user and test what IT can seeadduser deploy # Debian's friendly wrapper. useradd is the unfriendly one.usermod -aG docker deploy # -a is APPEND. Forget it and you wipe their other groups.id deploy # verify, because you will forget the -a eventuallyGroup changes only apply to new logins. Log out and back in. Yes, really. No, newgrp doesn’t count, it’ll only confuse you later.
ssh-keygen -t ed25519 -C "you@laptop"ssh-copy-id user@hostThen turn off password logins. The full walkthrough, plus the config file and “Permission denied (publickey)” debugging, is on SSH Keys.
Packages
Section titled “Packages”# Debian / Ubuntusudo apt update && sudo apt upgradeapt list --upgradableapt policy nginx # which version, from which repo, and why
# RHEL / Rocky / Alma / Fedorasudo dnf upgradednf info nginxapt update refreshes the list. apt upgrade actually installs things. They are different. Every junior gets bitten by this once.
Processes and ports
Section titled “Processes and ports”ps aux --sort=-%mem | head # who's eating the RAMtop # or htop, if someone was nice enough to install itkill PID # polite (SIGTERM)kill -9 PID # not polite (SIGKILL). The process doesn't get to clean up.ss -tulpn # what's listening on which port, and which process owns itss -tulpn answers “why won’t my thing start, address already in use” in about one second. Use it before you start guessing.
- Zipping & Unzipping, SSH Keys, Setting a Static IP
- Services with systemd for keeping things running.
- When Things Break for when they don’t.