Linux Server Hardening (Ubuntu 24.04)
Harden a single Ubuntu 24.04 LTS VPS with SSH key-only auth, no root login, least-privilege users, unattended-upgrades, fail2ban vs rate limiting, and auditing.
TL;DR
On a single Ubuntu 24.04 VPS: create a least-privilege sudo user, enforce SSH key-only auth and PermitRootLogin no in /etc/ssh/sshd_config.d/, enable unattended-upgrades for security patches, and pair kernel rate limiting with basic port/log auditing.
Why it exists
A fresh VPS with a public IPv4 address receives automated SSH brute-force probes within minutes of booting. Default root logins, forgotten password auth, and unpatched OpenSSH or kernel packages turn a cheap hobby server into an easy target.
Mental model
Shrink the attack surface in layers: remove password guessing at sshd, remove direct root access, keep packages patched automatically, and verify what is listening on public interfaces.
Do it
1. Create a least-privilege admin user
Run these on the VPS as root first, and copy your SSH public key before disabling root login:
# Create non-root admin user and grant sudo (not run)
sudo adduser deploy
sudo usermod -aG sudo deploy
# Copy authorized_keys from root to deploy (not run)
sudo rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy/For application processes (e.g., Node/Go daemons), create a dedicated system user with no login shell (sudo useradd --system --shell /usr/sbin/nologin appuser).
2. Enforce SSH keys only and disable root login
Create a drop-in file in /etc/ssh/sshd_config.d/00-hardening.conf (Ubuntu 24.04 evaluates sshd_config.d/*.conf in lexical order, first match wins for most directives):
# /etc/ssh/sshd_config.d/00-hardening.conf (not run)
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
MaxAuthTries 3
LoginGraceTime 30Validate syntax and reload while keeping your existing session open:
# Test config before reloading; verify login in a SECOND terminal (not run)
sudo sshd -t && sudo systemctl reload ssh3. Enable unattended security upgrades
# Install and enable Ubuntu security auto-updates (not run)
sudo apt update && sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
# Preview what unattended-upgrades would install (not run)
sudo unattended-upgrades --dry-run --debugIn /etc/apt/apt.conf.d/50unattended-upgrades, optionally set Unattended-Upgrade::Automatic-Reboot "true"; with Unattended-Upgrade::Automatic-Reboot-Time "03:30"; for kernel patches.
4. fail2ban vs rate limiting & basic auditing
- Rate limiting (
ufw limit 22/tcpornftables): Caps new SSH connections per source IP in the kernel with near-zero memory overhead. fail2ban: Tails/var/log/auth.log/journaldand bans offending IPs after repeated failures. Useful for reducing log noise or protecting HTTP endpoints, though key-only SSH already blocks password guessing.- Routine auditing:
# Check listening sockets, recent logins, and SSH auth logs (not run) sudo ss -tulpn last -n 20 sudo journalctl -u ssh -n 50 --no-pager
When you DON'T need this
- Serverless / static platforms: Hosting this static site on Cloudflare Workers means there is no Ubuntu OS, SSH daemon, or kernel for you to patch.
- Immutable / ephemeral cloud images: If your fleet rebuilds golden AMIs daily via CI/CD and disables SSH completely (using AWS SSM or Tailscale), skip
unattended-upgradesand SSH tuning on live instances. - Cost / complexity trade-off: Managing your own VPS gives predictable flat pricing, at the cost of owning OS patching, SSH key rotation, and log auditing.
Gotchas
- Cloud-init overriding
PasswordAuthentication: Ubuntu cloud images sometimes include/etc/ssh/sshd_config.d/50-cloud-init.confsettingPasswordAuthentication yes. Naming your file00-hardening.confensures it takes precedence—verify withsudo sshd -T | grep -E 'permitrootlogin|passwordauthentication'. - Locking yourself out: Never restart
sshwithout runningsudo sshd -tfirst, and always test a new session before closing the current one. - Auto-rebooting stateful DBs mid-day: Always set
Automatic-Reboot-Timeto a quiet maintenance window.
Production checklist
-
sshd -Tconfirmspermitrootlogin noandpasswordauthentication no. - Admin access uses a named non-root
sudouser; app daemons run as unprivileged system users. -
unattended-upgradesis active and tested with--dry-run. -
ss -tulpnshows only expected public ports (e.g.,22,80,443).
Check yourself
Check Yourself
1. You set PasswordAuthentication no in /etc/ssh/sshd_config, but password logins still work on an Ubuntu 24.04 cloud VPS. What is the most likely cause?
2. Why should you keep your current SSH terminal open when testing a new sshd_config change?
Sources
Linux Processes, Signals, and Exit Codes
How PID 1, SIGTERM, graceful shutdown, SIGKILL, exit codes 137 and 143, exec in entrypoints, and tini/--init keep container shutdowns clean.
Firewall Basics (UFW, nftables & Security Groups)
How stateful firewalls work, default-deny inbound rules, ufw vs nftables, cloud security groups, and why database ports must never be public.