Skip to content

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.

Terminal window
pwd # where you are
ls -lah # what's here, including the hidden stuff that's actually the problem
cd - # 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.
Terminal window
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:

Terminal window
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 numbers
grep -rIl "password" /opt/app # list files containing the word. You'll find some. You won't like it.

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.

Terminal window
chmod 640 file # owner rw, group r, everyone nothing
chmod +x script.sh # make it runnable
chown -R app:app /srv/app

The 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.

Terminal window
sudo -i # root shell. Use sparingly, exit promptly.
sudo -u app bash # become the service user and test what IT can see
adduser 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 eventually

Group changes only apply to new logins. Log out and back in. Yes, really. No, newgrp doesn’t count, it’ll only confuse you later.

Terminal window
ssh-keygen -t ed25519 -C "you@laptop"
ssh-copy-id user@host

Then turn off password logins. The full walkthrough, plus the config file and “Permission denied (publickey)” debugging, is on SSH Keys.

Terminal window
# Debian / Ubuntu
sudo apt update && sudo apt upgrade
apt list --upgradable
apt policy nginx # which version, from which repo, and why
# RHEL / Rocky / Alma / Fedora
sudo dnf upgrade
dnf info nginx

apt update refreshes the list. apt upgrade actually installs things. They are different. Every junior gets bitten by this once.

Terminal window
ps aux --sort=-%mem | head # who's eating the RAM
top # or htop, if someone was nice enough to install it
kill 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 it

ss -tulpn answers “why won’t my thing start, address already in use” in about one second. Use it before you start guessing.