How sshd reads its settings
On Ubuntu 22.04 and later, the main file /etc/ssh/sshd_config includes a directory on its first line:
Include /etc/ssh/sshd_config.d/*.conf
Files from the directory are read in alphabetical order, then the rest of the main file. For most settings
sshd uses the first value it finds. Two rules follow:
- your settings go in a separate file in
sshd_config.d/, and the main file stays untouched: on a package upgradeaptwon’t ask whose version to keep; - your file’s name must sort before the others. Servers built from a provider’s image almost always have
50-cloud-init.confthere, often withPasswordAuthentication yes. Ours is00-hardening.conf.
sudo sshd -T shows what ends up in effect: it prints every setting with all files applied. It’s the only
sure way to know — reading the files isn’t enough.
The file, block by block
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
The heart of the file. With no password, guessing is useless: a bot is refused before any password check. KbdInteractiveAuthentication is a second path to a password through PAM; Ubuntu already turns it off in the main file, but write it out so you don’t depend on other files. Apply only once key login is tested (SSH keys).
default:PasswordAuthentication yesKbdInteractiveAuthentication yes (no on Ubuntu)PubkeyAuthentication yes
PermitRootLogin no
Root doesn’t log in at all, not even with a key. Work as your own user with sudo: the log shows who did what, and a leaked key doesn’t hand over everything at once. The default prohibit-password lets root in with a key.
default prohibit-password
AllowUsers example-user
Only the listed users get in. System users and accidental ones (ubuntu from the image, a user created by a package) won’t log in even if they end up with a key. A typo here locks everyone out — test from a second window (Keep the old session open).
default all users
MaxAuthTries 3
LoginGraceTime 30
Less time and fewer tries per connection. MaxAuthTries 3: after three wrong keys the connection drops — if the client’s agent holds more keys, you need IdentitiesOnly yes (~/.ssh/config). LoginGraceTime 30: 30 seconds to log in instead of two minutes, so half-open bot connections don’t hang around.
default:MaxAuthTries 6LoginGraceTime 120
X11Forwarding no
AllowAgentForwarding no
X11 forwarding is pointless on a server without graphics; Ubuntu’s main file turns it on. The server allows agent forwarding by default — turn it off: with it on, root on the server can use your agent while the session is open (ssh-agent).
default:X11Forwarding no (yes on Ubuntu)AllowAgentForwarding yes
ClientAliveInterval 300
ClientAliveCountMax 2
Abandoned sessions close themselves: every 5 minutes the server checks on the client and drops the connection after two missed replies. Idle sessions stay up — the client answers as long as it’s alive.
default:ClientAliveInterval 0 — don’t checkClientAliveCountMax 3
AllowTcpForwarding noLeave port forwarding on: tunnels to closed admin panels depend on it (SSH tunnels). Turn it off if outside users log in to the server.
default yes
Port 48731Change the port only if log noise gets in the way (Port 22). On Ubuntu 24.04 and 26.04 this needs daemon-reload and an ssh.socket restart — see below. Open the new port in the firewall before restarting.
default 22
What’s not here and why
| Setting | Why it’s left out |
|---|---|
Protocol 2 |
protocol 1 has been gone since OpenSSH 7.4; sshd ignores the line |
UsePAM no |
Ubuntu relies on PAM for session accounting and motd; password login is closed anyway |
Ciphers, KexAlgorithms, MACs |
the default lists in OpenSSH 9.6 and 10.2 already exclude weak algorithms; hand-made lists go stale faster than anyone updates them |
PerSourcePenalties |
exists since OpenSSH 9.8 and is on by default: the server itself pushes back an address that keeps failing to log in. Works out of the box on Ubuntu 26.04; 24.04 (9.6) doesn’t have it — there CrowdSec stops the guessing (CrowdSec against SSH brute force) |
AllowGroups |
for when many people log in: create an ssh-users group, and a new person gets access with usermod -aG, no config edit. For one or two people AllowUsers is enough |
Apply it
Keep the current session open
Do every step in one session and test in a second, new one. A running session survives an
sshdrestart and any config error (Keep the old session open).Check the syntax
sudo sshd -t && echo ok # okOn an error,
sshdnames the file and the line:/etc/ssh/sshd_config.d/00-hardening.conf line 7: Bad configuration option: …. Nook— don’t go further.See what will take effect
sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|allowusers|maxauthtries) ' # permitrootlogin no # maxauthtries 3 # passwordauthentication no # kbdinteractiveauthentication no # allowusers example-userIf you see
passwordauthentication yes, a file read before yours sets it.Restart sshd
sudo systemctl restart sshOn Ubuntu the service is called
ssh, notsshd. A restart doesn’t touch open sessions: each has its own process — inpsit’ssshd: example-user@pts/0, and since OpenSSH 9.8,sshd-session.If you changed
PortorListenAddress, on Ubuntu 24.04 and 26.04 do this instead:sudo systemctl daemon-reload sudo systemctl restart ssh.socketThe port is held by
ssh.socket(systemd), andsshdstarts on the first connection. The systemd generator readsPortfromsshd_configondaemon-reload; without it the old port stays. Ifsystemctl is-enabled ssh.socketsaysdisabled, the provider has switched back to the classic startup — thenrestart sshis enough.Log in from a second window
A new terminal window, a new session — and only after it works, close the first one. The checks are below.
If it doesn’t work
The file says PasswordAuthentication no, but the server still asks for a password
- Cause
- A file earlier in alphabetical order has
PasswordAuthentication yes— most often50-cloud-init.conf. Insshdthe first value wins, so a line in99-…confor at the end ofsshd_configchanges nothing. - Fix
sudo grep -ri passwordauthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/shows every place. Name your file so it comes first (00-…), and check the effective value withsudo sshd -T | grep -i passwordauth.
After restarting sshd, new sessions can’t get in: Connection refused
- Cause
- A config error:
sshddidn’t start. The current session survives — it has its own process. - Fix
- In the open session:
sudo sshd -tnames the line; fix it, thensudo systemctl restart ssh. No session left — the provider console (Provider console).
sudo sshd -t says Missing privilege separation directory: /run/sshd
- Cause
sshdis started throughssh.socketand hasn’t run yet, so/run/sshddoesn’t exist:ssh.servicecreates it on start (RuntimeDirectory=sshd) and removes it on stop.- Fix
- The check itself has nothing to do with that directory:
sudo mkdir -p /run/sshd && sudo sshd -t.
Changed Port, but sshd still listens on 22
- Cause
- On Ubuntu 24.04 the port is held by
ssh.socket, i.e. systemd. It readsPortfrom the config only ondaemon-reload. - Fix
sudo systemctl daemon-reload && sudo systemctl restart ssh.socket; check withss -tlnp | grep -E ':(22|48731) '.
Root can still log in with a key
- Cause
- The default is
PermitRootLogin prohibit-password: root’s password is closed, but keys are allowed. - Fix
- Set
PermitRootLogin no, but only after your ownsudouser has logged in and runsudo -v(First login to a server).
Added AllowUsers — and nobody can log in
- Cause
- A typo in the name, or you left yourself out: with
AllowUsers, only the listed users get in. - Fix
- Fix the line from the open session; test a login from a second window before closing the first (Keep the old session open).
How to check
From your computer, in the second window. Passwords should no longer be accepted — force the client to try only a password:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password [email protected]
# [email protected]: Permission denied (publickey).
The parentheses list the login methods the server still offers. Only publickey means passwords are closed.
Root can’t log in, not even with a key:
ssh [email protected]
# [email protected]: Permission denied (publickey).
Your own login works:
ssh [email protected] 'echo ok'
# ok
What the bots see — on the server, an hour after applying. Even with ssh.socket, the sshd log goes
under ssh.service, so filter with -u ssh:
sudo journalctl -u ssh --since -1h | grep -c 'Failed password'
# 0