Skip to content
iD-SIGN
Contact us

Disabling password login for a user in Linux

It is often useful to disable remote password login for some users on a Linux system and allow only SSH key authentication. Here is how to do it.

3 min readpublished 9/2022

In this article 4 chapters
  1. Locking a user’s password
  2. Disabling passwords directly in SSH
  3. Unlocking a user’s password
  4. Checking the status of a user’s password

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:

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

bash
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).

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

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

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

bash
sudo passwd -S newman

The result may then look something like this:

bash
$ sudo passwd -S newman
newman L 06/15/2021 0 99999 7 -1

The output contains information in seven columns, with the following meaning:

  1. the user’s login name,
  2. whether the account is locked (L), has no password (NP) or has a usable password (P),
  3. the date of the last password change,
  4. the minimum number of days before the password can be changed again after the previous change,
  5. the maximum number of days before the password must be changed again,
  6. the number of days before password expiration during which the user starts receiving warnings,
  7. 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.

Tags Servers, Linux

Share LinkedIn Facebook

Need more than an article?

A few sentences about what you are dealing with are enough.