Reference · Network exposure
SSH password sign-in: check that PasswordAuthentication is really off
Check whether SSH on port 22 still accepts passwords: read the effective PasswordAuthentication, KbdInteractiveAuthentication and PermitRootLogin with sshd -T, override 50-cloud-init.conf and test from another computer.
| Target artifact | The OpenSSH server settings PasswordAuthentication, KbdInteractiveAuthentication and PermitRootLogin, read from /etc/ssh/sshd_config and the files in /etc/ssh/sshd_config.d/, for SSH on TCP port 22 or the port you chose. |
|---|---|
| Default behaviour | OpenSSH allows password authentication: PasswordAuthentication defaults to yes. Root cannot use a password under the default PermitRootLogin prohibit-password. Ubuntu Server 24.04 installed from the ISO image can carry 50-cloud-init.conf with PasswordAuthentication yes, which wins over a no in the main file. |
| Safe state | sshd -T shows passwordauthentication no, kbdinteractiveauthentication no and permitrootlogin no, or prohibit-password where a tool must sign in as root with a key; a password attempt from another computer gets Permission denied (publickey). |
| Check on Linux | sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) ' |
| Check from another device | ssh -o PubkeyAuthentication=no -o PreferredAuthentications=keyboard-interactive,password USER@SERVER_IP |
| Fix on Linux | put PasswordAuthentication no and KbdInteractiveAuthentication no in /etc/ssh/sshd_config.d/00-hardening.conf, then run sudo sshd -t and sudo systemctl restart ssh |
Read the result
sshd -T prints the configuration that applies once every file has been read. It reads the files itself; it does not ask the running service. It needs the folder /run/sshd, which Ubuntu's ssh.service creates when it starts and removes when it stops: while the service is not running, as on Ubuntu 24.04 before the first connection has started it, sshd -T and sshd -t stop with Missing privilege separation directory: /run/sshd. Without sudo it stops with sshd: no hostkeys available -- exiting.
sshd -T line | Meaning |
|---|---|
passwordauthentication yes | Accounts with a password can sign in with it; for root, permitrootlogin decides |
kbdinteractiveauthentication yes | If sshd -T also shows usepam yes, the server can still ask for the account password through PAM |
permitrootlogin yes | root can sign in, with its password while password sign-in is on |
permitrootlogin prohibit-password or without-password | root needs a key; OpenSSH 9.6 on Ubuntu 24.04 prints the default as without-password, the deprecated name for prohibit-password |
permitrootlogin no | root cannot sign in over SSH at all |
OpenSSH's manual explains why both settings matter: with PAM, keyboard-interactive authentication usually serves the same role as password authentication. Turning off only PasswordAuthentication can leave password sign-in working: in a test on Ubuntu 24.04, an account signed in with its password through keyboard-interactive. Ubuntu's main sshd_config already sets KbdInteractiveAuthentication no, so this applies where that line was changed or removed, or a file in sshd_config.d turns it back on.
The test from another computer shows what the server offers. If that computer has never connected, ssh first asks you to confirm the server's host key.
| Reply | Meaning |
|---|---|
Permission denied (publickey). with no prompt | Password sign-in is off |
A password prompt, or password in the list in brackets | Password sign-in is on: press Ctrl+C |
keyboard-interactive in the list | The server can ask for the password through PAM |
The list in brackets does not reflect PermitRootLogin: under the default, it can show password for root even though root's password is refused. Read root's setting from sshd -T.
Why a no in sshd_config is not enough
- For each setting, OpenSSH uses the first value it reads.
- Ubuntu's
/etc/ssh/sshd_configincludes/etc/ssh/sshd_config.d/*.confat the top, and the files are read in name order. APasswordAuthentication yesin50-cloud-init.confis read before anofurther down the main file, so theyesapplies. - Ubuntu Server 24.04 installed from the ISO image gets
/etc/ssh/sshd_config.d/50-cloud-init.confwith the single linePasswordAuthentication yes, from the installer's default. Cloud images can carry the same file: a Launchpad report shows it on a 22.04 cloud image set up with cloud-init'sssh_pwauth: true. - A
Matchblock overrides the global settings for the connections it matches, until the nextMatchline or the end of the file.
Find every active setting and every Match block:
sudo grep -rinE '^\s*(passwordauthentication|kbdinteractiveauthentication|challengeresponseauthentication|permitrootlogin|match)\b' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/ChallengeResponseAuthentication is the deprecated name of KbdInteractiveAuthentication; a line with either name sets it.
Turn password sign-in off
Stop Keep a way back in
- Confirm that you can sign in with a key before you turn passwords off. Guide 5 shows how to set it up.
- Keep your current session open until a new session has signed in.
- Make sure you can reach the server's console through your hosting provider's web panel.
Ubuntu 22.04 and 24.04
- Create a file that sorts before
50-cloud-init.conf:
sudo nano /etc/ssh/sshd_config.d/00-hardening.confPasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin noPermitRootLogin no refuses root even with a key: use it only when another account can sign in with a key and use sudo. Use PermitRootLogin prohibit-password instead of no where a tool signs in as root with a key, as the nodes of a Proxmox VE cluster do: see Proxmox VE port 8006.
- Check the files, confirm the result and apply it:
sudo sshd -t
sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '
sudo systemctl restart sshsshd -tprints nothing when the files are valid. If it prints an error, fix the file before you restart: Ubuntu's SSH service runs the same check when it starts, and does not start while the check fails. Restarting the SSH service does not drop sessions that are already open. Before you close anything, run the test from another computer, then sign in with your key in a new session: the test alone does not show that your key still works.
A Match block turns passwords back on for some addresses
sshd -T alone shows the global settings. Add the details of a connection to see what applies to it:
sudo sshd -T -C user=USER,host=CLIENT_NAME,addr=CLIENT_IP | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) 'If the result differs from the global one, a Match block applies. Change it, or keep it only if you mean passwords to work from those addresses.
Socket activation on Ubuntu 22.10 and later
Since Ubuntu 22.10, including 24.04, ssh.socket listens on the port and the SSH service starts only when the first connection arrives. The password settings above are read by sshd itself, so the check and the fix are the same as on 22.04.
The port is different. On 24.04, a generator turns the Port setting into the socket's configuration, so a port change needs sudo systemctl daemon-reload and sudo systemctl restart ssh.socket. Guide 5 has the full steps. If you test on another port, add -p PORT to the ssh command.
Troubleshooting
sshd -T shows no, but the test still gets a password prompt
- The service was not restarted after the change:
sudo systemctl restart ssh. - A
Matchblock applies to the address you tested from: runsshd -T -Cwith that address. - The test reached another server, or another port.
Locked out after the change
Use the console in your hosting provider's web panel. Set the line back to PasswordAuthentication yes in 00-hardening.conf, run sudo systemctl restart ssh, set up key sign-in, then turn passwords off again.
Related guides and lookups
Sources
- Ubuntu manual: sshd_config, 22.04
- OpenBSD manual: sshd, the -t, -T and -C options
- Ubuntu manual: ssh_config, 24.04
- Launchpad bug 1873528: order of sshd_config.d files
- Ubuntu bug mail: bug 2088207, cloud-init enables SSH password authentication in an unexpected config file
- Launchpad bug 2049860, comment 15: 50-cloud-init.conf on a 22.04 cloud image
- Ubuntu bug mail: bug 2107356, sshd -t fails while the SSH service is not running
- Ubuntu Discourse: sshd uses socket-based activation on 22.10 and later
- Ubuntu 24.04 release notes: SSH socket activation
- Launchpad bug 2069041: changing the SSH port on 24.04