← Reference library

Linux reference

Task-first recipes for the things you actually do at 2am.

Find what is using a port

Problem · Something owns :8080 and your service can't bind.

ss -tulpn | grep :8080
sudo lsof -i :8080
What the output means

ss/lsof map the listening socket to a PID and command — that's who to stop.

Common traps

`netstat` is deprecated; prefer `ss`. Run with sudo to see other users' processes.

Puzzle: The Service Won't Start

Check why a service failed

Problem · A unit is 'failed' and you need the real reason.

systemctl status <svc>
journalctl -u <svc> -n 50 --no-pager
journalctl -u <svc> -b
What the output means

status = current state; journal = the story. Look for the last FATAL/exit-code line.

Common traps

After editing a unit file you must `systemctl daemon-reload` or changes are ignored.

Track: Linux — systemd

Find large files safely

Problem · Disk is filling and you need the offenders fast.

df -h
du -xsh /* 2>/dev/null | sort -h | tail
du -xsh /var/* | sort -h | tail
What the output means

df finds the full filesystem; du drills into it. `-x` stays on one filesystem.

Common traps

If df and du disagree, suspect a deleted-but-open file: `lsof | grep deleted`.

Puzzle: Disk Full, But du Says It's Fine

Inspect logs with journalctl

Problem · You need recent, relevant log lines without drowning.

journalctl -u nginx -f
journalctl --since '10 min ago'
journalctl -p err -b
What the output means

-u scopes to a unit, -f follows, -p err shows errors this boot.

Common traps

Journald may be volatile (lost on reboot) unless persistent storage is enabled.

Check memory pressure

Problem · Is the box actually low on memory, or is that just cache?

free -h
cat /proc/meminfo | grep -i available
dmesg -T | grep -i oom
What the output means

Look at 'available', not 'free' — Linux uses spare RAM as cache. OOM kills show in dmesg.

Common traps

High 'used' with healthy 'available' is normal. Don't panic at buff/cache.

Understand load average

Problem · Load is high but you're not sure what it means.

uptime
top  # check %wa (IO wait)
iostat -x 1 3
ps -eo state,pid,cmd | grep '^D'
What the output means

Load counts runnable AND uninterruptible (D-state) tasks. High load + idle CPU = I/O wait.

Common traps

Adding CPUs won't help an I/O-bound box. Check the disk first.

Blog: Load average without lies

Trace DNS resolution

Problem · A name won't resolve and you need to know which step fails.

dig +trace example.com
dig @<resolver> example.com
getent hosts example.com
cat /etc/resolv.conf
What the output means

Compare what your resolver returns vs the authoritative answer; getent honours nsswitch (incl. /etc/hosts).

Common traps

/etc/hosts overrides DNS. A wrong answer on one box is usually local config.

Reference: DNS

Check disk and inode usage

Problem · Writes fail with 'No space left' but df shows free space.

df -h
df -i
What the output means

You can exhaust inodes (df -i) while bytes are free — common with millions of tiny files.

Common traps

'No space left on device' can mean inodes, not bytes. Always check `df -i` too.