How to Harden SSH on Ubuntu 24.04: Key-Only Login and Fail2ban
To harden SSH on Ubuntu 24.04, switch to key-only login, tighten the SSH server settings, add a firewall rule that allows only SSH, and let Fail2ban block repeated failed logins. That combination removes the two easiest ways into a server: a guessed password and an endless stream of login attempts. Password guessing bots scan the internet all day, and a default SSH setup gives them unlimited tries.
Do the steps in order. Set up your key and confirm it works first, then turn off password login. If you disable passwords before your key works, you lock yourself out. Keep your current SSH session open while you test each change in a new window.
How to Harden SSH on Ubuntu 24.04, Step by Step
The work breaks down into five parts: create a key, copy it to the server, disable password login, limit what the SSH server accepts, and add Fail2ban, with a firewall in front of all of it.
| Step | What it does | What it stops |
|---|---|---|
| SSH key | Replaces a typed password with a private key file | Password guessing |
| Key-only login | Refuses password and keyboard-interactive logins | Brute-force login attempts |
| sshd_config limits | Disables root login and unused login methods | Direct root attacks |
| UFW firewall | Allows SSH and blocks other inbound ports | Scans of other services |
| Fail2ban | Bans IPs after repeated failed logins | Sustained brute-force attacks |
Budget about an hour, including testing.
Create an SSH Key and Copy It to the Server
Start on your own computer, not on the server. Generate an Ed25519 key, the current default for new keys because it is short, fast, and widely supported. A separate key per server makes it easier to revoke one later without touching the others.
ssh-keygen -t ed25519 -C "[email protected]"
Accept the default file location and set a passphrase on the private key. A passphrase means a stolen key file alone is not enough to log in. You will type it each time you use the key unless you load it into an SSH agent with ssh-add.
Next, copy the public key to the server. The ssh-copy-id command appends it to ~/.ssh/authorized_keys for your user and sets the right permissions.
ssh-copy-id your-user@your-server-ip
Log in once with the key to confirm it works. If it asks for your key passphrase instead of your account password, the key is in place. If it still asks for the account password, check permissions on the server: ~/.ssh should be 700 and ~/.ssh/authorized_keys should be 600. For more detail, see our guide on how to set up SSH keys on a new VPS.
Turn Off Password Login in sshd_config
Once ssh key-only login works, disable password authentication so a guessed password can no longer open the door. This is the single change that does the most for Ubuntu 24.04 SSH security, because it takes password guessing off the table entirely. Open the SSH server configuration on the server.
sudo nano /etc/ssh/sshd_config
Find or add these lines. If a line already exists with a different value, change it rather than adding a second copy.
PasswordAuthentication no
ChallengeResponseAuthentication no
UsePAM no
Setting UsePAM no also turns off PAM-based password prompts. If you use PAM-based two-factor authentication, leave UsePAM and ChallengeResponseAuthentication set to yes and only disable plain password authentication. Our guide to how to disable SSH password authentication and use key-only login covers the PAM case in more detail.
Check the configuration for syntax errors, then reload the SSH service. On Ubuntu 24.04 the service is named ssh.
sudo sshd -t
sudo systemctl reload ssh
Now open a new terminal window and log in with your key. It should work. Then try forcing password login with ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no your-user@your-server-ip. The server should refuse it. Do not close your original session until both tests pass.
Lock Down sshd_config Further
Key-only login is the core change, but a few more settings in the same file shrink the attack surface further.
PermitRootLogin no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
AllowUsers your-user
Disabling root login means every login goes through a normal user account with sudo. MaxAuthTries 3 limits each connection to three authentication attempts, and LoginGraceTime 30 drops connections that sit unauthenticated for more than 30 seconds. AllowUsers is a whitelist: only the named users can log in over SSH. Replace your-user with your actual username.
After editing, validate and reload again.
sudo sshd -t
sudo systemctl reload ssh
Add UFW as a First Filter
A firewall does not replace key-only login, but it keeps other ports closed. Ubuntu ships with UFW, a simpler front end for the firewall. Allow SSH first, before you turn the firewall on, or you will lock yourself out.
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose
The OpenSSH profile opens port 22. Then add rules for the services you run, such as ports 80 and 443 for a web server, and leave everything else closed. Check the status output: it should list SSH as allowed and show the firewall as active. Our guide to setting up the UFW firewall on Ubuntu Server 24.04 covers the common service profiles and how to limit SSH to specific source IPs.
Install and Tune Fail2ban for SSH
With passwords disabled, a brute-force attack cannot succeed, but it can still fill your logs and waste server resources. A fail2ban ssh jail watches the SSH log for failed logins and bans the source IP at the firewall for a set time.
Install Fail2ban and start it.
sudo apt update
sudo apt install fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban
Fail2ban ships with SSH protection enabled by default on Ubuntu, but check the defaults rather than trusting them blindly. Create a local override file instead of editing the shipped configuration, so package updates do not overwrite your settings.
sudo nano /etc/fail2ban/jail.local
Add a jail for SSH. Five failed attempts within ten minutes earns a one-hour ban, which stops automated guessing without punishing a real user who mistypes a passphrase twice.
[sshd]
enabled = true
port = ssh
maxretry = 5
findtime = 600
bantime = 3600
If you changed the SSH port, set port to that number. Restart Fail2ban to apply the file, then check that the SSH jail is running.
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
The status output should show the jail as active with a ban list, which starts empty. To test it safely, make five failed login attempts from a test IP you control, then check the status again: the IP should appear in the banned list. Unban it with sudo fail2ban-client set sshd unbanip 203.0.113.10, using the test IP. Never run this test from your own main IP unless you have console access to recover. For a deeper setup, see our Fail2ban setup guide for protecting SSH against brute-force attacks.
Fail2ban can also ban a trusted IP that fails logins repeatedly, for example a monitoring script with an old key. Add trusted IPs to the ignoreip line in jail.local.
Test, Monitor, and Recover If You Get Locked Out
To harden SSH on Ubuntu 24.04 properly, test the failure cases, not just the happy path. From a second terminal while your first session stays open, check that key login works, password login is refused, root login is refused, UFW shows SSH allowed, and Fail2ban shows the SSH jail active. Then close the test window and log in once more with the key.
After that, watch the logs for a few days. Failed password attempts should drop to near zero once password login is off, and the Fail2ban log at /var/log/fail2ban.log shows which IPs are being banned and why.
Keep a recovery path ready before you need it. Store a backup copy of your private key somewhere safe, separate from the server, and make sure you know how to reach your provider console, which works even when SSH is blocked. If you do get locked out, the console lets you fix sshd_config or unban your IP directly.