Skip to content
Shakil Ahmed.
← Blog

17 Nov 2026 · by Md. Shakil Ahmed

Harden a Fresh Ubuntu Server in 15 Minutes: SSH Keys, UFW and Fail2ban

Spin up a new cloud server, and within minutes your logs will show strangers trying to log in as root, admin and ubuntu. They are not targeting you. Automated bots simply try every IP address on the internet.

The good news: a handful of settings, applied on day one, stops almost all of this. In my book DevOps for Developers I call it the foundation chapter, because everything else (Docker, CI/CD, Kubernetes) runs on top of a server you can trust. Here is the version you can finish in about fifteen minutes.

Three layers between the internet and your server

Before you start: keep your current SSH session open until the very end, and test every change from a second terminal. A typo in the firewall or SSH config can lock you out. Know where your provider's web console is, as it is your way back in.

Step 0: Update and create a normal user

sudo apt update && sudo apt upgrade -y
adduser deploy
usermod -aG sudo deploy

Work as a normal user with sudo, not as root.

Step 1: Log in with an SSH key, not a password

A password can be guessed; a key pair cannot, in any practical sense. The private key stays on your computer, the public key goes on the server.

On your own machine:

ssh-keygen -t ed25519 -C "admin@my-laptop"
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@SERVER_IP

Now confirm that ssh deploy@SERVER_IP logs you in without a password. Only after that works, disable passwords (Step 3).

Good habits: give every person (and every automation, such as CI/CD) their own key, so you can revoke one without touching the rest, and protect the private key with a passphrase.

Step 2: A firewall with a deny-by-default rule

UFW (Uncomplicated Firewall) is the doorman. The rule is simple: block everything coming in, then open only what you need.

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 80/tcp       # HTTP
sudo ufw allow 443/tcp      # HTTPS
sudo ufw allow 22/tcp       # SSH for now; we will change this in Step 3
sudo ufw enable
sudo ufw status verbose

Allow SSH before you enable the firewall, or you will cut off your own session.

Step 3: Harden SSH itself

Open the SSH daemon's configuration:

sudo nano /etc/ssh/sshd_config

Set these values:

Port 2222
PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes

Open the new port in the firewall, then test the config and restart:

sudo ufw allow 2222/tcp
sudo sshd -t && sudo systemctl restart ssh

Ubuntu 24.04 note: newer Ubuntu releases start SSH through socket activation, which means the Port line in sshd_config can be ignored. If the new port does not answer, run sudo systemctl disable --now ssh.socket && sudo systemctl enable --now ssh, then restart again.

From a second terminal, check that the new port works:

ssh -p 2222 deploy@SERVER_IP

When it does, close the old port:

sudo ufw delete allow 22/tcp

A word of honesty: changing the port is noise reduction, not security. It makes your logs quieter because most bots only try port 22. The real protection is keys only and no root login.

Step 4: Fail2ban, the bouncer

The firewall decides which doors are open. Fail2ban watches who keeps knocking on the open one, and bans IP addresses that fail too many times.

sudo apt install fail2ban -y
sudo nano /etc/fail2ban/jail.local
[sshd]
enabled  = true
port     = 2222
maxretry = 5
findtime = 10m
bantime  = 1h
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

The status command shows how many addresses are currently banned. Do not be surprised if you see some within the first hour.

Step 5: Automatic security updates

sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

Most real-world compromises use vulnerabilities that already had a patch. Letting the server install security updates by itself closes that gap.

The 60-second review

Run these to confirm the result:

sudo ufw status verbose                  # deny by default, only 80/443/2222
sudo grep -E "^(Port|PasswordAuthentication|PermitRootLogin)" /etc/ssh/sshd_config
sudo fail2ban-client status sshd         # jail is active

What you have now

  • No password logins, no direct root access
  • A firewall that blocks everything you did not explicitly open
  • Automatic bans for brute-force attempts
  • A server that patches itself

Three layers, and none of them is perfect on its own. Together they make you a much harder target than the thousands of servers that never got past the default setup.

From here you can safely move on to the interesting parts: a CI/CD pipeline, containers, and security gates in your pipeline.

This is one section of the first chapter of DevOps for Developers (Bengali Edition), written in simple Bengali, with every command explained. See the book, or contact me if you want your servers set up and hardened properly.

Need help with a project like this?

Get in touch