As the name suggest passwd command is used to change the password of system users. If the passwd command is executed by non-root user then it will ask for the current password and then set the new password of a user who 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.
In this post we will discuss passwd command with practical examples.
Syntax :
# passwd {options} {user_name}
Different options that can be used in passwd command are listed below :
Example:1 Change Password of System Users
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 the root password by default 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
Note : System user’s password is stored in an encrypted form in /etc/shadow file.
Example:2 Display Password Status Information.
To display password status information of a user , use -S option in passwd command.
[[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 Password Status info for all the accounts
To display password status info for all the accounts use “-aS” option in passwd command, example is shown below :
[email protected]:~# passwd -Sa
Example:4 Removing Password of a User using -d option
In my case i am removing/ 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] ~]#
“-d” option will make user’s password empty and will disable user’s account.
Example:5 Set 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 machine using linuxtechi user.
Example:6 Lock the password of System User
Use ‘-l‘ option in passwd command 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 locked.
[[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 using -u option
[[email protected] ~]# passwd -u linuxtechi Unlocking password for user linuxtechi. passwd: Success [[email protected] ~]#
Example:8 Setting inactive days using -i option
-i option in passwd command is used to set inactive days for a system user. This will come into the picture when password of user ( in my case linuxtechi) expired and user didn’t change its password in ‘n‘ number of days ( i.e 10 days in my case) then after that user will not able to login.
[[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 Password using -n option.
In the below example linuxtechi user has to change the 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 expire using -w option
‘-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.
[[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] ~]#
Recommended Read : 12 Useful ‘dmidecode’ Command Examples for Linux Admin
Great examples!
The following can be used to change user password using shell script.
echo -e "newpasswordnnewpassword" | passwd username
Can i make same current asking policy for root when using passwd command as root????
Hi Abhi,
Yes, you can make the same password policy for root user but it is not recommended to implement on root.
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.
while using the command passwd ,
why it showing error after changing the password or enter new password