SSH Hardening Checklist for Ubuntu 26.04 LTS and 24.04 LTS
A practical SSH hardening checklist for Ubuntu 26.04 LTS and 24.04 LTS: keys, no passwords, AllowUsers, a config drop-in, rate limiting and safe testing.
This checklist takes a fresh Ubuntu server from "root with a password" to key-only SSH logins for one named sudo user, with connection rate limiting and a configuration you can test before it can lock you out. It is written for Ubuntu 26.04 LTS and notes where Ubuntu 24.04 LTS differs.
Older tutorials on this topic targeted Ubuntu 14.04, which has been out of standard support for years. The ideas are the same, but the file layout and several option names have changed since then.
Requirements
- A server running Ubuntu 26.04 LTS or 24.04 LTS with the
openssh-serverpackage installed. - Root or sudo access over SSH. The examples use the server address
203.0.113.10and a new user calleddeploy. - Access to your provider's web console as a way back in if SSH stops working.
Step 1: Create a sudo user
Skip this step if you already log in as a non-root user with sudo rights. Otherwise, as root:
adduser deploy
usermod -aG sudo deploySet a strong password when prompted. It will no longer be accepted for SSH logins after step 4, but sudo still asks for it.
Step 2: Install an SSH key for that user
Run these commands on your own computer, not on the server. Create a key pair if you do not have one, and protect it with a passphrase:
ssh-keygen -t ed25519Copy the public key to the server:
ssh-copy-id deploy@203.0.113.10Then confirm that key login works and that sudo works:
ssh deploy@203.0.113.10
sudo -vDo not continue until this login succeeds without asking for the account password. Everything after this point assumes the key works.
Step 3: Understand where the settings go
Ubuntu's /etc/ssh/sshd_config starts with this line:
Include /etc/ssh/sshd_config.d/*.confPut your changes in a drop-in file in that directory and leave the main file as the package shipped it. Two rules decide which value wins:
- For each keyword,
sshduses the first value it reads. Because theIncludeis at the top, drop-ins override the main file. - Drop-in files are read in lexical order, so a file with a lower number beats one with a higher number.
Check what is already there, because cloud images often ship a drop-in that sets PasswordAuthentication:
ls -l /etc/ssh/sshd_config.d/
sudo grep -ri passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/Step 4: Write the hardening drop-in
Create a file that sorts before any existing one:
sudo nano /etc/ssh/sshd_config.d/10-hardening.confAdd:
# Keys only
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
# Only these accounts may log in over SSH
AllowUsers deploy
# Fewer guesses, shorter window for unauthenticated connections
MaxAuthTries 3
LoginGraceTime 30
# Features most servers do not need
X11Forwarding no
AllowAgentForwarding noWhat each line does:
PasswordAuthentication noandKbdInteractiveAuthentication noturn off both ways of typing a password. Ubuntu enables PAM for SSH, and PAM can serve either method, so disable both.KbdInteractiveAuthenticationis the current name for the oldChallengeResponseAuthenticationoption.AllowUsersrestricts SSH to the listed accounts, separated by spaces. Every other account is refused, including root, even with a valid key. UseAllowGroupsinstead if you prefer to manage access through a group.MaxAuthTries(default 6) limits authentication attempts per connection. Do not go below 3: an SSH client offers every key it holds, and each offer counts as an attempt.LoginGraceTime(default 120 seconds) is how long an unauthenticated connection may stay open.X11Forwardingis off in upstream OpenSSH but switched on in Ubuntu's default file. Agent forwarding is on by default; turn it off unless you use it.
Two related settings have their own guides on this site, so only briefly: add PermitRootLogin no to the same file to refuse root logins outright (the default, prohibit-password, still allows root with a key), and if you move SSH to another port with Port, treat it as a way to cut log noise, not as protection.
Step 5: Test and apply without locking yourself out
Keep your current SSH session open for this whole step. An existing session survives a restart of the SSH service, so it is your safety line.
Check the syntax. No output means no errors:
sudo sshd -tPrint the configuration sshd will actually use, after all includes are resolved, and confirm your values won:
sudo sshd -T | grep -Ei 'passwordauthentication|kbdinteractiveauthentication|allowusers|maxauthtries|permitrootlogin'If passwordauthentication still shows yes, another drop-in is read before yours. Give your file a lower number or edit the other file.
Apply the change:
sudo systemctl restart sshNow open a new terminal on your computer and log in again:
ssh deploy@203.0.113.10If that works, check that passwords are really refused:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@203.0.113.10The expected result is Permission denied (publickey). Only close the original session after both tests behave as described. If the new login fails, fix the drop-in from the session you kept open and restart the service again.
Step 6: Rate-limit connection attempts
With passwords disabled, brute-force attempts can no longer succeed, but they still fill the log and use resources. You have three layers available.
Built into sshd
MaxStartups limits concurrent unauthenticated connections overall. Its default of 10:30:100 starts dropping new connections at random once 10 are pending and refuses all of them at 100. PerSourceMaxStartups limits them per source address and is unset by default. To allow at most three unauthenticated connections per address, add this to your drop-in:
PerSourceMaxStartups 3Ubuntu 26.04 ships OpenSSH 10, which also has PerSourcePenalties, enabled by default: an address that repeatedly fails authentication or drops connections before authenticating is refused for a growing period. You do not need to configure anything. Ubuntu 24.04 ships OpenSSH 9.6, which does not have this option, so use one of the next two layers there.
Firewall rate limit with UFW
UFW's limit rule allows SSH but denies an address that opens six or more connections within 30 seconds:
sudo ufw limit ssh/tcp
sudo ufw enable
sudo ufw status verboseAdd the rule before enabling the firewall. If SSH listens on a port other than 22, use that port instead, for example sudo ufw limit 2222/tcp.
Log-based banning
Fail2ban reads the authentication log and blocks addresses with repeated failures for a set time. It is the usual choice on 24.04 and when you want bans that last longer than the firewall's 30-second window.
Maintenance and troubleshooting
- Follow the log while testing:
sudo journalctl -fu ssh.service. Refused users and failed keys are reported there with the reason. - Adding a user later: install their public key in
~/.ssh/authorized_keysfor their account, add the name toAllowUsers, then runsudo sshd -tand restart the service. - Key is ignored:
sshdrefuses keys when the home directory,~/.sshorauthorized_keysis writable by anyone but the owner. Runchmod 700 ~/.sshandchmod 600 ~/.ssh/authorized_keysas that user. - Changed
PortorListenAddressand nothing happened: on Ubuntu 24.04 the listening socket is managed by systemd and generated fromsshd_config. Runsudo systemctl daemon-reloadandsudo systemctl restart ssh.socket, then check withss -tlnp. - Keep OpenSSH patched:
sudo apt update && sudo apt upgradepicks up security fixes for theopenssh-serverpackage.
The full list of options is in the sshd_config manual, and Ubuntu's own notes are in the Ubuntu Server OpenSSH guide.
Frequently asked questions
I locked myself out. How do I get back in?
Log in through your provider's web console, which does not use SSH. Move your drop-in out of the way with sudo mv /etc/ssh/sshd_config.d/10-hardening.conf /root/, restart the service with sudo systemctl restart ssh, then fix the file and test it again as in step 5.
Should I add two-factor authentication on top of keys?
It helps when a stolen laptop or key file is a realistic risk. OpenSSH can require a key and a second factor together through AuthenticationMethods, and it supports hardware-backed keys (ssh-keygen -t ed25519-sk) that need a touch on a FIDO2 token for each login. A passphrase on your private key is the minimum.
Is ed25519 better than RSA?
Ed25519 keys are short, fast and what Ubuntu's documentation recommends for new keys. A 4096-bit RSA key is still acceptable if you must connect to or from very old systems. DSA keys no longer work with current OpenSSH releases.
Does AllowUsers also block SFTP and scp?
Yes. Both run over the same SSH login, so an account that is not listed cannot use them either.