Home UbuntuHarden SSH on Ubuntu 24.04: Complete Security Guide

Harden SSH on Ubuntu 24.04: Complete Security Guide

by Knowledge Base Linux
Harden SSH Ubuntu 24.04 server security with key authentication

Learning how to harden SSH Ubuntu 24.04 servers is one of the highest-value security tasks you can do on a new VPS, because SSH is usually the only door into the server and it is also the door attackers knock on first. Within minutes of a fresh server appearing on the internet, automated scanners start guessing passwords against port 22, and a weak password or an unchanged default configuration can turn a quiet server into a compromised one. This guide walks through a practical, tested hardening sequence: key-only authentication, a non-root sudo user, a hardened sshd configuration, a firewall that allows only what you need, and Fail2ban to slow down brute-force attempts. By the end, your server will still be easy for you to reach and much harder for anyone else to enter.

It is worth being clear about the threat model before changing anything. Most SSH attacks are not targeted break-ins by a skilled intruder; they are bulk, automated password guessing and credential-stuffing campaigns that try thousands of common passwords against every reachable host. That means the defences that matter most are the boring ones: disable password login entirely, require a strong SSH key, keep the software updated, and limit who can even reach the port. Exotic settings help only after those basics are in place. If you are setting up the rest of the server at the same time, our guide on how to protect SSH with Fail2ban on Ubuntu 24.04 covers the intrusion-prevention side in more depth.

Harden SSH Ubuntu 24.04 sshd configuration and firewall protection

Prerequisites

You need an Ubuntu 24.04 server with root or sudo access over SSH, and a local machine from which you will generate an SSH key. Do not start by disabling password authentication until you have confirmed that key login works, or you can lock yourself out. Keep your current SSH session open in one terminal window while you test changes in a second window, so a mistake does not cut off your only connection. You should also know the public IP address or network you connect from if you plan to restrict SSH access by source address later. No extra software is required to begin: OpenSSH server ships with Ubuntu, and Fail2ban and UFW are available from the standard repositories.

Harden SSH Ubuntu 24.04: Switch to Key-Only Authentication

Start on your local machine by generating a modern Ed25519 key pair. Ed25519 keys are short, fast, and secure, and they are supported by current OpenSSH versions on both sides. If you already have a key pair you use elsewhere, you can reuse it, but a dedicated key per server or per purpose makes revocation easier when a laptop is lost or a team member leaves.

ssh-keygen -t ed25519 -C "admin@workstation"
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@SERVER_IP

Replace user with your non-root username and SERVER_IP with the server address. The ssh-copy-id command appends your public key to ~/.ssh/authorized_keys on the server and fixes the permissions for you. Test the key by opening a new SSH session and confirming that you are not asked for a password. If you set a passphrase on the key, you will be asked for that passphrase instead, which is a different and much safer prompt: the passphrase protects the private key on your own machine, while the server never sees a password at all. Once key login works, create a dedicated sudo user if you do not already have one, and make sure that user can log in with the key before you touch the root account.

sudo adduser deploy
sudo usermod -aG sudo deploy
sudo mkdir -p /home/deploy/.ssh
sudo cp ~/.ssh/authorized_keys /home/deploy/.ssh/
sudo chown -R deploy:deploy /home/deploy/.ssh
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys

The official OpenSSH documentation on key-based authentication and sshd configuration is the authoritative reference for every option discussed in this guide, and it is worth bookmarking for the day a setting behaves unexpectedly.

Harden SSH Ubuntu 24.04: Lock Down sshd_config

The main SSH server configuration file is /etc/ssh/sshd_config. Ubuntu 24.04 also reads drop-in files from /etc/ssh/sshd_config.d/, so check that directory first: a setting in a later drop-in file can override the main file, and editing the wrong file is a classic source of confusion. Back up the current configuration before changing it, then apply the core hardening settings.

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
sudo nano /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
ChallengeResponseAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
X11Forwarding no
MaxAuthTries 3
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers deploy

Each setting has a purpose. PermitRootLogin no removes the most valuable target account from remote login entirely; you administer the server as a normal user and escalate with sudo. PasswordAuthentication no closes the password-guessing attack surface that bots exploit all day. MaxAuthTries 3 and LoginGraceTime 30 limit how long and how often an unauthenticated connection can linger. ClientAliveInterval drops idle sessions so a forgotten terminal does not stay authenticated forever. AllowUsers is a whitelist: only the named accounts can log in at all, which means a stray account created by another package cannot silently become an SSH entry point. After editing, validate the configuration before restarting, because a syntax error can prevent the daemon from starting.

sudo sshd -t
sudo systemctl restart ssh
sudo systemctl status ssh

Test from a second terminal window before closing your original session. Confirm that key login works, that password login is refused, and that root login is refused. If anything fails, your original session is still open and you can revert to the backup file in seconds.

Firewall, Fail2ban, and Changing the Port

A firewall is the next layer. UFW on Ubuntu makes the common case simple: deny everything by default, allow SSH, and allow the web ports your server actually serves. Order matters here: enable SSH access in the firewall before you enable the firewall itself, or you will lock yourself out just as surely as a bad sshd_config would.

sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

Next, install Fail2ban, which watches authentication logs and temporarily bans IP addresses that fail too often. The default SSH jail on Ubuntu is a sensible starting point: it bans after five failures within ten minutes, for ten minutes. That is enough to make bulk guessing painfully slow without banning a legitimate user who mistypes a passphrase twice. For the full setup, including custom jails and testing, see our UFW firewall guide for Ubuntu and the Fail2ban guide linked above.

sudo apt update
sudo apt install fail2ban -y
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

Should you change the SSH port from 22 to something obscure? It dramatically reduces log noise, because most scanners only probe the default port, but it is not real security: a targeted attacker will port-scan and find the new port in seconds. Treat a port change as log hygiene, not protection, and never let it replace key-only authentication. If you do change the port, update the Port directive in sshd_config, update UFW before restarting SSH, and update your client command with the -p flag. Keep the change in your server documentation, or the next person to administer the box will waste an hour wondering why port 22 is closed.

Troubleshooting

The most common failure after hardening is locking yourself out, and the fix is almost always preparation rather than rescue: keep an open session, test in a second window, and keep the backup configuration file. If you are locked out of a cloud VPS, use the provider console access, which bypasses SSH entirely, to restore the backup and re-enable password authentication temporarily while you fix the key. Permission problems are the second most common issue: SSH refuses keys when ~/.ssh is group-writable or authorized_keys is world-readable, so check for 700 on the directory and 600 on the file, and check ownership if you copied files as root. A third failure is silent: you edited sshd_config but a drop-in file in sshd_config.d overrides your setting. Run sshd -T as root to print the effective configuration and search it for the value you expected to change; that output settles arguments about which file wins.

If Fail2ban bans your own IP after a few failed attempts, unban it explicitly rather than disabling the service: fail2ban-client set sshd unbanip YOUR_IP restores access immediately. If key login works from one machine but not another, compare the public key on the server with the key the client offers using ssh -v, which prints the offered key fingerprint in its debug output. And if the daemon refuses to start after an edit, sshd -t prints the offending line number; fix that line, do not reinstall the package in frustration, because reinstalling will not restore your custom configuration and may make the problem harder to diagnose.

Conclusion

To harden SSH Ubuntu 24.04 servers well, you need only a short list of habits done in the right order: generate a strong key and confirm it works, create a sudo user, disable root and password login, validate and restart sshd, firewall everything except what you serve, and let Fail2ban slow down the remaining guessing. Revisit the settings whenever you add a user or a service, rotate keys when a device is lost, and keep the system updated so the hardened configuration sits on top of patched software. SSH hardening is not a one-time ritual; it is the baseline every other server task assumes, and once it is in place, the rest of your server work gets quieter and safer.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.