SaferComputing.com

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.

Published
Updated
Last checked
Applies to
OpenSSH server on Ubuntu 22.04 LTS and 24.04 LTS, and other Linux systems using OpenSSH
Technical specification
Target artifactThe 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 behaviourOpenSSH 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 statesshd -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 Linuxsudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '
Check from another devicessh -o PubkeyAuthentication=no -o PreferredAuthentications=keyboard-interactive,password USER@SERVER_IP
Fix on Linuxput 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 lineMeaning
passwordauthentication yesAccounts with a password can sign in with it; for root, permitrootlogin decides
kbdinteractiveauthentication yesIf sshd -T also shows usepam yes, the server can still ask for the account password through PAM
permitrootlogin yesroot can sign in, with its password while password sign-in is on
permitrootlogin prohibit-password or without-passwordroot needs a key; OpenSSH 9.6 on Ubuntu 24.04 prints the default as without-password, the deprecated name for prohibit-password
permitrootlogin noroot 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.

ReplyMeaning
Permission denied (publickey). with no promptPassword sign-in is off
A password prompt, or password in the list in bracketsPassword sign-in is on: press Ctrl+C
keyboard-interactive in the listThe 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_config includes /etc/ssh/sshd_config.d/*.conf at the top, and the files are read in name order. A PasswordAuthentication yes in 50-cloud-init.conf is read before a no further down the main file, so the yes applies.
  • Ubuntu Server 24.04 installed from the ISO image gets /etc/ssh/sshd_config.d/50-cloud-init.conf with the single line PasswordAuthentication 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's ssh_pwauth: true.
  • A Match block overrides the global settings for the connections it matches, until the next Match line or the end of the file.

Find every active setting and every Match block:

Ubuntu, as a user with sudo
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
  1. Create a file that sorts before 50-cloud-init.conf:
Ubuntu, as a user with sudo
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf
Contents of /etc/ssh/sshd_config.d/00-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no

PermitRootLogin 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.

  1. Check the files, confirm the result and apply it:
Ubuntu, as a user with sudo
sudo sshd -t
sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '
sudo systemctl restart ssh
  1. sshd -t prints 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:

Ubuntu, as a user with sudo
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 Match block applies to the address you tested from: run sshd -T -C with 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.

Sources