Guide Guides

How to harden an SSH server: keys instead of passwords, no root, and proof it worked

Any server with port 22 open to the internet gets hammered by bots, often thousands of attempts a day. Yet stock OpenSSH still allows password logins. Here is what to change so password guessing goes nowhere, how to do it without locking yourself out, and how to confirm what the server is actually running.

Kyberkit editorial team · · reading time 5 minutes · For admins and analysts

Guide: SSH server hardening

Key points

  • Stock OpenSSH allows password logins: PasswordAuthentication and KbdInteractiveAuthentication both default to yes. Move to Ed25519 keys protected by a passphrase and switch passwords off.
  • Put your settings in a file under /etc/ssh/sshd_config.d/. For most options OpenSSH uses the first value it finds, so whatever is read first wins.
  • Run sshd -t before restarting, confirm the result with sshd -T, and keep a second session open until you have logged in again.
  • OpenSSH 10.0 removed DSA and made the post-quantum mlkem768x25519-sha256 the default key exchange. The current release is 10.6p2 (5 November 2026).

Why it matters

SSH is the front door to a Linux server. What we see in SOC work: leave SSH exposed to the internet with passwords enabled and your logs fill up with hundreds or thousands of failed logins a day. One account with a weak or reused password is all an attacker needs.

And stock OpenSSH allows passwords. According to the sshd_config manual, both PasswordAuthentication and KbdInteractiveAuthentication default to yes. Distributions may change the shipped config, so always check the effective settings (step 5).

SSH hardening order: create an Ed25519 key with a passphrase, copy it to the server, add your own config file, check with sshd -t, test key login in a new session, only then close the old one1. Create an Ed25519 key with a passphrase; 2. Copy it to the server (ssh-copy-id); 3. Disable passwords and root in sshd_config.d; 4. sshd -t and restart; 5. Log in from a new session, then close the old one1Create an Ed25519 key witha passphrase2Copy it to the server (ssh-copy-id)3Disable passwords and root insshd_config.d4sshd -t and restart5Log in from a new session, then closethe old one
The order that keeps you from locking yourself out

Step 1: Keys instead of passwords

On your own machine, create a key. Ubuntu's documentation recommends Ed25519:

ssh-keygen -t ed25519

Protect it with a passphrase – if someone steals it from your laptop, it's useless without one. Use ssh-agent so you don't have to type it every time. Then copy the public key to the server:

ssh-copy-id user@server

Only the account owner should be able to write to ~/.ssh/authorized_keys (chmod go-w ~/.ssh/authorized_keys).

OpenSSH dropped DSA keys entirely in 10.0. If you still have one, replace it.

Step 2: Your settings go in sshd_config.d

Leave the main /etc/ssh/sshd_config alone. On Ubuntu it starts with Include /etc/ssh/sshd_config.d/*.conf, and for most options OpenSSH uses the first value it finds. A file in sshd_config.d therefore overrides anything further down the main file, and package updates won't overwrite it. Watch out for files that are already there (from your cloud provider, for example) – they're read in alphabetical order, so name yours to load first, such as 00-hardening.conf:

# /etc/ssh/sshd_config.d/00-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AllowGroups ssh-users
X11Forwarding no
AllowTcpForwarding no

What each line does:

  • PasswordAuthentication no and KbdInteractiveAuthentication no turn passwords off. Don't skip the second one, or a password may still get through via keyboard-interactive (PAM).
  • PermitRootLogin no – the default, prohibit-password, still lets root in with a key. We recommend no and working through personal accounts with sudo, so the logs show who did what.
  • AllowGroups (or AllowUsers) – by default every user may log in. With this, only members of the group can; create the group and add yourself first.
  • X11Forwarding no is already the default. AllowTcpForwarding defaults to yes – if you don't need tunnels, turn it off; otherwise an attacker with a stolen key could tunnel through the server into your internal network.

Step 3: Limit where a key works and what it can do

You can put restrictions in front of a key in authorized_keys. The sshd manual describes two useful ones:

  • from="203.0.113.0/24" – the key only works from the listed addresses (CIDR is supported). A stolen key is no use from anywhere else.
  • restrict – disables port, agent and X11 forwarding, PTY allocation and running ~/.ssh/rc. Ideal for backup and automation keys, ideally combined with command="…".
from="203.0.113.0/24",restrict,command="/usr/local/bin/backup" ssh-ed25519 AAAA… backup@nas

The strongest control sits outside SSH, though: allow port 22 in the firewall only from your VPN or admin addresses.

Step 4: Test and restart without locking yourself out

Keep your current session open. Then:

sudo sshd -t
sudo systemctl restart ssh

sshd -t checks the config and keys; a bad directive could otherwise stop sshd from starting. On Ubuntu and Debian the service is called ssh; on other distributions it may be sshd. Log in with your key from a new window. Only once that works should you close the old session.

Step 5: Check what the server is really using

Files can mislead – the effective config is what counts. sshd -T prints all of it:

sudo sshd -T | grep -i -E 'passwordauthentication|kbdinteractive|permitrootlogin|allowgroups|persourcepenalties'

You want to see passwordauthentication no, kbdinteractiveauthentication no and permitrootlogin no. If you use Match blocks, test a specific connection with -C (for example sudo sshd -T -C user=jan,host=pc,addr=203.0.113.5).

From the client side, try to force a password login – the server must refuse it:

ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password user@server

What recent OpenSSH releases changed

  • 10.0 removed support for weak DSA signatures and made the hybrid post-quantum mlkem768x25519-sha256 the default key exchange.
  • 10.1 – the client warns when a connection negotiates a key exchange that isn't post-quantum. You'll typically see this with old servers and network gear.
  • 10.3 – the built-in penalties for repeatedly failing addresses (PerSourcePenalties, on by default) now also cover login attempts with non-existent usernames (5 seconds by default).
  • 10.6 – a new server option, WarnWeakCrypto, logs clients that used a key exchange without post-quantum protection. The current release is 10.6p2, from 5 November 2026.

Distributions backport security fixes to older versions, so a version number alone doesn't tell you whether you're vulnerable. We cover critical bugs in our news – most recently the OpenSSH password bypass on Alpine (CVE-2026-123158).

How to tell if someone got in

  • In the log (journalctl -u ssh), look for Accepted password for – once passwords are off there should be no new ones – and for Accepted publickey for from unfamiliar addresses.
  • Review authorized_keys for every account, including root. A planted key is the classic backdoor.
  • If you find something, follow our guide Compromised server after an exploited vulnerability: the first hour.

Checklist

  1. Ed25519 key with a passphrase, no DSA keys.
  2. PasswordAuthentication no and KbdInteractiveAuthentication no.
  3. PermitRootLogin no, admin work through sudo.
  4. AllowGroups or AllowUsers.
  5. Tunnelling off where not needed; automation keys with restrict and from=.
  6. Port 22 open in the firewall only to your VPN or admin addresses.
  7. sshd -T confirms the result and password login fails.
  8. Regular review of authorized_keys and the logs.

FAQ

Does moving SSH off port 22 help?

It cuts most of the automated noise from your logs, but it doesn't make you more secure – a scanner will find SSH on any port. The real protection is keys instead of passwords and limiting where SSH can be reached from at all (firewall, VPN).

Is root login over SSH disabled by default?

Not entirely. OpenSSH's default for PermitRootLogin is prohibit-password: root can't log in with a password but can with a key. Distributions may ship a modified config. We recommend no, and working through personal accounts with sudo so the logs show who did what.

Do I still need fail2ban?

With passwords disabled, password guessing can't succeed. OpenSSH also penalises addresses that keep failing by default (PerSourcePenalties), and since 10.3 that includes attempts with non-existent usernames. Fail2ban can still help with log noise and other services, but it is no substitute for keys.

How do I know whether anyone logged in with a password?

Search the sshd log for “Accepted password for” lines (on Ubuntu, for example, journalctl -u ssh). Once passwords are off, no new ones should appear. “Accepted publickey for” lines show key logins, including the key fingerprint.

I have an RSA key. Do I need to replace it?

Ubuntu's documentation recommends Ed25519 for new keys, with 4096-bit RSA as the alternative. An existing RSA key of adequate length will keep working. DSA keys, however, are not supported at all since OpenSSH 10.0 – replace those now.

Sources

  1. OpenBSD manual – sshd_config(5)
  2. OpenBSD manual – sshd(8): -t, -T and authorized_keys options
  3. OpenSSH – Release Notes (10.0 to 10.6p2)
  4. Ubuntu Server documentation – OpenSSH server

Spotted a mistake? Let us know and we will correct it openly.