DEV Community

ALSOPS
ALSOPS

Posted on Originally published at alsopss.com

SSH hardening for Linux servers: settings you can verify

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
Enter fullscreen mode Exit fullscreen mode

Baseline

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

sudo groupadd ssh-users
sudo usermod -aG ssh-users deploy
Enter fullscreen mode Exit fullscreen mode
# /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
Enter fullscreen mode Exit fullscreen mode
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)
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)