Skip to main content

SSH hardening for Linux servers: settings you can verify

Updated 2 min readMarkdown

Most SSH hardening guides are lists of settings to paste. The catch is that sshd quietly ignores a setting when an earlier one wins, so a pasted config can look right and change nothing.

Keep an existing session open while you work and test every change from a second terminal.

sshd uses the first value it reads

For most keywords, sshd uses the first value it reads, not the last (sshd_config(5)). On Debian and Ubuntu, /etc/ssh/sshd_config starts with Include /etc/ssh/sshd_config.d/*.conf, read in lexical order. A drop-in called 10-hardening.conf beats the main file and any drop-in that sorts after it.

The reliable check is to ask sshd itself:

sudo sshd -t   # syntax check, silent when valid
sudo sshd -T | grep -iE "passwordauthentication|permitrootlogin"   # effective values

Baseline

Create the login group first, or AllowGroups locks you out:

sudo groupadd ssh-users
sudo usermod -aG ssh-users deploy
# /etc/ssh/sshd_config.d/10-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AllowGroups ssh-users
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowAgentForwarding no
sudo sshd -t && sudo systemctl reload ssh   # sshd on RHEL/Fedora
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@your-server
# expect: Permission denied (publickey)

Before OpenSSH 10.6, every key your agent offers counts against MaxAuthTries, so pin keys per host with IdentityFile + IdentitiesOnly yes.

Built-in brute-force penalties (OpenSSH 9.8+)

OpenSSH 9.8 added PerSourcePenalties, on by default. On 9.8 or later you already have basic throttling; exempt a shared office/VPN range with PerSourcePenaltyExemptList.

Limit where SSH is reachable

sudo ufw allow from 203.0.113.0/24 to any port 22 proto tcp

Changing the port only cuts log noise. On Ubuntu 22.10+ SSH is socket-activated: run systemctl daemon-reload then systemctl restart ssh.socket (LP#2069041).

Hardware keys, crypto defaults, patching

  • ssh-keygen -t ed25519-sk puts the key on a FIDO token (OpenSSH 8.2+).
  • Don't paste old Ciphers/KexAlgorithms lists: they drop the default post-quantum hybrid mlkem768x25519-sha256. Audit with ssh-audit and remove items with a leading -. On RHEL use update-crypto-policies.
  • CVE-2024-6387 (regreSSHion) hit portable OpenSSH 8.5p1–9.7p1. Distros backport fixes, so check your distro's tracker, not the version string.

Watch successful logins

sudo journalctl -u ssh --since yesterday | grep Accepted

Since 9.8 some lines are tagged sshd-session; filtering by unit catches both.

Full guide with the reasoning for each setting and a verification table: https://alsopss.com/blog/ssh-hardening

About the author

  • I’m Aiden, founder of Al’s-Ops in Utah. I build Vigilhold — host security for small teams that run their own Linux servers and don’t have a security hire. A light agent on each host watches what’s happening, explains findings in plain language with evidence, and only takes disruptive steps within limits you set. I write about practical infrastructure and security: what breaks on real boxes, what “documented” means when the machine disagrees, and how to keep automation under operator control. Previously and alongside that work I’ve shipped full-stack projects and spent a lot of time in the weeds of Linux servers, product tradeoffs, and clear technical writing. Based in Utah’s Silicon Slopes.

No comments yet. Be the first to comment.

More from this blog