Learning to harden ssh ubuntu 24.04 is the highest-value security work you can do on a new VPS, because SSH is the door every admin uses and every bot knocks on. A default install accepts password logins, allows root login on some images, and listens on the standard port, which invites constant brute-force attempts. Ten config changes below close most of that exposure without locking you out, provided you test each change in a second session before you close the first.
The golden rule runs through this whole guide: never close your working SSH session until a new session logs in under the new rules. Keep one connected terminal open while you edit, and open a second terminal to test. If the test fails, the first session still lets you fix the config. That habit prevents the most common self-lockout in server work.
Before you start, set up key-based login if you have not already. Our guide to set up SSH keys on a new VPS covers key generation and copying the public key to the server. Key-only login is change number one below, and it only works once your key is in place and tested.
Harden SSH Ubuntu 24.04: The 10 Changes
1. Switch to Key-Only Login
Password authentication lets bots guess forever. Key authentication requires a private key file that bots cannot guess, which ends most brute-force noise at once. In the SSH daemon config file (sshd_config), set password authentication off and public key authentication on, then restart the SSH service. Test a new login with your key before you rely on it. Our guide to disable password login and use SSH keys only covers the full switch, including how to recover if a key goes missing.
2. Disable Root Login
Root login gives an attacker a known username with full power. Turn root login off in the daemon config so attackers must guess both a username and a key. Admin work continues through a normal user account with sudo, which also leaves a clearer audit trail of who ran what. Keep one sudo user with a working key before you make this change.
3. Change the Default Port
Moving SSH off port 22 cuts automated scan noise sharply, since most bots only knock on the standard port. It is not real security on its own, since a port scan finds the new port, but it reduces log clutter and failed-login load. Update the firewall rule for the new port before you restart SSH, or you lock yourself out at the firewall layer. Our guide to change the default SSH port safely covers the firewall-first order that prevents that mistake.
4. Limit Which Users Can Log In
Use the allow-users setting to name the one or two accounts permitted to log in over SSH. Every other account then fails before authentication starts, which shrinks the attack surface to accounts you actually use. This change pairs well with disabling root login, since it states the allowed list in plain terms rather than relying on defaults.
5. Turn Off Empty Passwords and Challenge-Response
Empty passwords should never exist on a server, but the setting that forbids them costs nothing to confirm. Challenge-response authentication, used by some older login methods, should also go off once key login works, since it can re-open a password path you thought you closed. Check both settings in the same config edit as key-only login so no second path survives.
6. Limit Authentication Attempts
Set the maximum authentication attempts low, such as three, so a connection that guesses repeatedly drops fast. This change slows brute-force scripts and pairs well with Fail2ban, which watches the logs and bans repeat offenders. It also protects against accidental lockout loops from a misconfigured client, since the client fails fast and visibly instead of retrying silently for minutes.
7. Shorten Login Grace Time
Login grace time is how long an unauthenticated connection may stay open. A long window lets slow guessing scripts hold connections open. Cutting it to 30 seconds or less frees server resources and drops idle connections sooner. Legitimate logins finish in seconds, so this change has no effect on normal use.
8. Disable Agent and TCP Forwarding You Do Not Use
SSH forwarding features are useful, but each open feature is a path an attacker can abuse after gaining a foothold. Turn off agent forwarding and TCP forwarding unless a specific task needs them, and turn them back off when that task ends. Most WordPress and web server work needs neither, so the default for a web VPS should be off.
9. Add Fail2ban for SSH
Fail2ban reads the authentication log and bans IP addresses that fail too often. It turns log noise into automatic blocking, which keeps brute-force bots from hammering the server all day. Install it, confirm the SSH jail is active, and test with a few failed logins from a second address you control. Our Fail2ban setup guide for SSH brute-force protection covers jail settings and ban times in detail.
10. Keep SSH Updated and Reviewed
Security patches for the SSH package arrive through normal system updates, so automatic security updates matter as much as config. Review the daemon config after major system upgrades, since package updates can add new defaults or warn about old settings. A twice-yearly review takes minutes: confirm key-only login, root login off, allowed users, and Fail2ban status, then test a fresh login. Our first 10 things to do after buying a VPS checklist includes this review alongside firewall and user setup, which makes it a good companion for a new server.
Common Mistakes to Avoid
The most common mistake is editing the config and restarting SSH from the only open session without testing. The second is changing the port but forgetting the firewall, which blocks the new port. The third is disabling password login before the key works, which ends admin access entirely. All three share one fix: test in a second session, change one thing at a time, and keep a backup copy of the working config before each edit. If your provider offers a web console, confirm you can reach it before you start, since it is your last resort if SSH breaks.
The Hardened Baseline
After these ten changes, your VPS accepts only named users with keys, on a non-standard port, with failed attempts capped and banned automatically. Use this ssh hardening checklist as a pre-flight pass before every server change, and keep it with your server docs. Overall ubuntu server security improves most when these config changes sit alongside a firewall, updates, and key-only login. Bots still scan, but they find no password path and no root path, and repeated tries earn a ban. That baseline covers the SSH layer. The firewall, updates, and WordPress hardening layers still need their own work, but SSH no longer sits open as the weakest door.