Sub topic 12
Disabling password logins on the server
Key-based login now works for you, so the server no longer needs to accept passwords. This chapter shows how to turn password authentication off, check that the change took effect, and avoid locking yourself out.
It assumes you can already log in with your key, as set up in the earlier chapters. It also assumes you have sudo on the server and know how to edit a root-owned file with nano or vim.
Why turn passwords off
An SSH server on the public internet receives password guesses all day. Each guess is a chance of success if any account has a weak password. A key pair removes that risk. The server never accepts a secret you can type, so there is nothing to guess, and the private key never leaves your machine.
Disabling passwords does not make the server immune to attack. It closes one common route. Log noise from failed guesses also drops, because the server rejects password attempts immediately instead of checking them.
Which settings control password logins
OpenSSH has more than one way to ask for a password. Turn off all of them.
| Setting | What it controls | Value to use |
|---|---|---|
| `PubkeyAuthentication` | Logins with a key pair | `yes` |
| `PasswordAuthentication` | Plain password prompts | `no` |
| `KbdInteractiveAuthentication` | Keyboard-interactive prompts, which PAM can use to ask for a password | `no` |
On OpenSSH older than 8.7, the last setting is named ChallengeResponseAuthentication. If you only set PasswordAuthentication no, a server with UsePAM yes can still ask for a password through the keyboard-interactive method. That is the most common reason people think they disabled passwords and later find they didn't.
PermitRootLogin is related. Modern OpenSSH defaults to prohibit-password, which allows root to log in with a key but not a password. You can leave that default or set no if you never log in as root.
A safe procedure
The main risk is closing the only door you have. Follow the order below. The order matters more than any single command.
1. Confirm key login works first
Open a new terminal and log in with only your key. This is the check from the troubleshooting chapter.
ssh -v -o PreferredAuthentications=publickey -o PasswordAuthentication=no [email protected]If this does not succeed, stop. Fix key login before touching the server configuration.
2. Keep a working session open
Leave one logged-in session open for the whole procedure. An established SSH connection survives a reload of sshd, so it is your way back in if the new settings are wrong. Do not close it until the last step is done.
3. Edit the configuration
The main file is /etc/ssh/sshd_config. Open it with sudo.
sudo nano /etc/ssh/sshd_configFind each of the three settings. They are often present but commented out with #, or set to yes. Change them so they read:
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication noIf a line appears twice, OpenSSH uses the first one it reads and ignores later duplicates. Remove or fix every copy so the file does not contradict itself.
4. Check for drop-in files that override you
Many current distributions, including Ubuntu 22.04 and later, Debian 12 and Fedora, put this line near the top of sshd_config:
Include /etc/ssh/sshd_config.d/*.confFiles in that directory are read in alphabetical order, and because the first value wins, a drop-in can silently override the main file. Cloud images often ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes. List what is there:
ls /etc/ssh/sshd_config.d/
grep -ri 'PasswordAuthentication' /etc/ssh/sshd_config /etc/ssh/sshd_config.d/You have two choices. You can edit the offending drop-in, or you can add your own file whose name sorts earlier, so your values are read first:
sudo tee /etc/ssh/sshd_config.d/00-keys-only.conf > /dev/null <<'EOF'
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
EOFThe second choice is more durable, because a package or cloud-init update may rewrite the file you edited.
If your sshd_config has no Include line, the directory is ignored and only the main file matters.
5. Check the syntax, then reload
Run the syntax check before reloading. A typo in the configuration can stop sshd from restarting, which would block every new login.
sudo sshd -t && sudo systemctl reload sshdsshd -t prints nothing when the file is valid. The && means the reload only runs if the check passes. On Debian and Ubuntu the service is named ssh, so use sudo systemctl reload ssh there.
6. Check the settings `sshd` actually uses
Reading the file is not proof. Ask sshd for its effective configuration, which includes every drop-in and default:
sudo sshd -T | grep -Ei '^(pubkeyauthentication|passwordauthentication|kbdinteractiveauthentication|permitrootlogin) 'Output keys are lowercase. You want this result:
permitrootlogin without-password
pubkeyauthentication yes
passwordauthentication no
kbdinteractiveauthentication nowithout-password is the older name for prohibit-password, and some versions print one name or the other. On OpenSSH older than 8.7, look for challengeresponseauthentication no instead of the keyboard-interactive line.
7. Test from a new session
Do not close your safety session. From another terminal, log in normally:
ssh [email protected]This should succeed using your key. Then check that passwords are really refused, by telling the client to skip keys:
ssh -o PreferredAuthentications=password -o PubkeyAuthentication=no [email protected]A correct result is an immediate failure with no password prompt:
[email protected]: Permission denied (publickey).If you are still asked for a password, one of the settings did not take effect. Go back to step 4 and step 6.
Exceptions with Match blocks
Sometimes you need an exception, for example a service account on an internal network that cannot use keys. A Match block applies settings only to connections that fit a condition:
PasswordAuthentication no
Match Address 10.0.5.0/24
PasswordAuthentication yesEverything after a Match line belongs to that block until the next Match or the end of the file, so put these blocks at the very end. Treat each exception as a deliberate hole. It brings back the risk you just removed for that group of connections.
What this does not change
- Disabling password logins affects SSH only. Console logins,
suandsudostill use account passwords. - A user with no password set cannot use password-based
sudo. Plan for this before you lock the account down. Either keep a password forsudo, or configuresudorules deliberately. - Existing sessions continue after the reload. Stale sessions that authenticated with a password stay open until they end.
- Anyone whose public key is in an
authorized_keysfile can still log in. Review those files, as covered in the key management chapter.
Recovering from a lockout
If you closed your last session and the new settings block you, you need another way in.
- Use the provider's web console or serial console on a cloud server, or a physical or virtual console on your own hardware.
- Log in there with the account password. Console logins do not use
sshd. - Restore the old values, run
sudo sshd -t, and reload the service.
Check ahead of time that you can reach the console. Finding out after a lockout that the console is not available is the worst time to learn it.
Common errors
- Still prompted for a password after the change. A drop-in file is overriding you,
KbdInteractiveAuthenticationis stillyes, or the service was never reloaded. Check withsshd -T. sshd -treports "Bad configuration option". The option name does not exist in your OpenSSH version, often because of theChallengeResponseAuthenticationrename. Checkssh -V.- Reload says the unit is not found. The service is
sshon Debian and Ubuntu andsshdon most other distributions. - Key login fails after the reload. The setting did not cause it. The key was never working. Return to the permissions and troubleshooting chapters, and use
sudo journalctl -u sshor/var/log/auth.logon the server to see why the key was rejected.
Exercise
You set PasswordAuthentication no in /etc/ssh/sshd_config and reloaded sshd, but the server still asks for a password. List two likely causes and one command that would show the real situation.
Show answer
Likely causes: (1) a file in /etc/ssh/sshd_config.d/ such as 50-cloud-init.conf sets PasswordAuthentication yes and is read first, so its value wins; (2) KbdInteractiveAuthentication (or ChallengeResponseAuthentication on older OpenSSH) is still yes with UsePAM yes, so the password is requested through the keyboard-interactive method. The command sudo sshd -T | grep -Ei '^(passwordauthentication|kbdinteractiveauthentication) ' shows the values the server is actually using.
Exercise
Why do you run "sudo sshd -t && sudo systemctl reload sshd" instead of restarting without a check, and why should you keep your current session open while testing?
Show answer
sshd -t validates the configuration, and the && stops the reload if it fails, so a typo cannot leave the service unable to accept new logins. An established session survives a reload, so keeping it open gives you a way to revert the file if a new login fails.