TExcelTAD
Home › Linux Server Administration › Hardening the SSH Server

Hardening the SSH Server

A server with a public IP address sees SSH login attempts within minutes of booting, all day, every day. Almost all of them guess passwords for root, admin, oracle and friends. Hardening sshd makes that noise irrelevant: no passwords, no root logins, only the accounts that need SSH, and nothing it doesn't use.

A hardened baseline

On Ubuntu 24.04 the main file /etc/ssh/sshd_config starts with Include /etc/ssh/sshd_config.d/*.conf, so put your settings in a drop-in such as /etc/ssh/sshd_config.d/10-hardening.conf:

text
# Authentication: keys only, no root
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
PermitEmptyPasswords no

# Who may log in at all
AllowGroups ssh-users

# Slow down guessing, drop dead sessions
MaxAuthTries 3
LoginGraceTime 20
ClientAliveInterval 300
ClientAliveCountMax 2

# Features most servers don't need
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
PermitTunnel no
Setting Why
PasswordAuthentication no, KbdInteractiveAuthentication no password guessing becomes pointless; both are needed, because keyboard-interactive can also ask for a password via PAM
PermitRootLogin no admins log in as themselves and use sudo, so actions are attributable; prohibit-password (the default) still allows root with a key
AllowGroups ssh-users a new account (a service user, a package's user) can't log in unless you add it to the group
MaxAuthTries 3 fewer keys tried per connection; an agent with many keys may need IdentitiesOnly yes on the client
ClientAliveInterval 300 + CountMax 2 drop sessions whose client vanished, after about 10 minutes
AllowTcpForwarding no etc. no tunnels through the server unless a Match block allows them

Changing the port (Port 2222) reduces log noise but is not a security control; key-only authentication is.

The trap: the first value wins

In sshd_config, unlike most config files, the first occurrence of a keyword wins. Drop-ins are included at the top, so they override the main file, and among drop-ins the alphabetically first wins. Cloud images often ship /etc/ssh/sshd_config.d/50-cloud-init.conf with PasswordAuthentication yes. sshd -T prints the effective configuration, which is the only reliable way to know what you got:

bash · try it yourself
ssh-keygen -q -t ed25519 -N "" -f hostkey
mkdir -p sshd_config.d
echo "PasswordAuthentication yes" > sshd_config.d/50-cloud-init.conf
cat > sshd_config <<EOF
Include $PWD/sshd_config.d/*.conf
HostKey $PWD/hostkey
PidFile $PWD/sshd.pid
PermitRootLogin no
PasswordAuthentication no
EOF
sshd -t -f sshd_config && echo "syntax OK"
sshd -T -f sshd_config | grep -E '^(permitrootlogin|passwordauthentication) '
echo "--- after adding 10-hardening.conf:"
printf 'PasswordAuthentication no\nKbdInteractiveAuthentication no\n' > sshd_config.d/10-hardening.conf
sshd -T -f sshd_config | grep -E '^(passwordauthentication|kbdinteractiveauthentication) '
syntax OK
permitrootlogin no
passwordauthentication yes
--- after adding 10-hardening.conf:
passwordauthentication no
kbdinteractiveauthentication no

The syntax was fine both times, but the first effective config still allowed passwords. Prefix your drop-in with a low number (10-) so it is read first, and check sshd -T in your automation, not just sshd -t.

Match blocks: exceptions per user, group or address

Match applies settings to some connections only; it lasts until the next Match or the end of the file, so keep Match blocks at the end. sshd -T -C evaluates the config for a hypothetical connection:

bash · try it yourself
ssh-keygen -q -t ed25519 -N "" -f hostkey
cat > sshd_config <<EOF
HostKey $PWD/hostkey
PidFile $PWD/sshd.pid
PasswordAuthentication no
AllowTcpForwarding no
X11Forwarding no

Match User backup
    ForceCommand /usr/local/bin/backup-receive
    PermitTTY no

Match User ada Address 10.0.0.0/8
    AllowTcpForwarding yes
EOF
sshd -t -f sshd_config && echo "syntax OK"
for who in "user=ada,addr=10.1.2.3" "user=ada,addr=192.0.2.9" "user=backup,addr=192.0.2.9"; do
  echo "== $who"
  sshd -T -f sshd_config -C "$who,host=client" | grep -E '^(allowtcpforwarding|forcecommand|permittty) '
done
syntax OK
== user=ada,addr=10.1.2.3
permittty yes
allowtcpforwarding yes
forcecommand none
== user=ada,addr=192.0.2.9
permittty yes
allowtcpforwarding no
forcecommand none
== user=backup,addr=192.0.2.9
permittty no
allowtcpforwarding no
forcecommand /usr/local/bin/backup-receive

Ada gets port forwarding only from the internal network, and the backup key can do nothing but run its receiver. Match Group admins works too, but -C resolves groups by looking the user up on the machine where you run it, so test group rules on the server itself (or a VM with the same users).

What sshd -t catches

bash · try it yourself
ssh-keygen -q -t ed25519 -N "" -f hostkey
printf 'HostKey %s/hostkey\nPermitRootLogn no\n' "$PWD" > typo.conf
printf 'HostKey %s/hostkey\nPermitRootLogin maybe\n' "$PWD" > value.conf
printf 'HostKey %s/hostkey\nKexAlgorithms curve25519-sha265\n' "$PWD" > algo.conf
printf 'HostKey %s/hostkey\nKexAlgorithms +diffie-hellman-group1-sha1\n' "$PWD" > weak.conf
for f in typo.conf value.conf algo.conf weak.conf; do
  echo "== $f"; sshd -t -f "$f"; echo "exit code: $?"
done
== typo.conf
typo.conf: line 2: Bad configuration option: PermitRootLogn
typo.conf: terminating, 1 bad configuration options
exit code: 255
== value.conf
value.conf line 2: unsupported option "maybe".
exit code: 255
== algo.conf
Unsupported KEX algorithm "curve25519-sha265"
algo.conf line 2: Bad SSH2 KexAlgorithms 'curve25519-sha265'.
exit code: 255
== weak.conf
exit code: 0

Misspelled keywords, invalid values and unknown algorithm names are caught. The last file is the warning: it re-enables a weak SHA-1 key exchange that OpenSSH disables by default, and sshd -t is perfectly happy with it. A syntax check is not a policy check; that is what reviewing sshd -T output (or a scanner such as ssh-audit) is for. OpenSSH 9.x defaults are already modern (Curve25519 and post-quantum hybrid key exchange, ChaCha20/AES-GCM ciphers), so on a current server you rarely need to list algorithms yourself; when compliance asks, check with sshd -T | grep -E '^(kexalgorithms|ciphers|macs)'.

Applying it without locking yourself out

bash
$ sudo sshd -t && sudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin|allowgroups) '
passwordauthentication no
permitrootlogin no
allowgroups ssh-users
$ id -nG ada | grep -w ssh-users     # are YOU in the allowed group?
ada sudo ssh-users
$ sudo systemctl reload ssh          # existing sessions stay open
$ # now, from a SECOND terminal, before closing the first:
$ ssh -o PreferredAuthentications=password ada@web1
ada@web1: Permission denied (publickey).
$ ssh ada@web1 true && echo "key login still works"
key login still works

reload re-reads the config for new connections and leaves running sessions alone, which is what makes the "second terminal" test safe. On Ubuntu 24.04 the service is ssh.service, socket-activated by ssh.socket by default; with socket activation, Port and ListenAddress changes need systemctl daemon-reload and systemctl restart ssh.socket.

Go further: Insecure Remote Administration shows how exposed SSH and RDP are found in an assessment and how they are fixed.

Test yourself

/etc/ssh/sshd_config says PasswordAuthentication no, but passwords still work. What is the most likely cause?

  1. sshd wasn't reloaded with the right signal
  2. An included drop-in (e.g. 50-cloud-init.conf) sets it to yes, and in sshd_config the first value wins
  3. Passwords can't be disabled
  4. PAM ignores sshd_config

What is the difference between sshd -t and sshd -T?

  1. None
  2. -t only checks the syntax; -T also prints the full effective configuration
  3. -T restarts sshd
  4. -t is for clients

Why put Match blocks at the end of sshd_config?

  1. They are faster there
  2. A Match block lasts until the next Match or the end of the file, so global settings after it would only apply to matching connections
  3. sshd ignores them elsewhere
  4. It is required by sshd -t