An exposed SSH server is the front door to your machine, and by default Debian and Ubuntu leave it on the latch: passwords are accepted, root can log in, and the crypto list still carries algorithms from another era. This is a practical, verifiable hardening pass for Debian and Ubuntu — keys only, no root, a tight cipher list and a post-quantum key exchange. Every step is reversible, and nothing here needs a reboot.
What you are about to change
All of it lives in /etc/ssh/sshd_config, and on Debian 12 and Ubuntu 22.04 or newer the sane way to add rules is a drop-in file in /etc/ssh/sshd_config.d/, which the main config includes. Keep your changes in one file you can delete in a second.
| Directive | Default | What we set |
|---|---|---|
PasswordAuthentication |
yes | no |
PermitRootLogin |
prohibit-password | no |
KexAlgorithms |
long list | post-quantum only |
MaxAuthTries |
6 | 3 |
1. Keep a second session open
Before you touch anything, open a second SSH session and leave it logged in. If a bad rule locks you out, that session is your way back in to fix it. Then create the drop-in file:
sudo nano /etc/ssh/sshd_config.d/99-hardening.conf
2. Switch to key-only authentication
Generate an Ed25519 key on your workstation, copy it over, and test it before you turn passwords off:
# On your workstation
ssh-keygen -t ed25519 -a 100 -C "me@laptop"
ssh-copy-id user@server
# Prove the key works on its own
ssh -o PreferredAuthentications=publickey user@server
Not every key is worth having. If you are choosing today, pick Ed25519; if you want the private key locked in hardware, pick the -sk variant:
| Key type | Verdict |
|---|---|
ed25519 |
The default choice: small, fast, no parameter tuning |
ed25519-sk |
Same, but the private key lives on a FIDO2 token |
rsa (4096) |
Only for old servers; slower and much larger |
dsa / ecdsa |
Avoid: DSA is broken, ECDSA is tied to NIST curves |
Only then, on the server:
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitEmptyPasswords no
3. Never log in as root
Root over SSH is a username every attacker already knows. Turn it off and use sudo, which is logged and auditable:
PermitRootLogin no
AllowUsers youruser
4. Keep only modern crypto — and post-quantum key exchange
Since OpenSSH 10.0 (April 2025) the hybrid key exchange mlkem768x25519-sha256 is the default, combining ML-KEM (FIPS 203) with X25519. Restricting the server to post-quantum and modern AEAD ciphers drops a lot of legacy surface:
KexAlgorithms mlkem768x25519-sha256,sntrup761x25519-sha512
Ciphers chacha20-poly1305@openssh.com,aes256-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
OpenSSH 10.6 (October 2026) added the WarnWeakCrypto option to the server, on by default: it logs any client that negotiates a key exchange that is not post-quantum. If your log fills with those warnings, the client is the one that needs updating.
5. Shrink the attack surface
MaxAuthTries 3
LoginGraceTime 20
X11Forwarding no
AllowAgentForwarding no
PermitTunnel no
ClientAliveInterval 300
ClientAliveCountMax 2
6. Add fail2ban for the brute-force noise
With keys only, password guessing cannot succeed, but fail2ban still cuts the log noise and the wasted connections:
sudo apt install fail2ban
sudo tee /etc/fail2ban/jail.d/sshd.local >/dev/null <<'EOF'
[sshd]
enabled = true
backend = systemd
maxretry = 3
bantime = 1h
EOF
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
7. Verify before you trust it
sudo sshd -t # syntax check, no output = ok
sudo sshd -T | grep -Ei 'password|permitroot|kex'
sudo systemctl restart ssh # Debian/Ubuntu unit name
ssh -v user@server 2>&1 | grep 'kex: algorithm'
The last line should print mlkem768x25519-sha256. If it prints curve25519-sha256, the server or the client is too old to negotiate a post-quantum key exchange, and that is exactly the signal you want to see.
A note on the port, and on agent forwarding
Changing the SSH port from 22 is the most debated tip in this list. It does not make you safer in any real sense — a port scan finds the new one in seconds — but it removes the overwhelming majority of automated login attempts from your logs, which makes the noise you actually care about visible. If you do move it, remember to update your firewall and any client config, or you will lock yourself out of a host that worked a minute ago.
Agent forwarding is the subtler trap. Forwarding your local ssh-agent to a server means anyone with root there can use your keys while you are connected. For a jump host you trust, fine; for a shared box, do not. AllowAgentForwarding no on the server and ForwardAgent no on the client is the safe default.
Harden the client too
The server is only half the connection. A short block at the top of ~/.ssh/config applies sane defaults to every host:
Host *
HashKnownHosts yes
ForwardAgent no
ServerAliveInterval 60
ServerAliveCountMax 3
HashKnownHosts stops known_hosts from leaking the hostnames you connect to, and the keepalives drop a dead session instead of leaving it hanging.
What good logs look like
After a day, check what your server is actually seeing. With keys only, the line you want is Accepted publickey; everything else is noise:
journalctl -u ssh -b | grep -iE 'accepted|failed' | tail -20
sudo grep 'sshd' /var/log/auth.log | tail -20 # Debian/Ubuntu
If you see a steady stream of Failed password from the same addresses, fail2ban is doing its job and you can watch it ban them: sudo fail2ban-client status sshd lists the current bans, and sudo fail2ban-client set sshd unbanip 203.0.113.7 lifts one when you lock out a colleague by mistake.
Closing
None of this is exotic: keys instead of passwords, no root, a short list of modern algorithms, and a verification command that tells you the truth. Run sshd -T after every edit, keep that second session open until you have reconnected, and your Debian or Ubuntu box stops being a password away from a stranger.
Sources
- OpenSSH — Post-Quantum Cryptography: the hybrid key exchanges and when they became default.
- OpenSSH 10.6 release notes: the server-side
WarnWeakCryptooption and the LZ77 compression fix. - sshd_config(5) — Debian man page: every directive used here, with its default.
- fail2ban — GitHub: jail configuration and the systemd backend.





Leave a Reply