How do I disable SSH password login safely?
Put PasswordAuthentication no in a file in /etc/ssh/sshd_config.d/ whose name sorts first, run sudo sshd -t, then reload ssh. Keep your current session open and test a new login in a second window before you close it.
Last updated
Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b
container image), .
Set PasswordAuthentication no in a small file under /etc/ssh/sshd_config.d/. Check it with sudo sshd -t and reload sshd. Before you close the session you are in, open a second one and prove that your key still works. Do all of this only after key login works, as set up in the SSH keys article.
Check that your key works first
Passwords are your fallback right now. If the key does not work and you remove them, nothing is left. Log in with the key and nothing else:
$ ssh localhost 'echo key login works'
key login works
On a real server, run this from your own computer against the server’s address, and use the same user you plan to keep.
See what sshd uses now
sshd -T prints the settings sshd would use, after it has read every file. It needs root, because it loads the host keys:
$ sudo sshd -T | grep -iE '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin|pubkeyauthentication)'
permitrootlogin without-password
pubkeyauthentication yes
passwordauthentication yes
kbdinteractiveauthentication no
passwordauthentication yes is the setting to change. Three more lines matter:
permitrootlogin without-passwordis the old name ofprohibit-password, the default. Root may log in with a key but not with a password.pubkeyauthentication yesmust stay on, or nobody can log in.kbdinteractiveauthentication nois the other door. WithUsePAM yes, a password can also arrive as a keyboard-interactive answer, andsshd_config(5)says to disable one of the two. The default insshd_config(5)isyes; Ubuntu’s file sets it tono. If it showsyes, addKbdInteractiveAuthentication noto the same drop-in file below.
Find every place that sets it
Many cloud images ship a drop-in file that turns passwords on. Search the main file and the folder:
$ sudo grep -rn PasswordAuthentication /etc/ssh/sshd_config /etc/ssh/sshd_config.d/
/etc/ssh/sshd_config:66:#PasswordAuthentication yes
/etc/ssh/sshd_config:88:# PasswordAuthentication. Depending on your PAM configuration,
/etc/ssh/sshd_config:92:# PAM authentication, then enable this but set PasswordAuthentication
/etc/ssh/sshd_config.d/50-cloud-init.conf:1:PasswordAuthentication yes
The lines with # are comments. The last line is the one that counts: 50-cloud-init.conf sets yes. Ubuntu’s sshd_config starts by including every *.conf file of that folder, and for each keyword sshd uses the first value it reads. The files are read in alphabetical order.
Write the setting, and see it ignored
The natural name for a new file is 99-hardening.conf. Try it, with root login switched off as well:
$ printf 'PasswordAuthentication no\nPermitRootLogin no\n' | sudo tee /etc/ssh/sshd_config.d/99-hardening.conf
PasswordAuthentication no
PermitRootLogin no
$ sudo sshd -T | grep -iE '^(passwordauthentication|permitrootlogin)'
permitrootlogin no
passwordauthentication yes
PermitRootLogin took effect, because no earlier file set it. PasswordAuthentication did not, because 50-cloud-init.conf comes first. Rename the file so that it sorts before the others:
$ sudo mv /etc/ssh/sshd_config.d/99-hardening.conf /etc/ssh/sshd_config.d/10-hardening.conf
$ sudo sshd -T | grep -iE '^(passwordauthentication|permitrootlogin)'
permitrootlogin no
passwordauthentication no
Now both are no. Nothing has changed for running sshd yet. It reads the files only at start and on reload.
If you use Match blocks, remember that they can set the value again for some users or addresses. The -C option of sshd -T prints the settings as they apply to one user connecting from one address:
$ sudo sshd -T -C user=dev,addr=203.0.113.5 | grep -i '^passwordauthentication'
passwordauthentication no
Check the file before sshd reads it
A typo in a config file can stop sshd from starting, and that is the one change you cannot repair over SSH. sshd -t checks the syntax and exits with a status other than 0 if something is wrong. Here is what it says about a bad value:
$ printf 'PasswordAuthentication maybe\n' > /tmp/bad.conf
$ sudo sshd -t -f /tmp/bad.conf; echo "exit status: $?"
/tmp/bad.conf line 1: unsupported option "maybe".
exit status: 255
Your real configuration should print nothing and exit with 0:
$ sudo sshd -t && echo config ok
config ok
Reload, then test a second login
Run the reload from inside an SSH session. It proves the point that matters: the session you are in does not end.
$ ssh localhost 'sudo systemctl reload ssh; echo "reloaded, this session is still open"'
reloaded, this session is still open
On Ubuntu and Debian the service is called ssh. On most other systems it is sshd. sshd(8) says that sshd reads its configuration again on SIGHUP by starting itself again, which is what a reload sends. Sessions that are open stay open.
Now open a second terminal and test, without closing the first. First, a login that may only use a password. It must fail:
$ ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password localhost true
dev@localhost: Permission denied (publickey).
The server no longer offers passwords. The only method it names is publickey. Then check that the key still works:
$ ssh localhost 'echo key login still works'
key login still works
Only now close the old session.
How not to lock yourself out
Keep the first session open until the second login has worked. An open session does not need to log in again, so it stays your way back in when the new login fails.
Make sure the key is in authorized_keys of the account you use, and that the permissions are right (the SSH keys article shows the failure and the log line).
Turn off root login only when another account can use sudo. Otherwise you lose the way to administer the server. Sudo for one command shows a narrower alternative to full sudo.
Know your fallback. A provider console or a rescue mode is the way back in when everything else fails.
Mistakes people make
You put the file in the folder with a name that sorts last. sshd -T shows the value that counts, so always read it after the edit.
You edit the file and never reload. The old settings stay until sshd reads the files again.
You change PasswordAuthentication and leave keyboard-interactive on, on a system where it is yes. Check both lines.
You test the new setting from the session you are already in. That session logged in before the change and proves nothing about the next login.
Turning off passwords takes away one way to log in. It does not limit what a key can do after it logs in; for that, see safe rm on servers and the sudo article.
Questions
Does reloading sshd close my session?
No. A reload makes sshd read its configuration again. Connections that are already open stay open, and the new settings apply to the next login. That is why you keep one session open until a second login works.
Why does sshd -T still say passwordauthentication yes after I edited a file?
sshd uses the first value it finds for each keyword, and it reads the files in /etc/ssh/sshd_config.d/ in alphabetical order. Another file that sorts earlier, such as 50-cloud-init.conf, wins. Name your file so that it sorts first.
Should I set PermitRootLogin to no or prohibit-password?
prohibit-password, the default, lets root in with a key only. no blocks root completely. Choose no once another account can use sudo, so that you can still administer the server.
How do I undo it if I locked myself out?
Use the console of your hosting provider or the rescue mode of your machine, delete the drop-in file and reload ssh. Without console access, a locked-out server may not be recoverable, which is why the second login test matters.
References
- sshd_config(5) at man7.org, PasswordAuthentication, PermitRootLogin, KbdInteractiveAuthentication and UsePAM man7.org
- sshd(8) at man7.org, the -t, -T and -C options and SIGHUP man7.org
- sshd_config(5) on Ubuntu 24.04 manpages.ubuntu.com
- OpenSSH manual pages www.openssh.org
Related articles
The app is free.
Get notified at launchmacOS is coming to the Mac App Store. iOS and Android later.