Server management · Security · Tutorial

How to harden a fresh Ubuntu 24.04 server in 30 minutes

A step-by-step tutorial for securing a new Ubuntu 24.04 LTS server: a sudo user, SSH keys only, a firewall, automatic security updates and Fail2ban.

To harden a new Ubuntu 24.04 server: create a sudo user, sign in with SSH keys only and disable root and password logins, enable the UFW firewall for just the ports you need, turn on unattended security upgrades, and install Fail2ban. It takes about 30 minutes and blocks the vast majority of automated attacks.

A new server with a public IP address starts receiving login attempts within minutes of booting. Most of these are bots trying common passwords against the root account. Every server I set up for a client gets the same baseline before anything else is installed. Here it is, in order.

1. Update the system

sudo apt update && sudo apt full-upgrade -y
sudo reboot

Reboot if a new kernel was installed. Start from a fully patched machine so you know every later problem is yours, not a known bug.

2. Create a sudo user

Working as root makes every typo a potential disaster and gives attackers a known username. Create your own:

sudo adduser deploy
sudo usermod -aG sudo deploy

3. Use SSH keys, not passwords

On your own computer, generate a key if you don't have one, then copy it to the server:

ssh-keygen -t ed25519 -C "[email protected]"
ssh-copy-id deploy@your-server-ip

Open a new terminal and confirm you can sign in as deploy with the key before going further. Keep your current session open as a safety net.

4. Lock down the SSH daemon

Ubuntu 24.04 reads drop-in files from /etc/ssh/sshd_config.d/, which survive package upgrades better than editing the main file. Create one:

sudo tee /etc/ssh/sshd_config.d/99-hardening.conf > /dev/null <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
AllowUsers deploy
EOF

sudo sshd -t && sudo systemctl restart ssh

sshd -t checks the configuration for errors first. If it prints nothing, the restart is safe. Test a fresh login again before closing your original session.

5. Turn on the firewall

Allow SSH first, or you will lock yourself out:

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

Only open the ports a public service actually needs. Databases, Redis and admin panels should listen on localhost or a private network, never on the public interface.

One catch worth knowing: Docker writes its own iptables rules, so a container port published with -p 5432:5432 can bypass UFW. Bind published ports to localhost (-p 127.0.0.1:5432:5432) unless they genuinely need to be public.

6. Automatic security updates

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

This installs security patches daily. Check what it did in /var/log/unattended-upgrades/. For servers that can reboot at a quiet hour, enable automatic reboots for kernel updates in /etc/apt/apt.conf.d/50unattended-upgrades.

7. Fail2ban for repeated failures

sudo apt install -y fail2ban
sudo tee /etc/fail2ban/jail.local > /dev/null <<'EOF'
[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
EOF
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

With password logins disabled, brute force can't succeed anyway, but Fail2ban cuts the noise in your logs and blocks scanners before they find anything else.

8. The small things that matter later

  • Time: confirm timedatectl shows the clock is synchronised. TLS, logs and scheduled jobs all depend on it.
  • Swap: small cloud servers often ship without any. A 1 to 2 GB swap file prevents a memory spike from killing your database.
  • Monitoring: set up an external uptime check and disk-space alerts on day one, not after the first outage.
  • Backups: a hardened server still fails. Back up off the machine, and test the restore.

FAQ

Should I change the SSH port from 22?

It reduces log noise but adds no real security. Key-only authentication, no root login and Fail2ban do the actual work. If you do change it, update the UFW rule before restarting SSH.

Is UFW enough, or do I need a cloud firewall too?

Use both if your provider offers one. A cloud firewall blocks traffic before it reaches the machine and still protects you if a later change disables UFW by mistake.

How often should I review a hardened server?

Monthly: check pending updates, Fail2ban bans, disk space, users with sudo access and listening ports. On a care plan, this is the routine work done for you.

Keep reading