Locking a user’s password
You can disable password login for a user by locking their password. Just use the passwd command with the -l (--lock) option followed by the username:
sudo passwd -l newman
The -l option disables the password by changing it to a value that does not match any possible encrypted value. As a result, the user will not be able to log in with a password. Logging in with an SSH key keeps working as long as the SSH server has PAM enabled (UsePAM yes, the default on Debian and Ubuntu) – without PAM, SSH rejects a locked account entirely, even with a key. If we wanted to block logins completely, we would also have to change the user’s shell, for example like this:
sudo usermod -s /usr/sbin/nologin newman
Disabling passwords directly in SSH
If all you need is to block remote password login, it is more reliable to switch passwords off in the SSH server configuration. The password then does not have to be locked, and local password login, for example on the server console, keeps working. For a single user, add a Match block to the end of /etc/ssh/sshd_config – it applies until the end of the file or the next Match. For all users, set PasswordAuthentication no outside of it (and KbdInteractiveAuthentication no if you have it enabled).
Match User newman
AuthenticationMethods publickey
Check the configuration before reloading it and keep your current connection open, so that a mistake does not lock you out:
sudo sshd -t && sudo systemctl reload ssh
Unlocking a user’s password
To allow password login again, simply unlock the password with the -u (--unlock) option:
sudo passwd -u newman
Checking the status of a user’s password
If we want to find out the status of a user’s password, we use the -S (--status) option:
sudo passwd -S newman
The result may then look something like this:
$ sudo passwd -S newman
newman L 06/15/2021 0 99999 7 -1
The output contains information in seven columns, with the following meaning:
- the user’s login name,
- whether the account is locked (L), has no password (NP) or has a usable password (P),
- the date of the last password change,
- the minimum number of days before the password can be changed again after the previous change,
- the maximum number of days before the password must be changed again,
- the number of days before password expiration during which the user starts receiving warnings,
- the number of days after password expiration before the user is deactivated.
For this note I drew mostly on this article, which describes further options for working with user passwords.