Hardening a fresh Ubuntu server: SSH keys, firewall, fail2ban and safe updates

A new server with a public address is scanned within minutes. These steps take about 20 minutes and remove most of the risk. They are written for Ubuntu 22.04 and 24.04.

1. Create your own user

adduser admin
usermod -aG sudo admin

2. Log in with a key, not a password

On your own computer:

ssh-keygen -t ed25519
ssh-copy-id admin@SERVER_ADDRESS

Open a new terminal and confirm that ssh admin@SERVER_ADDRESS works without asking for the account password. Keep the old session open until the end of step 3.

3. Turn off password logins

Create /etc/ssh/sshd_config.d/10-hardening.conf:

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password

Test the configuration before you apply it:

sudo sshd -t && sudo systemctl reload ssh

Now open one more new session to make sure you can still get in. Only then close the old one. Most lockouts happen because this check was skipped.

4. Firewall

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

Open only the ports you really serve. Databases, admin panels and caches should listen on 127.0.0.1 or on a private network, never on the public address.

Check what is actually listening:

sudo ss -tlnp

Anything bound to 0.0.0.0 or [::] that you do not recognise deserves a closer look.

5. fail2ban

sudo apt install fail2ban

Create /etc/fail2ban/jail.local:

[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

6. Updates without surprise reboots

Security updates should install by themselves, but a streaming server should not reboot by itself in the middle of the evening.

sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

In /etc/apt/apt.conf.d/50unattended-upgrades make sure this line is set:

Unattended-Upgrade::Automatic-Reboot "false";

Then reboot for kernel updates yourself, in a quiet hour. The file /var/run/reboot-required exists when a reboot is pending.

7. Backups you have tested

A backup that was never restored is a hope, not a backup. Keep at least one copy on a different machine, and restore it once to a test server so you know the steps and the time it takes.

Checklist

  • Password login over SSH is off, and you confirmed key login in a fresh session.
  • ufw status shows only the ports you serve.
  • ss -tlnp shows no database or admin service on the public address.
  • fail2ban is running.
  • Automatic reboot is off, and you have a reminder to reboot after kernel updates.
  • One off-server backup exists and was restored once.