# How do I disable SSH password login safely?

Turn off SSH password login with PasswordAuthentication no. Check the file with sshd -t and sshd -T, reload, and test a second login before you close anything.

Source: https://valakes.com/learn/disable-ssh-password-login/

Last updated: 2026-11-25

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.

Commands checked on Ubuntu 24.04.5 LTS (the ubuntu:24.04@sha256:562111b88caa79c71391c0c4d3c2114a7d5c35915c20561fd893cb1e07929e6b container image), 25 November 2026.

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](https://valakes.com/learn/ssh-keys/).

## 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:

```console
$ 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:

```console
$ 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-password` is the old name of `prohibit-password`, the default. Root may log in with a key but not with a password.
- `pubkeyauthentication yes` must stay on, or nobody can log in.
- `kbdinteractiveauthentication no` is the other door. With `UsePAM yes`, a password can also arrive as a keyboard-interactive answer, and `sshd_config(5)` says to disable one of the two. The default in `sshd_config(5)` is `yes`; Ubuntu's file sets it to `no`. If it shows `yes`, add `KbdInteractiveAuthentication no` to 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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:

```console
$ 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.

```console
$ 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:

```console
$ 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:

```console
$ 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](https://valakes.com/learn/ssh-keys/) 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](https://valakes.com/learn/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](https://valakes.com/learn/safe-rm-on-servers/) and [the sudo article](https://valakes.com/learn/sudo-for-one-command/).

## 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](https://man7.org/linux/man-pages/man5/sshd_config.5.html)
- [sshd(8) at man7.org, the -t, -T and -C options and SIGHUP](https://man7.org/linux/man-pages/man8/sshd.8.html)
- [sshd_config(5) on Ubuntu 24.04](https://manpages.ubuntu.com/manpages/noble/man5/sshd_config.5.html)
- [OpenSSH manual pages](https://www.openssh.org/manual.html)

## Related articles

- [How do I generate an SSH key and put it on a server?](https://valakes.com/learn/ssh-keys/): Run ssh-keygen -t ed25519, then ssh-copy-id -i ~/.ssh/id_ed25519.pub user@host. The private key stays on your computer, the public key goes into ~/.ssh/authorized_keys on the server, and you log in without a password.
- [How do I let a user run one command with sudo?](https://valakes.com/learn/sudo-for-one-command/): Write a rule that names the command with its full path and exact arguments, put it in its own file under /etc/sudoers.d/, check it with visudo -cf, and install it with mode 440. Do not use wildcards in the arguments, and do not allow editors or any program that can start a shell.
- [How do I use rm safely on a server?](https://valakes.com/learn/safe-rm-on-servers/): List what a command will remove before you run it, with ls, find or echo. Use rm -I so that rm asks once before a recursive delete, and write variables as "${dir:?}" so that an empty one stops the command. The built-in --preserve-root protects only the path /.
