SSH Hardening on Ubuntu 24.04: A Tested sshd_config (2026)

SSH hardening on Ubuntu 24.04 with a drop-in sshd_config tested on OpenSSH 9.6: why cloud-init can undo your edits and how to verify with sshd -T.

13 min read

You set PasswordAuthentication no, reload sshd, and a password login still works. On Ubuntu 24.04 that is not a bug in OpenSSH. It is the way the config files are stacked, and it catches people who copy a generic SSH hardening checklist onto a cloud VM.

This guide is SSH hardening for Ubuntu 24.04 specifically. Every setting below was checked against the openssh-server package that 24.04 ships (1:9.6p1-3ubuntu13.20), using sshd -t and sshd -T on a test config, plus real logins against a throwaway sshd on a spare port. Where a setting from newer OpenSSH guides does not exist on 9.6, we say so.

Quick Summary: What You’ll Learn

  • A complete drop-in hardening file for /etc/ssh/sshd_config.d/, with the sshd -T output that proves each line took effect.
  • Why PasswordAuthentication no in sshd_config can lose to 50-cloud-init.conf, and how file naming fixes it.
  • Which settings matter on 9.6, and which ones from newer guides (PerSourcePenalties) make sshd refuse to start.
  • How to change crypto without dropping the post-quantum hybrid key exchange.
  • A rollout order that does not lock you out, including the ssh.socket detail for port changes.

How do you harden SSH on Ubuntu 24.04?

Put your settings in one file at the front of /etc/ssh/sshd_config.d/, check it with sshd -t, confirm the effective values with sshd -T, reload, and test from a second session before closing the first. Here is the file we tested:

bash

# /etc/ssh/sshd_config.d/00-hardening.conf
# Authentication
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
AuthenticationMethods publickey
MaxAuthTries 3
LoginGraceTime 30
MaxStartups 10:30:60
AllowGroups sshusers

# Forwarding and tunnels
X11Forwarding no
AllowAgentForwarding no
AllowTcpForwarding no
PermitTunnel no

# Dead and idle connections
ClientAliveInterval 300
ClientAliveCountMax 2
ChannelTimeout session:*=30m
UnusedConnectionTimeout 5m

# Logging and key exchange
LogLevel VERBOSE
KexAlgorithms -diffie-hellman-group14-sha256,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521

# Exceptions go last
Match Address 10.0.0.0/8
    AllowTcpForwarding local

Do not paste it as is. AllowGroups sshusers locks out every account that is not in a group called sshusers, and the forwarding lines break workflows that depend on ssh -L or agent forwarding. The sections below explain each block so you can decide what to keep.

After saving it, run the syntax check, then ask sshd what it will actually use:

bash

sudo sshd -t
sudo sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|kbdinteractiveauthentication|authenticationmethods|maxauthtries|allowgroups|x11forwarding|allowtcpforwarding|channeltimeout|kexalgorithms)'

On our test config the second command printed passwordauthentication no, authenticationmethods publickey, maxauthtries 3, allowgroups sshusers, channeltimeout session:*=30m and the trimmed key exchange list. sshd -T is the step most checklists skip, and it is the only one that tells you the truth, because it prints the merged result of every file sshd reads.

Why does PasswordAuthentication no not work on Ubuntu?

Two rules combine. First, from sshd_config(5): “for each keyword, the first obtained value will be used.” Second, Ubuntu’s /etc/ssh/sshd_config starts with Include /etc/ssh/sshd_config.d/*.conf, so everything in that directory is read before the main file. Ubuntu’s own server documentation spells out the consequence: settings in sshd_config.d/ override the ones in the main file.

Now add cloud-init. Many cloud images write /etc/ssh/sshd_config.d/50-cloud-init.conf from the ssh_pwauth setting in user data, and when that is true the file contains PasswordAuthentication yes. That line is read first, so your PasswordAuthentication no further down in sshd_config is ignored.

We reproduced this with three configs on OpenSSH 9.6p1, checking the effective value with sshd -T:

  • 50-cloud-init.conf says yes, main sshd_config says no: effective value yes.
  • 10-hardening.conf says no, 50-cloud-init.conf says yes: effective value no.
  • 99-hardening.conf says no, 50-cloud-init.conf says yes: effective value yes.

Files in the directory are processed in lexical order, and the first value wins, so a hardening file has to sort before the file it overrides. That is why the file above is named 00-hardening.conf. The common advice to name your override 99-something.conf comes from tools where the last value wins; for sshd it is backwards.

Two follow-ups. Check what is already in the directory with ls /etc/ssh/sshd_config.d/ and grep -r . /etc/ssh/sshd_config.d/ before writing anything, since cloud images often ship more than one file. And if your provider re-runs cloud-init, setting ssh_pwauth: false in user data fixes the source rather than out-sorting it.

Which sshd_config settings actually matter?

Authentication: keys only, no root, few tries

PasswordAuthentication no and KbdInteractiveAuthentication no together remove password prompts. The second one matters because with UsePAM yes (Ubuntu’s default), PAM can ask for a password through keyboard-interactive auth, and the man page says you should disable one or the other. Ubuntu already sets KbdInteractiveAuthentication no in the main file, but being explicit in your drop-in means it survives someone editing sshd_config.

AuthenticationMethods publickey makes the intent explicit: only public key auth can complete a login. With this config, a client that refuses to offer keys gets exactly this:

bash

$ ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password,keyboard-interactive user@host
user@host: Permission denied (publickey).

The list in parentheses is what the server offers. If it still shows password or keyboard-interactive, a file somewhere is overriding you; go back to sshd -T.

PermitRootLogin defaults to prohibit-password on 9.6, which already blocks root password logins. Set it to no if every admin has a named account with sudo, which also gives you per-person entries in the auth log. Keep prohibit-password if automation logs in as root with a key, and treat that key accordingly. For key generation itself, see our ssh-keygen best practices.

MaxAuthTries 3 cuts the default of 6. One side effect: every key your agent offers counts as an attempt, so a laptop with five keys loaded can hit “Too many authentication failures” before the right key is tried. Pin the key per host with IdentityFile and IdentitiesOnly yes on the client; our ssh-agent guide covers that.

AllowGroups sshusers is the strongest line in the file and the most dangerous. When we connected as a user outside the group, the client saw a plain Permission denied and sshd logged:

bash

User root from 127.0.0.1 not allowed because none of user's groups are listed in AllowGroups

Create the group and add yourself before enabling it: sudo groupadd sshusers, sudo usermod -aG sshusers $USER, then check with id -nG <user> from a fresh login.

Connection limits

LoginGraceTime 30 drops connections that have not authenticated within 30 seconds (default 120). MaxStartups 10:30:60 controls unauthenticated connections: from 10 onward sshd refuses new ones with 30% probability, rising linearly until it refuses all at 60. The default on 9.6 is 10:30:100. These limit how many half-open connections a scanner can hold, not how many guesses it gets.

Forwarding: honest about what it buys

X11Forwarding no, AllowAgentForwarding no, AllowTcpForwarding no and PermitTunnel no shrink what an authenticated session can do. The man page is blunt about the limits: disabling agent or TCP forwarding “does not improve security unless users are also denied shell access, as they can always install their own forwarders.” So these settings are about removing defaults you do not use, not about stopping a determined user with a shell. Agent forwarding is the one with a real exposure for the person connecting; our post on agent forwarding risks explains why ProxyJump is usually the better tool.

If you need exceptions, use a Match block at the end of the file, like the Match Address 10.0.0.0/8 above. Check it with -C, which evaluates the config for a given connection:

bash

sudo sshd -T -C user=deploy,host=x,addr=10.1.2.3 | grep allowtcpforwarding
sudo sshd -T -C user=deploy,host=x,addr=192.168.1.5 | grep allowtcpforwarding

In our test the first printed allowtcpforwarding local and the second allowtcpforwarding no. Two details we confirmed: a Match block inside an included file ends at the end of that file, so it does not swallow the rest of sshd_config; and DisableForwarding yes overrides every forwarding option, including ones set in a Match block, so use the individual options if you want exceptions.

Idle and dead sessions

ClientAliveInterval 300 with ClientAliveCountMax 2 makes sshd probe a silent client every 5 minutes and drop it after two missed replies. That cleans up sessions from laptops that went to sleep. It does not close an idle but healthy connection, because the client answers the probes.

That is what the two options added in OpenSSH 9.2 are for. ChannelTimeout session:*=30m closes session channels with no traffic for 30 minutes. Use the session:* form from the man page; the channel types are session:shell, session:command and so on. UnusedConnectionTimeout 5m closes connections that have no open channels at all. The man page warns that closing a session channel does not guarantee its processes stop, so long jobs belong in tmux or a systemd unit anyway. If your team runs long interactive sessions, raise the timeout or skip it.

Logging

LogLevel VERBOSE adds lines that INFO does not have. In our test, VERBOSE logged a Connection from ... port ... line for every connection, and Accepted key ED25519 SHA256:... found at <file>:<line>, which tells you which line of which authorized_keys file let someone in. On a server with several keys per account, that line is what you want during an incident review. Do not go to DEBUG: the man page says it “violates the privacy of users and is not recommended.”

Should you change KexAlgorithms, Ciphers and MACs?

Mostly no. The 9.6 defaults already exclude SHA-1 key exchange and CBC ciphers. What you can do safely is remove entries with the - prefix instead of replacing the whole list. The line in our file removes the NIST curves and the 2048-bit group, and sshd -T showed what is left:

bash

kexalgorithms [email protected],curve25519-sha256,[email protected],diffie-hellman-group-exchange-sha256,diffie-hellman-group16-sha512,diffie-hellman-group18-sha512

The first entry is the reason to use -. [email protected] is the post-quantum hybrid that 9.6 offers by default, and hardening guides that paste a full KexAlgorithms list from a few years ago often drop it without anyone noticing. Note that 9.6 does not have mlkem768x25519-sha256, which became the default in OpenSSH 10.0, so do not copy it from a newer guide either. If you are seeing post-quantum warnings from clients, that is a separate topic from server hardening.

Before trimming anything, check your oldest clients. Embedded devices and old libraries sometimes support only the NIST curves.

What about PerSourcePenalties and fail2ban on Ubuntu 24.04?

PerSourcePenalties arrived in OpenSSH 9.8 and is on by default there: sshd penalises addresses that repeatedly fail to authenticate. Ubuntu 24.04’s 9.6 does not have it, and adding it to the config does not just get ignored:

bash

/etc/ssh/sshd_config.d/00-h.conf: line 1: Bad configuration option: PerSourcePenalties
/etc/ssh/sshd_config.d/00-h.conf: terminating, 1 bad configuration options

sshd -t exits 255 on that. On 24.04 the ssh.service unit runs sshd -t before starting and before reloading, so a reload fails instead of taking down the daemon, but a guide that skips sshd -t and goes straight to a restart is how people lose a server. The same thing happens with a typo: PasswordAuthentcation no produced the identical “Bad configuration option” error.

For rate limiting on 9.6, use the firewall. ufw limit ssh/tcp adds a rule that, per the ufw man page, denies connections from an address that tries to open 6 or more within 30 seconds. fail2ban works too and can ban across services. With keys only, these mostly reduce log noise and load; they are not what keeps attackers out.

How do you apply the changes without locking yourself out?

  • Open two sessions to the server. Do all the work in one; leave the other alone until the end.
  • Make sure your user is in AllowGroups and that key login works for it.
  • Write /etc/ssh/sshd_config.d/00-hardening.conf, then run sudo sshd -t and fix every error.
  • Run sudo sshd -T and check each value you set, especially passwordauthentication.
  • Apply with sudo systemctl reload ssh. On 24.04 the unit’s reload runs sshd -t and then sends SIGHUP, so existing sessions stay up.
  • From your laptop, open a new connection with your key, then try the password-only command from earlier and expect Permission denied (publickey).
  • Only now close the spare session.

If you have a provider console (serial console or web console), know where it is before step 5.

Changing the port on Ubuntu 24.04

Ubuntu 24.04 starts sshd through socket activation: ssh.socket listens on port 22, and Ubuntu’s announcement says the port and address settings are pulled from the sshd config by a systemd generator. Editing Port and reloading sshd is not enough. You need:

bash

sudo systemctl daemon-reload
sudo systemctl restart ssh.socket

Open the new port in the firewall first, confirm with ss -tlnp that it is listening, and test a login on it before removing port 22. A different port reduces log noise from scanners; it is not a control that stops a targeted attempt, so it belongs at the end of this list, not the start. For the “is this a firewall or is sshd not listening” question, see connection refused vs timeout.

Where does termique fit?

Hardening one server is the easy part. The work is keeping ten of them consistent and checking them again after an image update or a cloud-init run.

termique doesn’t change your server config; it makes the checking cheaper. Save the sshd -T | grep -Ei ... line from this guide as a snippet and run it from the snippet palette in any session, which shows you the full command before it runs. Groups let you open sessions to every host in a group at once, and split panes keep a spare session open next to the one you are working in, which is step 1 of the rollout above. Your SSH passwords and private keys are end-to-end encrypted before they sync between devices, so the sync server only stores ciphertext; hosts, groups and snippets sync as plaintext metadata. Details are on our security page, and the wider picture (key rotation, bastions, monitoring) is in the SSH security best practices guide.

FAQ

Does sshd_config.d override sshd_config on Ubuntu?

Yes. Ubuntu’s sshd_config includes /etc/ssh/sshd_config.d/*.conf on its first line, and sshd keeps the first value it reads for most keywords. Anything set in a drop-in wins over the same keyword later in the main file.

Why is PasswordAuthentication still yes after I set it to no?

Usually a file in /etc/ssh/sshd_config.d/, often 50-cloud-init.conf, sets it to yes and is read first. Run sudo sshd -T | grep passwordauthentication to see the effective value, then put your setting in a file that sorts earlier, such as 00-hardening.conf.

Does Ubuntu 24.04 support PerSourcePenalties?

No. It was added in OpenSSH 9.8, and 24.04 ships 9.6p1. Adding it makes sshd -t fail with “Bad configuration option”. Use MaxStartups, ufw limit or fail2ban instead.

Should I disable root login on Ubuntu?

Root password login is already blocked by the default prohibit-password. Set PermitRootLogin no if everyone has a named account with sudo; keep prohibit-password only if automation needs root over a key.

How do I change the SSH port on Ubuntu 24.04?

Set Port in the sshd config, then run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket, because 24.04 uses socket activation. Open the port in your firewall before restarting and test it before you close port 22.

If you manage more servers than you want to check by hand, try termique free: saved check commands, grouped hosts, and credentials encrypted before they sync.