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:

Bash
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:

Bash
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 3 stops each connection after three failed attempts.
  • AllowGroups ssh-users means 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:

Bash
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:

Bash
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

  1. Make an ed25519 key with a passphrase and confirm you can log in with it.
  2. Add 10-hardening.conf, after creating the ssh-users group and joining it.
  3. Run sudo sshd -T and confirm passwords and root login really are off.
  4. Reload, test from a new terminal, then close the old one.

The bots will keep knocking. Let them. Nobody's answering the door with a password any more.