Guides / Linux servers

Harden SSH on Debian, Ubuntu, RHEL, Rocky, Alma and Alpine

beginnerUpdated 26 Nov 20261294 words

SSH is the way into most servers, and it is the service that bots try first. A short drop-in file turns off passwords and direct root logins and trims the rest. sshd sometimes ignores such a file, 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. A drop-in file there is ignored. 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

Check which case you have with grep -i '^include' /etc/ssh/sshd_config.

For each keyword, sshd uses the first value it reads. The files in sshd_config.d are read in alphabetical order, before the rest of sshd_config. So a file named 10-hardening.conf wins over 50-cloud-init.conf and over any line further down in sshd_config. 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, which can say yes.

The effective configuration, after all includes, comes from sshd -T:

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. Only keys (or certificates) are accepted.
KbdInteractiveAuthentication no Keyboard-interactive is the other way to ask for a password, through PAM. It has to be off as well, or 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, sshd is started through ssh.socket, and a systemd generator reads Port and ListenAddress from the sshd configuration. Settings such as the ones 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. Tell the client which key to use 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 narrow logins to named accounts. 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 as shown above, and test with a new connection.

Ciphers, MACs and key exchange

Current OpenSSH releases pick safe algorithms on their own. Problems come from old lines that someone added years ago 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, the best fix is to 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. Read the list the running server uses with:

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 any login. Distributions shipped fixed packages, often with the old version number and a patch, so check the package version with your package manager, not ssh -V. Automatic security updates (Debian and Ubuntu, RHEL family, Alpine) cover this.

OpenSSH 9.8 and later also block addresses for a while that fail to authenticate again and again (PerSourcePenalties, on by default). Of the releases tested 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). A root login with your key should fail too when PermitRootLogin no is set.

Patchbrief reads the effective sshd settings on each server every day and reports the ones on this page.

Sources

Check your servers every day

The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.

Start free