This guide is for people who run Linux servers that matter: payment hosts, bastions, database nodes, CI runners. At the end you will have one drop-in sshd_config file, a way to prove each setting is live, and a short checklist you can paste into a change ticket.

Most SSH hardening guides online were written for OpenSSH 7 or 8. Several of their tips are now defaults, a few are now harmful, and the newest controls are missing. OpenSSH 10.0 made post-quantum key exchange the default and removed DSA. OpenSSH 9.8 added built-in penalties for failed logins. This checklist is written against those versions and marks anything that needs an older fallback.

Nothing here changes how SSH is reached from the outside. Network controls (a bastion, a VPN, firewall rules that allow port 22 only from management ranges) matter more than any single option below. Treat this as the layer behind them.

0. Before you touch anything: avoid locking yourself out

SSH changes are the classic way to lose a server. Three habits prevent it.

  1. Keep your current session open until a new session works with the new settings.
  2. Make sure you have console access (cloud serial console, iLO, iDRAC, vSphere console) before you start.
  3. Test the configuration before every restart:
sudo sshd -t && echo "config OK" 

sshd -t checks syntax and host keys without restarting anything. If it prints an error, do not restart.

Check which version you are running, because several options below depend on it:

# Debian / Ubuntu dpkg -l openssh-server | tail -1 # RHEL / Rocky / Alma / Fedora rpm -q openssh-server 

1. Use a drop-in file, and understand "first value wins"

OpenSSH uses the first value it reads for most keywords. Later lines with the same keyword are ignored. This is the single most misunderstood rule in SSH hardening.

Ubuntu and Debian ship Include /etc/ssh/sshd_config.d/*.conf at the very top of /etc/ssh/sshd_config. RHEL 9 does the same and ships its own 50-redhat.conf drop-in. Files are read in lexical order, so a file named 10-hardening.conf wins over 50-anything.conf and over the main file.

Create your baseline as /etc/ssh/sshd_config.d/10-hardening.conf. If your distribution has no Include line, add one as the first line of sshd_config.

Why it matters: cloud images often add their own drop-ins, such as 50-cloud-init.conf. If you edit the main file, a drop-in can silently override you.

Verify the effective configuration, not the file you edited:

sudo sshd -T | grep -iE 'passwordauthentication|permitrootlogin|kbdinteractive' 

sshd -T prints what the daemon will actually use. Use grep -i, because OpenSSH 10.4 and later print keywords in mixed case and older versions print lower case.

2. The baseline drop-in

Here is the full file. Each block is explained in the sections that follow.

# /etc/ssh/sshd_config.d/10-hardening.conf # Baseline for OpenSSH 9.8+ (notes inline for older versions)  # --- Who may log in, and how --- PermitRootLogin no PubkeyAuthentication yes PasswordAuthentication no KbdInteractiveAuthentication no PermitEmptyPasswords no AuthenticationMethods publickey AllowGroups ssh-users # --- Slow down guessing and pre-auth abuse --- MaxAuthTries 3 LoginGraceTime 30s MaxStartups 10:30:100 # --- No tunnels or forwarding unless a Match block allows it --- DisableForwarding yes PermitTunnel no PermitUserEnvironment no # --- Idle sessions --- ClientAliveInterval 300 ClientAliveCountMax 2 # --- Key strength --- RequiredRSASize 3072 # --- Logging --- LogLevel INFO 

Before you apply it, create the group and add the people and service accounts that need SSH:

sudo groupadd --system ssh-users sudo usermod -aG ssh-users alice 

Then test and restart:

sudo sshd -t && sudo systemctl restart ssh # Debian / Ubuntu sudo sshd -t && sudo systemctl reload sshd # RHEL family 

Open a new terminal and log in before you close the old one.

3. Authentication: keys only, no root

PasswordAuthentication and KbdInteractiveAuthentication both default to yes upstream. Turning off only the first one is a common gap, because keyboard-interactive can still prompt for a password through PAM. AuthenticationMethods publickey makes the rule explicit: one successful public key, nothing else.

PermitRootLogin defaults to prohibit-password, which still allows root with a key. Set it to no. Administrators log in as themselves and use sudo, which gives you a named audit trail.

AllowGroups limits SSH to members of one group. Accounts created by packages, contractors who left, and test users stop being SSH accounts by default.

Verify from another machine:

# Should fail with "Permission denied (publickey)" ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password,keyboard-interactive alice@server # Should fail ssh root@server 

If you must keep passwords for a small group (for example a legacy integration), require a key and a second factor for them instead of a password alone. AuthenticationMethods publickey,keyboard-interactive inside a Match block does that.

4. Use the built-in penalties instead of only fail2ban

OpenSSH 9.8 added PerSourcePenalties, and it is on by default. When a source address fails authentication, crashes sshd, or disconnects without trying, sshd records a penalty. Once penalties pass a minimum, all new connections from that address (and its network block) are refused for a while.

The defaults, from the manual page:

Condition Default penalty
authfail (failed authentication) 5s
invaliduser (login to a user that does not exist, OpenSSH 10.3+) 5s
noauth (disconnect without trying) 1s
grace-exceeded (hit LoginGraceTime) 10s
crash (client caused an sshd crash) 90s
min (threshold before blocking starts) 15s
max (longest block) 10m

Two adjustments are worth considering:

# Exempt your management and monitoring ranges so a broken script cannot lock out operators PerSourcePenaltyExemptList 10.20.0.0/24,10.20.1.0/24 
# Punish guessing of non-existent usernames harder (OpenSSH 10.3+) PerSourcePenalties invaliduser:1m 

The release notes warn that servers reached from behind NAT or a proxy share one source address for many users. On a bastion behind a load balancer, one noisy user could block everyone. Test before you tighten.

Since OpenSSH 10.1, blocks are logged at INFO, so they appear in the default logs. Verify the feature exists on your version:

sudo sshd -T | grep -i persourcepenalties 

No output means your version predates 9.8. Keep fail2ban or an equivalent on those hosts until they are upgraded.

MaxStartups 10:30:100 is the upstream default, listed here so it is explicit. It starts dropping unauthenticated connections at random once 10 are pending.

5. Turn forwarding off, then allow it where needed

AllowTcpForwarding and AllowAgentForwarding both default to yes. On a payment host, an SSH session that can open tunnels is a ready-made pivot into the network. DisableForwarding yes turns off TCP, agent, X11 and Unix-socket forwarding in one line.

Be aware of two facts from the manual and release notes:

  • Disabling forwarding does not stop a user who has a shell from running their own forwarder. It removes the easy path, not every path.
  • Before OpenSSH 10.0, DisableForwarding did not disable X11 and agent forwarding as documented. On older versions also set AllowAgentForwarding no and X11Forwarding no.

Bastions and jump hosts usually need TCP forwarding for ssh -J. Re-enable it only for that group with a Match block. Put Match blocks at the end of the main /etc/ssh/sshd_config, because a Match applies to every line after it until the next Match:

# End of /etc/ssh/sshd_config on a bastion Match Group bastion-users DisableForwarding no AllowTcpForwarding local AllowAgentForwarding no X11Forwarding no 

Verify what a specific user would get, without logging in:

sudo sshd -T -C user=alice,host=admin-pc,addr=10.20.0.15 | grep -iE 'disableforwarding|allowtcpforwarding' 

The -C option applies the Match rules for that simulated connection.

6. Cryptography: trust the new defaults, do not paste old lists

This is where old guides now do harm. Many of them tell you to set KexAlgorithms to a fixed list such as curve25519-sha256,diffie-hellman-group16-sha512. On OpenSSH 10, that list removes the post-quantum defaults.

The OpenSSH 10 default key exchange list starts with:

mlkem768x25519-sha256, sntrup761x25519-sha512,sntrup761x25519-sha512@openssh.com, curve25519-sha256,curve25519-sha256@libssh.org, ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521 

OpenSSH 10.0 also removed finite-field Diffie-Hellman from the server's default list and removed DSA entirely. OpenSSH 10.6 adds a server-side WarnWeakCrypto option, on by default, that logs connections that do not use post-quantum key exchange. The reason is "store now, decrypt later": recorded traffic could be decrypted once quantum computers are practical.

What to do:

  • On OpenSSH 10.x, do not set KexAlgorithms, Ciphers or MACs unless an auditor requires it. If you must remove something, use the - prefix, which subtracts from the default instead of replacing it: KexAlgorithms -ecdh-sha2-nistp256.
  • On RHEL and its rebuilds, crypto settings come from system-wide crypto policies. Change them with update-crypto-policies, not by hand-editing sshd_config. To opt out for SSH only, Red Hat documents a drop-in numbered below 50, such as 49-crypto-policy-override.conf.
  • Raise RequiredRSASize from the default 1024 to 3072. This rejects weak RSA user keys. Note that the setting can only be raised, never lowered.

Verify what a client actually negotiates:

ssh -v alice@server exit 2>&1 | grep 'kex: algorithm' 

On a current client and server, expect mlkem768x25519-sha256.

For a full external view, ssh-audit checks the algorithms, host keys and banner against built-in policies:

pip3 install ssh-audit ssh-audit --skip-rate-test server.example.internal ssh-audit -L # list built-in hardening policies 

The default audit runs a connection-rate test. Skip it against production so you do not trip MaxStartups or penalties.

7. Host keys: keep Ed25519, retire what you do not need

Check which host keys exist and their sizes:

for f in /etc/ssh/ssh_host_*_key.pub; do ssh-keygen -l -f "$f"; done 

Expect Ed25519 and usually RSA and ECDSA. DSA keys are useless on OpenSSH 10 and should be deleted with their HostKey lines. Private host key files must not be group- or world-readable; sshd refuses to use them if they are.

If you rotate host keys, every client sees a "host key changed" warning. Plan it, publish the new fingerprints, or move to host certificates (next section) so clients trust a CA instead of individual keys.

8. Replace long-lived keys with short-lived certificates

The quiet risk in SSH is not passwords. It is thousands of authorized_keys entries that nobody owns. NIST IR 7966 is built around this problem: user keys get provisioned easily and are almost never removed.

Find every authorized_keys file and count the keys in it (read-only):

sudo find / -xdev \( -name authorized_keys -o -name authorized_keys2 \) -type f \ -exec sh -c 'printf "%s\t%s keys\n" "$1" "$(grep -cvE "^\s*(#|$)" "$1")"' _ {} \; 

Review the result like an access list: every key needs an owner and a reason.

OpenSSH certificates fix this at the source. A CA you control signs a user's public key with an identity, allowed usernames ("principals") and an expiry. Servers trust the CA, not each key.

Sign a user key that is valid for eight hours:

ssh-keygen -s /secure/user_ca -I alice@corp -n alice,deploy -V +8h alice_id_ed25519.pub 

On each server:

# in 10-hardening.conf TrustedUserCAKeys /etc/ssh/user_ca.pub AuthorizedPrincipalsFile /etc/ssh/auth_principals/%u 

Each file in /etc/ssh/auth_principals/ lists the principals allowed to log in as that local user. Certificates without principals are rejected. Keep the CA private key offline or in an HSM; ssh-keygen can sign with a key held in a PKCS#11 token (-D) or an agent (-U).

Revocation still matters for the hours a certificate is valid. RevokedKeys accepts a key revocation list made with ssh-keygen -k. Replace that file atomically, because if it becomes unreadable, sshd refuses all public key logins.

For administrators, hardware-backed keys (ssh-keygen -t ed25519-sk) add a physical touch to every login. PubkeyAuthOptions verify-required also demands a PIN on the security key.

9. Logging and what to watch

LogLevel INFO is the default and records successful and failed logins. Avoid DEBUG in production; the manual notes it violates user privacy.

Quick checks you can run today:

# Ubuntu / Debian unit name is ssh, RHEL is sshd sudo journalctl -u ssh --since "24 hours ago" | grep -E 'Accepted|Invalid user|Failed' | tail -50 

Forward these logs to a central system. Alert on:

  • A successful login from an address outside your management ranges.
  • Accepted password on any host where passwords are supposed to be off.
  • Logins as service accounts at unusual times.
  • Penalty blocks against your own management ranges, which means a script is broken.

10. Keep OpenSSH patched quickly

The OpenSSH team now ships more frequent releases because AI-assisted bug finding has increased the flow of security reports. In November 2026 a password bypass hit musl-based systems built without PAM, including Alpine's default openssh-server. Hosts with password authentication already off had far less to worry about. Our fix guide for CVE-2026-123158 (SSHole) covers the update and detection steps.

Put openssh-server on your fast-patch list, with a target in days, not a monthly cycle. If you need a repeatable way to do that across many hosts, the free first module of our patching course, The first hour, covers triage and rollout order.

Common mistakes

  • Editing the main file while a drop-in overrides it. Always confirm with sshd -T, never by reading the file.
  • Turning off PasswordAuthentication but not KbdInteractiveAuthentication. PAM can still prompt for a password.
  • Pasting a cipher list from an old guide. On OpenSSH 10 this removes post-quantum key exchange.
  • Changing Port on Ubuntu 22.10 or later and nothing happens. Ubuntu uses systemd socket activation, so the listening port is set in ssh.socket drop-ins under /etc/systemd/system/ssh.socket.d/. A changed port is also not a security control.
  • Putting a Match block in the middle of the file. Every global line after it becomes part of the block.
  • Restarting without a second session open. Keep one session open until a new login works.
  • Treating DisableForwarding as complete isolation. A user with a shell can still run their own forwarder. Use ForceCommand or no shell for service accounts that only copy files.
  • Ignoring authorized_keys sprawl. Hardened sshd_config does not help if a former contractor's key still works.

Checklist

SSH hardening checklist (OpenSSH 9.8+ / 10.x) [ ] Console access confirmed; second session open during changes [ ] Baseline in /etc/ssh/sshd_config.d/10-hardening.conf (Include at top of sshd_config) [ ] sshd -t passes before every restart [ ] sshd -T shows: permitrootlogin no, passwordauthentication no, kbdinteractiveauthentication no, authenticationmethods publickey [ ] AllowGroups ssh-users; only named people and service accounts in it [ ] MaxAuthTries 3, LoginGraceTime 30s [ ] PerSourcePenalties present (9.8+); management ranges in PerSourcePenaltyExemptList [ ] DisableForwarding yes globally; Match blocks at end of file for bastion groups [ ] On < 10.0: AllowAgentForwarding no and X11Forwarding no set explicitly [ ] No custom KexAlgorithms/Ciphers/MACs on 10.x; RHEL uses crypto-policies [ ] ssh -v shows kex mlkem768x25519-sha256 from current clients [ ] RequiredRSASize 3072; no DSA host keys [ ] authorized_keys inventory reviewed; every key has an owner [ ] Plan for short-lived user certificates (TrustedUserCAKeys + principals) [ ] Auth logs shipped centrally; alert on Accepted password and off-range logins [ ] openssh-server on the fast-patch list 

Sources

Originally published on errorbudget.io, where we also publish fix guides for actively exploited vulnerabilities.