10 passwd Command Examples in Linux

As the name suggests passwd command in linux is used to change the password of system users. If the passwd command is executed by non-root user then it will prompt for the current password and then allows to set new password of a user who has invoked the command. When this command is executed by super user or root then it can reset the password for any user including root without knowing the current password.

When we run passwd command to set user’s password then user’s encrypted password string is saved in /etc/shadow file. In this article, we will discuss 10 passwd command examples in linux.

Syntax :

# passwd {options} {user_name}

Passwd Command Options

passwd-command-options

Now let’s dive into the passwd command practical examples.

Example 1) Change System User’s Password

When you logged in as non-root user like ‘linuxtechi’ in my case and run passwd command then it will reset password of logged in user.

[[email protected] ~]$ passwd
Changing password for user linuxtechi.
Changing password for linuxtechi.
(current) UNIX password:
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[[email protected] ~]$

When you logged in as root user and run passwd command then it will reset root user password and if you specify the user-name after passwd command then it will change the password of that user.

[[email protected] ~]# passwd
[[email protected] ~]# passwd linuxtechi

passwd-command

Note : A regular user can also change password of root and users provided that user is having sudo privileges. Let’s assume sysops is sudo privilege user, to change password of other users and root user, type sudo in front of passwd command, examples is shown below.

$ sudo password root          // this will change the root password$ sudo password linuxtechi    //this will change the password of linuxtechi

Example 2) Display User Status Information

To display user / account status information, use -S option in passwd command. User’s status information consists of seven fields as shown below.

[[email protected] ~]# passwd -S linuxtechi
linuxtechi PS 2015-09-20 0 99999 7 -1 (Password set, SHA512 crypt.)
[[email protected] ~]#

In the above output first field shows the user name and second field shows Password status ( PS = Password Set , LK = Password locked , NP = No Password ), third field shows when the password was changed and last & fourth field shows minimum age, maximum age, warning period, and inactivity period for the password

Example 3) Display Information of all users

To display the information of all users then use ‘-aS’ option in passwd command, example is shown below :

[email protected]:~# passwd -Sa

passwd-sa

Example 4)  Delete User’s Password

To delete User’s delete password via passwd command use ‘-d’ option. In the below example, We are deleting the password of ‘linuxtechi‘ user.

[[email protected] ~]# passwd -d linuxtechi
Removing password for user linuxtechi.
passwd: Success
[[email protected] ~]#
[[email protected] ~]# passwd -S linuxtechi
linuxtechi NP 2015-09-20 0 99999 7 -1 (Empty password.)
[[email protected] ~]#

So we can say ‘-d’ option will make user’s password empty and will disable account.

Example 5) Set User’s Password Expiry Immediately

Use ‘-e’ option in passwd command to expire user’s password immediately , this will force the user to change the password in the next login.

[[email protected] ~]# passwd -e linuxtechi
Expiring password for user linuxtechi.
passwd: Success
[[email protected] ~]# passwd -S linuxtechi
linuxtechi PS 1970-01-01 0 99999 7 -1 (Password set, SHA512 crypt.)
[[email protected] ~]#

Now try to ssh remote machine using linuxtechi user.

passwd-expiry

Example 6)  Lock User’s Password

Use ‘-l‘ option in passwd command is used to lock a user’s password, it will add “!” at starting of user’s password. A user can’t change it’s password when his/her password is in locked state.

[[email protected] ~]# passwd -l linuxtechi
Locking password for user linuxtechi.
passwd: Success
[[email protected] ~]# passwd -S linuxtechi
linuxtechi LK 2015-09-20 0 99999 7 -1 (Password locked.)
[[email protected] ~]#

Example 7) Unlock User’s Password

To unlock user’s password, use ‘-u’ option passwd command followed by user name. Example is shown below,

[[email protected] ~]# passwd -u linuxtechi
Unlocking password for user linuxtechi.
passwd: Success
[[email protected] ~]#

Example 8) Set Inactive Days after Password Expiry

‘-i’ option in passwd command is used to set inactive days for a system user. This will come into the picture when the password of a user is expired and user didn’t change its password in ‘n’ number of days ( i.e 10 days in my case) then user will not able to login and its account will be disabled.

[[email protected] ~]# passwd -i 10 linuxtechi
Adjusting aging data for user linuxtechi.
passwd: Success
[[email protected] ~]#
[[email protected] ~]# passwd -S linuxtechi
linuxtechi PS 2015-09-20 0 99999 7 10 (Password set, SHA512 crypt.)
[[email protected] ~]#

Example 9) Set Minimum Days to Change User Password

In Linux, we can force system users to change its password in n number of days using ‘-n’ in passwd command.

In the below example, linuxtechi user has to change its password in 90 days. A value of zero shows that user can change it’s password in any time.

[[email protected] ~]# passwd -n 90 linuxtechi
Adjusting aging data for user linuxtechi.
passwd: Success
[[email protected] ~]# passwd -S linuxtechi
linuxtechi PS 2015-09-20 90 99999 7 10 (Password set, SHA512 crypt.)
[[email protected] ~]#

Example 10) Set Warning Days Before Password Expiry

‘-w’ option in passwd command is used to set warning days for a user. It means a user will be warned for n number of days that his/her password is going to expire. In the below example, we have set 12 warning days before expiry.

[[email protected] ~]# passwd -w 12 linuxtechi
Adjusting aging data for user linuxtechi.
passwd: Success
[[email protected] ~]# passwd -S linuxtechi
linuxtechi PS 2015-09-20 90 99999 12 10 (Password set, SHA512 crypt.)
[[email protected] ~]#

That’s all from this article, I hope you have found it informative and insightful. For any queries please drop comments in the comments section below.

Recommended Read : 12 Useful ‘dmidecode’ Command Examples for Linux Admin

5 thoughts on “10 passwd Command Examples in Linux”

  1. Great examples!

    The following can be used to change user password using shell script.
    echo -e "newpasswordnnewpassword" | passwd username

  2. What is difference between passwd and usermod command. Even passwd can do same things that usermod does. then please tell the actual difference between passwd and usermod command.

Leave a Reply to Mangal Chavan Cancel reply

Your email address will not be published. Required fields are marked *