How to Use the passwd Command in Linux?

The passwd command is utilized to set and modify the passwords of the user accounts. The users can change the passwords of their accounts, while those with sudo privileges can manage the passwords of all user accounts. 

This blog will explain the working and detailed functionality of the passwd command in Linux with the following outcomes.

How Does the passwd Command Work in Linux?

The general syntax of using the passwd command in Linux:

$ passwd [Options] [Username]

The explanation of the above general syntax:

  • Use the passwd to invoke the command
  • Use different options of the passwd command
  • Specify the username whose password is going to be changed or set

Different options which can be used with the passwd command can be observed using the command: 

$ passwd --help

How to Use the passwd Command in Linux?

To explain the usage of the passwd command in Linux, we will use different examples. 

Example 1: Change the Password

Using only the passwd command without any flag will change the password of the current user as shown below: 

$ passwd

To change the password of another user’s account, use the sudo or the root account as demonstrated below: 

$ sudo passwd milton

The password of the user “milton” has been changed successfully. 

Example 2: Delete the Password

The passwd command with the “d” option can be used to delete the user password as we did here for the user named milton

$ sudo passwd -d milton

The password has been deleted for user Milton.

Example 3: Find the Status of the Password

To find the status of the user password, we can use the “S” option of the passwd command. For example, to find the status of the current user, run the command:

$ sudo passwd -S

The explanation of the above output:

  • root is the user name whose password status is displayed
  • The “L” shows that the account has the locked password
  • Date shows the last change in the password
  • The 0 is the minimum age of the password in days
  • The 9999 is the maximum age of the password in days
  • The 7 is the expiry date of the password in days
  • The -1 shows the inactivity of the password in days

To find the status of all the accounts, we will use the “a” option with the “S” option:

$ sudo passwd -aS

All the user’s password status has been displayed.

Example 4: Lock/Unlock the Password

To make the user account inaccessible for all authorized users, we can also lock the password. This will encrypt the password with the ! which will not match any other password. For example, we will lock the password of the “milton” using the command:

$ sudo passwd -l milton

To confirm the status of the passwd, use the command:

$ sudo passwd -S milton

To unlock the password, use the “u” option:

$ sudo passwd -u milton

It is recommended to set the password with the “p” option instead of unlocking it.

Example 5: Set the Expiry of the Password

The expiry option can be used with the “e” option. This option will expire the password instantly with the execution of the command:

$ sudo passwd -e milton

The passwd expiry has been changed. Now, on the next login, it will ask the user to set a new password. 

Conclusion

The passwd command is used by following the general syntax of the “passwd [Options] [User name]”. This passwd command is used to set and modify the user accounts’ passwords. This article has demonstrated the detailed usage of the passwd command in Linux.