← Back to section · Digital Security
SSH Hardening Guide: Start by Turning Off Passwords
Key-only authentication, algorithm selection, the sshd_config settings that matter, fail2ban and bastion hosts, and certificate-based SSH — a checklist you can work through directly.
Why SSH is the front door
SSH is the remote administration entry point on nearly every server, and accordingly one of the most scanned and brute-forced services on the internet: a freshly provisioned machine typically sees its first password attempts on port 22 within minutes. The goal of hardening is not to hide SSH but to make being scanned irrelevant — an attacker may reach the port, but there is no credential to guess.
First cut: public-key authentication only
Generate an Ed25519 key pair — a modern algorithm with short signatures and well-reviewed implementations — and keep the private key passphrase-protected on your own machine:
ssh-keygen -t ed25519 -a 100 -f ~/.ssh/id_ed25519
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@server
Once key login is confirmed working, close every password path on the server in /etc/ssh/sshd_config:
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
PermitRootLogin no forces root access through a named account plus sudo, keeping the audit trail intact; if you must keep it, prohibit-password is the floor. Validate the syntax with sshd -t before reloading — and do not close your current session until a new one has logged in successfully. Locking yourself out is the classic self-inflicted incident of SSH hardening.
Algorithm selection, including post-quantum key exchange
Declare the algorithm lists explicitly and drop the historical baggage — CBC modes, arcfour, ssh-rsa (SHA-1):
KexAlgorithms sntrup761x25519-sha512@openssh.com,curve25519-sha256,curve25519-sha256@libssh.org
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
MACs hmac-sha2-512-etm@openssh.com,hmac-sha2-256-etm@openssh.com
HostKeyAlgorithms ssh-ed25519,rsa-sha2-512,rsa-sha2-256
The post-quantum dimension deserves attention. Since OpenSSH 9.0 (2022), the default key exchange has been sntrup761x25519-sha512@openssh.com — a hybrid combining the post-quantum NTRU Prime algorithm with classical X25519, so the session key is safe if either half holds. It exists specifically to defeat “harvest now, decrypt later”, where today’s recorded handshakes are decrypted by a future quantum computer. OpenSSH 10.0 moved the default again, to mlkem768x25519-sha256. In other words, simply keeping OpenSSH current has quietly completed the post-quantum migration of SSH key exchange; on older versions you can put the hybrid algorithm first in KexAlgorithms yourself.
Tighten the remaining settings in the same pass:
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowTcpForwarding no # except where forwarding is the point, e.g. a bastion
AllowUsers deploy ops # allow-list; every other account is refused
ClientAliveInterval 300
ClientAliveCountMax 2
Verify: effective configuration and negotiated results
A correct-looking file is not the same as an effective configuration. OpenSSH ships two self-check tools. sshd -T prints the final effective configuration — defaults expanded, conditional blocks resolved — so after every change you can confirm the critical settings were actually adopted. ssh -Q kex and ssh -Q cipher list the algorithms the local client supports, which makes it easy to spot a weak algorithm still lurking in the list. Finally, connect from another machine with ssh -vv and read the debug output to confirm the key exchange actually negotiated is the first entry on your list, not a silent downgrade caused by an ordering or spelling mistake. “Verify the negotiated result” belongs in the change procedure itself, not just “check the file” — typos and distribution defaults overriding local intent are failures that recur in real operations.
Beyond the config: rate limits, bastions, exposure
Add two layers of depth outside the configuration file:
- fail2ban / rate limiting: watch the authentication log and temporarily ban source IPs with repeated failures, or rate-limit new connections at the firewall. This suppresses noise and raises scanning costs; it is not a substitute for key authentication.
- A bastion host: internal machines expose no SSH at all; the only way in is through one hardened jump host, and clients reach through it in a single command:
ssh -J bastion.example.com internal-host
- Changing the port reduces log noise and nothing else. Do it if you like, but never let it substitute for the measures above.
Audit and alerting: somebody has to watch the logins
Hardening is not a one-off act. The sshd authentication log (usually /var/log/auth.log or the ssh unit in journald) must be able to answer three questions: who logged in successfully, when, and from which IP; whether failed attempts cluster abnormally (distributed credential stuffing often stays under per-IP fail2ban thresholds); and whether a key fingerprint has appeared that was never seen before. Centralize the logs and alert on “successful login from a first-seen source IP”, “root-side logins outside working hours” and “one account logging in from several places in a short window” — inexpensive rules that are how most SSH intrusions are actually discovered: not stopped at the firewall, but recognized afterwards in the log. The client side has its own discipline: private keys must be mode 600 and ~/.ssh mode 700; when using ssh-agent, add confirmation and a lifetime (ssh-add -c -t 3600); and keep ForwardAgent off by default — agent forwarding lends your signing capability to whatever the remote end asks, so enable it only for hosts you fully trust.
Host keys: the other half of trust needs care too
Clients authenticate servers by host keys, yet host-key operations are routinely neglected. The first-connection fingerprint check (TOFU) should be verified over an out-of-band channel, not waved through with a reflexive “yes”. Since OpenSSH 8.0 the server supports host key rotation: old and new keys coexist for a transition period, and after a successful authentication the client learns the new key automatically via UpdateHostKeys and retires the old one — no per-machine known_hosts surgery. Rotation triggers include machine rebuilds, suspected compromise, and algorithm upgrades (an RSA host key giving way to Ed25519). One step further, SSH host certificates or SSHFP records in DNS (backed by DNSSEC) turn host identity into a centrally distributable assertion instead of a hundred manual fingerprint comparisons — and humans comparing fingerprints a hundred times will eventually get one wrong; automation is the endgame.
Going further: certificate-based SSH
At scale, authorized_keys distribution and revocation fall apart: when someone leaves, nobody can enumerate every machine holding their public key. OpenSSH natively supports certificate-based SSH. A CA key signs user public keys into short-lived certificates carrying an expiry and a scope (principals); servers trust the CA instead of maintaining per-machine key lists:
# CA issues an 8-hour certificate, valid only for the "deploy" principal
ssh-keygen -s ca_key -I user_key -n deploy -V -5m:+8h -z 2 user_key.pub
The server needs a single line, TrustedUserCAKeys /etc/ssh/ca_key.pub. Certificates expire on their own; revocation and audit concentrate at the CA. This is the end-state of SSH governance at scale — and combined with hardware-backed keys (FIDO2 sk-ed25519, supported since OpenSSH 8.2) the private key can be locked inside a physical device as well.
References
- OpenSSH manuals and the sshd_config manual page — https://www.openssh.com/manual.html ; https://man.openbsd.org/sshd_config
- OpenSSH 9.0 release notes (sntrup761x25519 hybrid key exchange becomes the default, 2022) — https://www.openssh.com/releasenotes.html
- RFC 4251–4254, The Secure Shell (SSH) Protocol Architecture and transport layer — https://www.rfc-editor.org/rfc/rfc4253
- OpenSSH certificate format specification (PROTOCOL.certkeys, shipped with the source) — https://www.openssh.com/specs.html
Tags: SSHOpenSSHHardeningPost-Quantum