Harden SSH on Debian, Ubuntu, RHEL, Rocky, Alma and Alpine
A short drop-in file turns off passwords and direct root logins. sshd ignores such a file in some setups,
so check what the running server does with sshd -T. If
you still log in with a password, read switching to key-only login first.
Before you change anything
- Keep one SSH session open while you work, and test every change with a second, new connection.
- Make sure a user other than root can log in with a key and run
sudo. - Know how to reach the console of the server (the web console of the hosting provider, IPMI, or a rescue system). A typo in the configuration can lock everyone out.
How sshd reads its configuration
sshd reads /etc/ssh/sshd_config. On Debian 12 and 13, Ubuntu 24.04, Rocky Linux 9 and 10 and Alpine 3.24, the
first active line of that file includes the drop-in directory:
Include /etc/ssh/sshd_config.d/*.conf
RHEL 8 and its rebuilds (Rocky Linux 8, AlmaLinux 8) ship OpenSSH 8.0 with an sshd_config that does not have
this line, and the directory does not exist. sshd ignores a drop-in file there: in a test on Rocky Linux 8.10
(openssh-server 8.0p1-35.el8_10), sshd -T still showed passwordauthentication yes. Add the line once at the
top of sshd_config. The package marks the file as a configuration file, so updates keep your change:
sudo mkdir -p /etc/ssh/sshd_config.d
sudo sed -i '1i Include /etc/ssh/sshd_config.d/*.conf' /etc/ssh/sshd_config
sudo sshd -t
grep -i '^include' /etc/ssh/sshd_config shows which case you have.
For each keyword, sshd uses the first value it reads. It reads the files in sshd_config.d in alphabetical
order, before the rest of sshd_config. A file named 10-hardening.conf therefore wins over
50-cloud-init.conf and over any line further down in sshd_config, and a file named 99-hardening.conf
loses against both. This is a common reason why PasswordAuthentication no seems to be ignored. On cloud
images, cloud-init writes its PasswordAuthentication setting (option ssh_pwauth) to
/etc/ssh/sshd_config.d/50-cloud-init.conf, and that file can say yes.
sshd -T prints the effective configuration after all includes:
sudo sshd -T | grep -E '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|permitemptypasswords|x11forwarding|maxauthtries) '
The hardening file
# /etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitEmptyPasswords no
X11Forwarding no
MaxAuthTries 3
| Setting | Why |
|---|---|
PermitRootLogin no |
Logins go to a named user, and root work goes through sudo, which logs who did what. prohibit-password (the OpenSSH default) still lets root in with a key; it is a fair choice when a backup system logs in as root with a key. |
PasswordAuthentication no |
Password guessing stops working. sshd accepts only keys (or certificates). |
KbdInteractiveAuthentication no |
Keyboard-interactive is the other way to ask for a password, through PAM. If it stays on, passwords still work. OpenSSH before 8.7 (RHEL 8, Ubuntu 20.04) also has ChallengeResponseAuthentication; set both to no there. |
PermitEmptyPasswords no |
The default. Listed so that an old line elsewhere cannot turn it on. |
X11Forwarding no |
A server does not need graphical programs forwarded. Debian, Ubuntu, Rocky and RHEL ship X11Forwarding yes in their configuration, although the OpenSSH default is no. Alpine ships no. |
MaxAuthTries 3 |
Fewer attempts per connection. The default is 6. |
Check the syntax, then reload sshd. Existing sessions stay open.
sudo sshd -t
sudo systemctl reload ssh # Debian, Ubuntu
sudo systemctl reload sshd # RHEL, Rocky, Alma
sudo rc-service sshd reload # Alpine
sshd -t prints nothing when the configuration is valid.
On Ubuntu 24.04, ssh.socket starts sshd, and a systemd generator reads Port and ListenAddress from the
sshd configuration. The settings above apply after the reload; a change of Port or ListenAddress also
needs sudo systemctl daemon-reload and sudo systemctl restart ssh.socket.
Too many authentication failures
With MaxAuthTries 3, a client that has many keys loaded in its SSH agent can fail before it reaches the right
one, because every offered key counts as a try. The client then sees Too many authentication failures. Name
the key in ~/.ssh/config on your workstation:
Host myserver
HostName 203.0.113.10
User deploy
IdentityFile ~/.ssh/id_ed25519
IdentitiesOnly yes
Limit who may log in
AllowUsers or AllowGroups allow logins only for the accounts named. A group is easier to keep up to date:
sudo groupadd ssh-users
sudo usermod -aG ssh-users deploy
echo 'AllowGroups ssh-users' | sudo tee /etc/ssh/sshd_config.d/20-allow.conf
sudo sshd -t
Add yourself to the group before you reload sshd, and test with a new connection.
Ciphers, MACs and key exchange
Current OpenSSH releases pick safe algorithms on their own. Weak ones come from old lines that someone added
to support an old client, such as Ciphers with aes256-cbc or 3des-cbc, or MACs with hmac-md5. Look
at what the server offers:
sudo sshd -T | grep -E '^(ciphers|macs|kexalgorithms) '
On Debian, Ubuntu and Alpine, delete such lines and let the defaults apply. To remove single algorithms from
the defaults without listing all others, prefix the list with -:
MACs -hmac-sha1*,umac-64*
On RHEL, Rocky and Alma, the algorithms come from the system crypto policy, and sshd reads them from
/etc/crypto-policies/back-ends/opensshserver.config. Change the policy instead of sshd_config:
update-crypto-policies --show
sudo update-crypto-policies --set DEFAULT
On RHEL 8 the service passes the policy to sshd as command line options, which override sshd_config, and a
plain sshd -T does not show them. To read the list the running server uses:
sudo sh -c '. /etc/crypto-policies/back-ends/opensshserver.config; sshd -T $CRYPTO_POLICY' | grep -E '^(ciphers|macs|kexalgorithms) '
The DEFAULT policy of RHEL 8 still offers aes256-cbc, aes128-cbc, diffie-hellman-group14-sha1 and
diffie-hellman-group-exchange-sha1. The explainer of ssh.weak_algorithms shows
how to remove them with policy modules.
Keep OpenSSH updated
Configuration does not protect against bugs in sshd itself. CVE-2024-6387
("regreSSHion") was a race condition in OpenSSH 8.5p1 to 9.7p1 that could give remote code execution as root
without a login. Distributions shipped fixed packages, often with the old version number and a patch, so check
the package version with your package manager instead of ssh -V. Automatic security updates
(Debian and Ubuntu, RHEL family,
Alpine) install such fixes.
OpenSSH 9.8 and later also block, for a while, addresses that fail to authenticate again and again
(PerSourcePenalties, on by default). Of the releases checked here, Debian 13 (OpenSSH 10.0), Rocky Linux 9.9
and 10.3 (9.9) and Alpine 3.24 (10.3) have it; Debian 12 (9.2) and Ubuntu 24.04 (9.6) do not. See
fail2ban or CrowdSec for what that means for brute-force protection.
Check the result from outside
From your workstation, try to log in with a password only:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password,keyboard-interactive deploy@203.0.113.10
The answer should be Permission denied (publickey). With PermitRootLogin no, a root login with your key
fails too.
Vulnpocalypse reads the effective sshd settings on each server every day and reports the ones on this page.
Sources
- sshd_config(5): first obtained value wins,
Include, defaults of each setting, default algorithm lists and the+/-/^prefixes - OpenSSH 9.8 release notes: CVE-2024-6387 (versions 8.5p1 to 9.7p1) and
PerSourcePenalties - Test runs on 2026-11-26 with
sshd -T, andsshd -ton a configuration withPerSourcePenalties yes: Rocky Linux 9.9 (openssh-server 9.9p1-17.el9_9), Debian 13 (1:10.0p1-7+deb13u4) and Alpine 3.24 (10.3_p1-r2) have the setting on; Debian 12 (1:9.2p1-2+deb12u10) and Ubuntu 24.04 (1:9.6p1-3ubuntu13.20) reject it as a bad configuration option - Rocky Linux 10 BaseOS packages: openssh-server 9.9p1-34.el10_3
- Ubuntu: sshd socket-based activation:
ssh.socket, the generator of Ubuntu 24.04 (we found/usr/lib/systemd/system-generators/sshd-socket-generatorin openssh-server of 24.04) - OpenSSH 8.7 release notes:
KbdInteractiveAuthenticationreplacesChallengeResponseAuthentication - cloud-init ssh_util.py: writes
sshd_config.d/50-cloud-init.conf; set passwords module:ssh_pwauth - crypto-policies (upstream of the RHEL system-wide cryptographic policies)
- RHEL 8 tested on 2026-11-26 in
rockylinux/rockylinux:8.10.20240528(openssh-server 8.0p1-35.el8_10):sshd_configwithoutInclude, a drop-in ignored, theIncludeline at the top,rpm -qcand thenoreplaceflag ofsshd_config,$CRYPTO_POLICYinsshd.service
Check your servers every day
The Vulnpocalypse agent reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.