SSH hardening for Linux servers: settings you can verify

The sshd_config settings that matter on Debian, Ubuntu and RHEL, why a pasted config often changes nothing, and how to prove each setting is in effect.

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. This guide covers the settings worth changing on current Debian, Ubuntu and RHEL-family servers, and how to prove each one is actually in effect.

Keep an existing session open while you work and test every change from a second terminal. If the new config is wrong, the open session is how you fix it. Setting up a brand-new server? Start with the general checklist; this guide goes deeper on SSH. How to secure a Linux VPS →

How sshd reads its configuration

For most keywords, sshd uses the first value it reads, not the last. On Debian and Ubuntu, /etc/ssh/sshd_config starts with Include /etc/ssh/sshd_config.d/*.conf, and the included files are read in lexical order before the rest of the main file. A drop-in called 10-hardening.conf therefore beats the main file and any drop-in that sorts after it. Some VPS images set PermitRootLogin yes in the main file; your drop-in overrides it. sshd_config(5) ↗

grep -n Include /etc/ssh/sshd_config
ls /etc/ssh/sshd_config.d/

The only reliable check is to ask sshd what it will really use. sshd -t checks the syntax and prints nothing when the file is valid; sshd -T prints every effective setting in lowercase. Run both after every change in this guide. sshd(8) ↗

sudo sshd -t
sudo sshd -T | grep -iE "passwordauthentication|permitrootlogin"

The baseline config

First create a group for the people allowed to log in, and add yourself to it. Do this before the config below, or AllowGroups will lock you out.

sudo groupadd ssh-users
sudo usermod -aG ssh-users deploy
id deploy   # should list ssh-users

Then put the settings in a drop-in file:

# /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
SettingDefaultWhy change it
PasswordAuthentication noyesBots guess passwords all day. They can’t guess a key.
KbdInteractiveAuthentication noyesWith UsePAM yes, this is a second route to a password prompt.
PermitRootLogin noprohibit-passwordAttackers try root first. Named users plus sudo also leave a record of who did what.
AllowGroups ssh-usersEveryoneA forgotten account or a service user with a password can’t log in at all.
MaxAuthTries 36Fewer guesses per connection.
LoginGraceTime 30120 secondsConnections that haven’t logged in are dropped sooner.
X11Forwarding nono, but Debian’s package sets yesServers rarely need to forward a graphical display.
AllowAgentForwarding noyesProtects your keys rather than the server. See below.

Agent forwarding deserves a sentence. Anyone who can get past file permissions on the server, which includes root, can use your forwarded agent to authenticate elsewhere as you. They can’t copy the key, but they don’t need to. To reach a host behind a bastion, use ssh -J bastion target instead: the connection is tunnelled through the bastion and your agent never leaves your machine. ssh_config(5): ForwardAgent ↗

One catch with MaxAuthTries 3: before OpenSSH 10.6, every key your agent offers counts as an attempt, so with several keys loaded you can run out before the right one is tried. Name the key for each host with IdentityFile and IdentitiesOnly yes in ~/.ssh/config. OpenSSH release notes ↗

Apply the config and check it:

sudo sshd -t && sudo systemctl reload ssh   # the service is sshd on RHEL and Fedora
sudo sshd -T | grep -iE "passwordauthentication|kbdinteractiveauthentication|permitrootlogin|allowgroups|maxauthtries|logingracetime|x11forwarding|allowagentforwarding"

Then, from a second terminal, confirm that passwords are really refused. This should fail with “Permission denied (publickey)”:

ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@your-server

Brute-force penalties are built in since OpenSSH 9.8

OpenSSH 9.8 added PerSourcePenalties, on by default: sshd refuses connections for a while from addresses that repeatedly fail to log in, connect without ever authenticating, or crash the server. If your server runs 9.8 or later, you already have basic throttling without installing anything. If it runs something older, a tool such as fail2ban fills that gap. OpenSSH 9.8 release notes ↗ Fail2ban and its alternatives →

dpkg -s openssh-server | grep ^Version   # Debian, Ubuntu
rpm -q openssh-server                     # RHEL, Fedora

The release notes warn that penalties apply per address, so a team sharing one office or VPN exit address can briefly lock each other out after a few mistakes. Exempt that range with PerSourcePenaltyExemptList.

Limit where SSH can be reached from

This is the single most effective change, because a login prompt nobody can reach can’t be attacked. If your team connects from known addresses or over a VPN, allow SSH only from there:

sudo ufw allow from 203.0.113.0/24 to any port 22 proto tcp
sudo ufw status numbered
sudo ufw delete allow OpenSSH   # only after the rule above works

To check, try from an address outside the range. nc -vz -w 5 your-server 22 should time out instead of connecting. For a fleet, the usual pattern is one bastion host or a VPN, with every other server accepting SSH only from it.

Should you change the port?

Moving SSH off port 22 cuts the noise from bots in your logs. It doesn’t stop anyone who scans all ports, so treat it as tidying rather than protection. If you do move it, open the new port in the firewall first.

On Ubuntu 22.10 and later, SSH is socket-activated, so changing Port and restarting ssh.service does nothing. Ubuntu’s maintainers say to run systemctl daemon-reload and then systemctl restart ssh.socket. On RHEL with SELinux enforcing, allow the new port first with semanage port -a -t ssh_port_t -p tcp 2222. Ubuntu bug 2069041 ↗

Use hardware-backed keys for admins

Since OpenSSH 8.2, keys can live on a FIDO security key. The private key never leaves the token and every login needs a physical touch, so a stolen laptop or a copied ~/.ssh folder isn’t enough to get in. Nothing changes on the server: it is still public-key authentication. OpenSSH 8.2 release notes ↗

ssh-keygen -t ed25519-sk
ssh-copy-id -i ~/.ssh/id_ed25519_sk.pub deploy@your-server

To check, the new line in the server’s ~/.ssh/authorized_keys starts with [email protected].

Leave the cryptography defaults alone

Many guides still paste Ciphers, MACs and KexAlgorithms lists written years ago. A fixed list freezes your server in that year. Current OpenSSH prefers mlkem768x25519-sha256, a hybrid post-quantum key exchange, and an old pasted list silently removes it. On RHEL, these settings come from the system-wide crypto policy, which you change with update-crypto-policies rather than in sshd_config. sshd_config(5): KexAlgorithms ↗ Red Hat: OpenSSH and crypto policies ↗

To see what your server offers, use ssh-audit, which is packaged in Debian and available from its GitHub project. If you decide to drop something it flags, remove it with a leading minus so you keep every other default and anything added in future releases: ssh-audit on GitHub ↗

sudo apt install ssh-audit
ssh-audit localhost

# example: drop the NIST-curve key exchanges, keep the rest of the defaults
KexAlgorithms -ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521

To check what a real connection negotiated, run ssh -v your-server and look for the “kex: algorithm:” line.

Keep sshd patched

In July 2024, regreSSHion (CVE-2024-6387) showed why updates beat any setting. It was a race condition in sshd, present in portable OpenSSH 8.5p1 to 9.7p1, that could allow remote code execution as root. The OpenSSH team reported exploitation on 32-bit Linux with glibc after 6 to 8 hours of connections under lab conditions. NVD rates it 8.1, high. OpenSSH 9.8 security notes ↗ NVD: CVE-2024-6387 ↗

Distributions backport fixes without changing the upstream version number, so don’t judge your exposure from the version string. Check your distribution’s tracker, and make sure security updates install automatically. Debian security tracker ↗

apt list --upgradable 2>/dev/null | grep openssh   # Debian, Ubuntu
dnf check-update openssh-server                    # RHEL, Fedora

Watch the logins that succeed

Hardening lowers the odds of a break-in. It doesn’t tell you when someone gets in with a stolen key. Look at successful logins regularly:

sudo journalctl -u ssh --since yesterday | grep Accepted   # -u sshd on RHEL

Since 9.8, some sshd log lines carry the name sshd-session rather than sshd. Filtering by the systemd unit, as above, catches both; a filter that matches on the program name may not. OpenSSH 9.8 release notes ↗

Across more than a few servers, nobody reads those lines every day. Vigilhold, our host agent for Linux servers, watches SSH logins along with processes, connections and file changes on every server. It also warns you if a server with SSH open to the internet stops reporting login activity for three hours, which is how a logging change that blinds your other tools usually shows up. What a HIDS does → See Vigilhold →

The short version

StepCheck it worked
Config is in effectsshd -T shows your values, not the defaults
Passwords refusedA password-only login attempt gets Permission denied (publickey)
Only the group can log inA user outside ssh-users is refused
Brute-force penaltiesopenssh-server is 9.8 or later, or a ban tool is counting failures
Reachable only where expectednc from an outside address times out
PatchedNo pending openssh updates
Logins visibleYou can list yesterday’s Accepted lines, or something does it for you