How to change a user's password | passwd command in Linux

passwd command in Linux
passwd command in Linux

The passwd command is one of the most important commands for managing users in Linux. This command allows you to change a user's password and set or change other password-related settings. In this article, we'll show you how to use the passwd command to change a user's password in Linux.

  • To change the password of a user, you will need to have root privileges
  • It is important to note that changing a user's password should only be done when necessary.
  • Choosing a strong password that one cannot easily guess is also essential.

Changing a User's Password

The most basic use of the passwd command is to change a user's password. This is done by typing "passwd" followed by the username of the user whose password you want to change.

Syntax:

passwd [options] [LOGIN]

Example:

To change jane's password, you would type:

sudo passwd jane

You will need to enter the new password for the user. Once you have entered the new password, you must confirm it by typing it again. Once you have done this, the users' password will be changed.



Changing Other Password Settings

In addition to changing a user's password, the passwd command can also change other password-related settings.

password expiration date

You can set a user's password expiration date using the passwd command. To do this, you would use the -e option followed by the username of the user whose password you want to expire. For example, to set an expiration date for the password of the user "jane" in 30 days, you would type:

sudo passwd -e jane 30

Lock and Unlock Users Password

You can also lock or unlock a user's account. To lock a user's account, you would use the -l option followed by the username of the account you want to lock.
For example, to lock the account of the user "jane," you would type:

sudo passwd -l jane

To unlock a user's account, you would use the -u option followed by the username of the account you want to unlock.

Example:
To unlock the account of the user "jane," you would type:

sudo passwd -u jane

Delete a Users password

The -d option will delete a password for an account. The account will remain active, but anyone can log in without supplying a password.

sudo passwd -d jane

Set the number of days before a password can be changed

The -n option, followed by a number, sets the days before a password can be changed again. In the following example, a user's password can not be changed for 10 days.

sudo passwd -n 10 jane

You can confirm this using the following command.

sudo passwd -S user1

Output:

jane P 09/03/2022 10 99999 7 -1

The number 10 after the date is the password's lifetime before it can be changed.

There are more options for the passwd command, but these are commonly used. Find out more about the passwd command in the man pages.

man passwd

Conclusion

The passwd command is a powerful tool for managing users in Linux. In this article, we've shown you how to use the passwd command to change a user's password and how to use it to change other password-related settings. With this knowledge, you should be able to manage users on your Linux system effectively. Explore other Linux topics on managing users in the system.

Usermod Command in Linux

Thanks for reading.

If you enjoy our content, please consider buying us a coffee to support our work:

Table of Contents
Great! Next, complete checkout for full access to GeekBits.
Welcome back! You've successfully signed in.
You've successfully subscribed to GeekBits.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.