Field Notes: Running Servers Without Surprises
Practical habits collected from real outages, grouped by the mistake they prevent. Each note is short; the linked lessons have the details.
Never apply a config you haven't validated
Most self-inflicted outages are a reload of a broken file. Wrap every change in "copy, edit, validate, apply, verify, or roll back". Here is that wrapper for Nginx, tried with a good and a broken change on a running server:
mkdir -p www && echo "site v1" > www/index.html
cat > nginx.conf <<'EOF'
pid nginx.pid;
error_log stderr;
events {}
http { access_log off; server { listen 127.0.0.1:8080; root www; } }
EOF
nginx -p "$PWD" -c nginx.conf -e stderr
for i in $(seq 50); do curl -s localhost:8080/ > /dev/null && break; sleep 0.1; done
apply_nginx() { # usage: apply_nginx NEW_CONFIG_FILE
cp nginx.conf nginx.conf.bak
cp "$1" nginx.conf
if ! nginx -t -p "$PWD" -c nginx.conf -e stderr > check.log 2>&1; then
echo "REJECTED: $(grep -o 'unexpected.*' check.log)"
cp nginx.conf.bak nginx.conf; return 1
fi
nginx -p "$PWD" -c nginx.conf -e stderr -s reload 2>/dev/null; sleep 0.5
if curl -sf localhost:8080/health > /dev/null; then echo "APPLIED"
else echo "health check failed, rolling back"; cp nginx.conf.bak nginx.conf; nginx -p "$PWD" -c nginx.conf -e stderr -s reload 2>/dev/null; fi
}
echo "ok" > www/health
sed 's/root www;/root www; add_header X-Env prod;/' nginx.conf > good.conf
sed 's/root www;/root www; add_header X-Env prod/' nginx.conf > broken.conf # missing semicolon
apply_nginx broken.conf; curl -s localhost:8080/
apply_nginx good.conf; curl -sI localhost:8080/ | grep -i x-env | tr -d '\r'
nginx -p "$PWD" -c nginx.conf -e stderr -s quit 2>/dev/nullREJECTED: unexpected "}" in /home/learner/nginx.conf:4
site v1
APPLIED
X-Env: prod
The broken change never reached the running server, and the site kept serving. The same shape works for every daemon in the Toolkit: sshd, sudoers, fstab, nftables (with an automatic rollback timer), units.
Keep /etc under version control
"What changed on this server last week?" has a one-command answer if /etc is a Git repository. etckeeper does this automatically (and commits before and after every apt run). The idea in miniature:
mkdir -p etc/ssh && cd etc
printf 'PermitRootLogin no\nPasswordAuthentication no\n' > ssh/sshd_config
echo "web1" > hostname
git init -q && git add -A && git commit -qm "baseline after provisioning"
sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' ssh/sshd_config # someone "just testing"
git status --short
git diff --stat
git diff -U0 | grep '^[-+][^-+]' M ssh/sshd_config
ssh/sshd_config | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
-PasswordAuthentication no
+PasswordAuthentication yes
A drift like this one, password logins switched back on "for a minute", is found in seconds. Better still, the real source of truth is a repository of Ansible roles or images (Ansible), and the server's /etc is only the output.
Access
- Keep a second session open while changing SSH, sudo, PAM, firewalls or networking, and test a new login before closing it (Hardening the SSH Server).
- Know your out-of-band path (cloud serial console, IPMI, hypervisor console) and test it once, before the day you need it.
- Personal accounts, then sudo. Shared
adminaccounts make the audit log useless. Remove people's access the day they leave; SSH certificates with short lifetimes make that automatic (SSH Certificates). - Automation gets its own narrow keys and sudo rules, never a human's.
Services
- Every long-running process is a systemd unit: no
nohup, noscreensessions that die with a reboot, no@rebootcron lines. - Give each unit
Restart=on-failure, aUser=(orDynamicUser=), a memory limit and the basic sandbox (Sandboxing a Service). - Alert on
systemctl --failedand onNRestartsgrowing: a service that restarts every ten minutes looks "active" whenever you glance at it (systemctl). - After package updates, restart what
needrestartlists, and reboot for kernels on a schedule (Patching).
Disks and data
- Separate filesystems (or LVs) for data and for
/var/log, andnofailon every non-root mount (Disks, Mounts & /etc/fstab). - Alert on the rate of disk growth, not only on a percentage; truncate live logs instead of deleting them (When the Disk Is Full).
- A backup that has never been restored is a hope. Run restore drills on a schedule, onto another machine, and time them (Backup Strategy).
- Snapshots and replicas are not backups; off-site and immutable copies are.
Network and TLS
- When something "can't connect", read the exact error first: refused, timed out, unreachable and name-not-known are four different problems (ping, traceroute & mtr).
ss -tlnpbefore blaming the firewall: half the time the service listens on127.0.0.1(Listening Sockets).- Capture at both ends to stop arguing about whose fault it is (tcpdump Recipes).
- Serve the full chain, renew 30 days early, reload after renewal, and monitor the certificate that is actually served (Renewal & Expiry Monitoring).
- Lower DNS TTLs a day before migrations.
Changes and incidents
- Change one thing at a time, and write down what you changed, when, and why (a ticket, a chat channel, the Git log).
- Before you "fix" a production server by hand, capture the evidence:
journalctl -b,ss -tanp,top -b -n 1,df -h,dmesg -T. A restart destroys most of it. - Every manual fix becomes code (an Ansible task, an image change, a unit drop-in in Git) the same week, or it will be lost at the next rebuild.
Test yourself
What does the apply_nginx wrapper do when nginx -t fails?
- Reloads anyway
- Restores the previous config file and leaves the running server untouched
- Stops nginx
- Deletes the backup
Why track /etc in Git (e.g. with etckeeper)?
- To make /etc faster
- So you can see exactly what changed, when, and roll it back
- Git replaces backups
- Package managers require it
A service is fixed by hand at 2 a.m. What should happen next?
- Nothing, it works
- Capture what was changed and turn it into code (config management, image, unit drop-in) so the next rebuild keeps the fix
- Reboot to be sure
- Disable monitoring for that service