SSH Hardening in Fifteen Minutes: Keys Only, No Root, No Surprises
Your SSH port has had more visitors this week than your website, and none of them brought snacks. SSH hardening in fifteen minutes: ed25519 keys, no passwords, no root, and the cloud-init file that quietly undoes it all.
Silas Voss
4 min read
Your server's SSH port has had more visitors this week than your website. None of them called ahead. None of them brought snacks. All of them tried the password admin123.
Run journalctl -u ssh on any server with a public address and you'll meet them, by the thousand, every day. They're mostly bots, mostly harmless, and occasionally lucky. SSH hardening is how you make sure luck never comes into it. It takes fifteen minutes, and the most important step is the one people get wrong.
Step 1: get a proper key
On your own machine, not the server:
ssh-keygen -t ed25519 -C "charles-laptop"
ed25519 keys are short, fast and strong. Give it a passphrase. A key without a passphrase is a password written on a sticky note, except the sticky note is a file and the file is in your backups.
Copy the public half to the server:
ssh-copy-id user@server
Then log in with the key, in a new terminal, before you change anything else. I will repeat this later, because I've seen what happens when people don't.
Step 2: write your rules in a drop-in file
Modern OpenSSH on Debian and Ubuntu reads extra config from /etc/ssh/sshd_config.d/. Use that, and leave the main file alone so package updates don't argue with you. Create /etc/ssh/sshd_config.d/10-hardening.conf:
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
MaxAuthTries 3
AllowGroups ssh-users
What each one buys you:
- No passwords at all. Bots guess passwords. They don't guess private keys.
- No root login. Log in as yourself and use
sudo, so there's a name in the logs and root isn't a free target. MaxAuthTries 3stops each connection after three failed attempts.AllowGroups ssh-usersmeans only people in that group get in at all. Create the group and add yourself first (sudo groupadd ssh-users,sudo usermod -aG ssh-users $USER), or you'll lock yourself out with excellent security.
Step 3: the trap
Here's the part that catches nearly everyone. For most settings, sshd uses the first value it reads and ignores the rest. The main config includes the drop-in folder near the top, and reads the files in alphabetical order.
Cloud servers often ship with a file called 50-cloud-init.conf in that folder, and it may well say:
PasswordAuthentication yes
If your file were called 60-hardening.conf, cloud-init's "yes" would be read first, and it would win. Your hardening would sit there looking official and doing nothing. That's why ours is 10-. Low numbers go first.
Don't trust it. Ask sshd what it actually decided:
sudo sshd -t
sudo sshd -T | grep -Ei 'passwordauthentication|permitrootlogin|kbdinteractive'
-t checks the syntax. -T prints the final, effective settings. If it says passwordauthentication yes, something read earlier is overruling you, so go and find it.
Step 4: reload without locking yourself out
Keep your current SSH session open. That's your lifeline. Then:
sudo systemctl reload ssh # Debian, Ubuntu (it's "sshd" on Fedora and RHEL)
Now, in a new terminal, log in again. Told you I'd repeat it.
- Did the key login work? Good.
- Does a password login get refused? Check with
ssh -o PubkeyAuthentication=no user@server. You want "Permission denied (publickey)".
Only close the old session once both are true. If the new login fails, the old session is still there to fix things. If you closed it already, I hope your provider has a web console. Mine did. That's how I know.
Step 5: keep OpenSSH patched
Configuration is half of it. In July 2024, "regreSSHion" (CVE-2024-6387) showed that OpenSSH itself can have a serious bug: a race in the server's signal handling that could, in the worst case, let someone run code remotely before logging in. Versions 8.5p1 to 9.7p1 were affected, and distributions shipped fixes quickly. Keep openssh-server in your regular updates. (See "unattended-upgrades" on this blog. It exists so you don't have to remember.)
What to do today
The bots will keep knocking. Let them. Nobody's answering the door with a password any more.
Security
More like this
Security
Who Did That? Tracing sudo and Logins on a Linux Server
The config changed overnight and nobody did it. Everyone says so. The logs disagree. How to read sudo logs and login records, record whole sudo sessions, watch key files with auditd, and why the logs shouldn't only live on the box.
Security
What fail2ban Actually Stops (and the Things It Was Never Going To)
fail2ban is the bouncer who throws out anyone who gets their name wrong three times. Useful, and not a security strategy. How to set up its SSH jail, check who it has banned, and know what it can't protect you from.
Security
How to Read a CVE Without Panicking: CVSS, KEV and EPSS Explained
A 9.8 CVE drops on a Friday and your group chat catches fire. Before you cancel the weekend, learn how to read a CVE: what the score measures, what it doesn't, and the two lists that tell you whether anyone is actually attacking it.