Skip to Content
Mengty Learns
Foundations

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.

how-tointermediateTested with ubuntu 24.04 LTS (docs), openssh 9.6p1 (docs)Updated Nov 27, 2026

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.

Key-only sshd drops password probes; a non-root user escalates via sudo only when needed.

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 30

Validate 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 ssh

3. 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 --debug

In /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/tcp or nftables): Caps new SSH connections per source IP in the kernel with near-zero memory overhead.
  • fail2ban: Tails /var/log/auth.log / journald and 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-upgrades and 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.conf setting PasswordAuthentication yes. Naming your file 00-hardening.conf ensures it takes precedence—verify with sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication'.
  • Locking yourself out: Never restart ssh without running sudo sshd -t first, and always test a new session before closing the current one.
  • Auto-rebooting stateful DBs mid-day: Always set Automatic-Reboot-Time to a quiet maintenance window.

Production checklist

  • sshd -T confirms permitrootlogin no and passwordauthentication no.
  • Admin access uses a named non-root sudo user; app daemons run as unprivileged system users.
  • unattended-upgrades is active and tested with --dry-run.
  • ss -tulpn shows 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