Linux Commands – passwd
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed useradd command in Linux which is used to create a new user in Linux.
https://cloudaffaire.com/linux-commands-useradd/
In this blog post, we will discuss passwd command in Linux. passwd command is used to add, update, or delete the user’s password. You can update your own password using passwd command but if you want to update or set other user passwords, you need to have root privileges.
Linux Commands – passwd:
You can use passwd command to set, update, or delete user password.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
############################# ## Linux Commands | passwd ## ############################# ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ##------- ## passwd ##------- ## passwd [options] LOGIN passwd ## reset your own password sudo useradd myuser ## create a user sudo passwd myuser ## set myuser password sudo grep myuser /etc/passwd ## myuser:x:1001:1001::/home/myuser:/bin/bash sudo grep myuser /etc/shadow ## myuser:encrypted_pwd:18409:0:99999:7::: ## password policy configuration files sudo ls /etc/pam.d/ sudo chage -l myuser ## get myuser password expiry info |
You can use passwd -S or –status options to output a short information about the status of the password for a given account. The status information consists of 7 fields. Below is the meaning of each field:
- User login name
- User account status (LK – locked password, NP – no password, PS – usable password)
- User last password change date
- Minimum password age in days.
- Maximum password age in days.
- Warning period in days.
- Inactivity period in days.
Note: The date of the last password change is stored as a number of days since epoch. Depending on the current time zone, the passwd -S username may show the date of the last password change that is different from the real date of the last password change by ±1 day.
1 2 3 4 5 |
## passwd -S or --status options sudo passwd -S myuser ## get myuser password status ## returns ## myuser PS 2020-05-27 0 99999 7 -1 (Password set, SHA512 crypt.) |
You can use passwd -l or –lock options to lock the password of the specified account. The locking is performed by rendering the encrypted password into an invalid string (by prefixing the encrypted string with an !).
Note: The user can still log in by other means of authentication such as the ssh public key authentication. Use chage -E 0 user command instead for full account locking.
1 2 3 4 5 6 7 8 9 |
## passwd -l or --lock options sudo passwd -l myuser ## lock myuser password sudo passwd -S myuser ## get myuser password status ## returns myuser LK 2020-05-27 0 99999 7 -1 (Password locked.) su - myuser ## try to login using myuser ## su: Authentication failure |
You can use passwd -u or –unlock options to unlock the account password by removing the ! prefix (reverse of the -l option). By default, passwd will refuse to create a passwordless account (it will not unlock an account that has only “!” as a password). The force option -f will override this protection.
1 2 3 4 5 6 7 8 9 10 11 |
## passwd -u or --unlock options sudo passwd -u myuser ## unlock myuser password sudo passwd -S myuser ## get myuser password status ## returns myuser PS 2020-05-28 0 99999 7 -1 (Password set, SHA512 crypt.) su - myuser ## try to login using myuser ## successfully logged in as myuser exit ## logout myuser |
You can use passwd -d or –delete options to delete a password for an account. It will set the named account passwordless.
1 2 3 4 5 6 |
## passwd -d or --delete options sudo passwd -d myuser ## delete myuser password sudo passwd -S myuser ## get myuser password status ## returns myuser NP 2020-05-28 0 99999 7 -1 (Empty password.) |
You can use passwd -f or –force option to force the specified operation. For example, passwd will refuse to unlock a locked passwordless account. The force option -f can override this protection.
1 2 3 4 5 6 7 8 |
## passwd -f or --force option sudo passwd -l myuser ## lock myuser password sudo passwd -u myuser ## try to unlock myuser password ## passwd: Warning: unlocked password would be empty. sudo passwd -fu myuser ## successfully unlocked with -f option |
You can use passwd –stdin option to indicate that passwd should read the new password from standard input, which can be a pipe.
1 2 3 4 5 6 7 8 9 10 11 |
## passwd --stdin option sudo passwd -S myuser ## get myuser password status ## returns myuser NP 2020-05-28 0 99999 7 -1 (Empty password.) mypwd='myPa$$word' ## define myuser password in a variable ## set myuser password using --stdin option echo $mypwd | sudo passwd --stdin myuser sudo passwd -S myuser ## get myuser password status ## returns myuser PS 2020-05-28 0 99999 7 -1 (Password set, SHA512 crypt.) |
You can use passwd -e or –expire options to expire a password for an account. The user will be forced to change the password during the next login attempt.
1 2 3 4 5 6 7 8 9 10 11 12 |
## passwd -e or --expire options sudo chage -l myuser ## get myuser password expiry info ## Password expires : never sudo passwd -e myuser ## expire myuser password sudo chage -l myuser ## get myuser password expiry info ## Password expires : password must be changed su - myuser ## try to login as myuser ## You are required to change your password immediately (root enforced) |
You can use passwd -n or –minimum DAYS options to set the minimum password lifetime, in days, if the user’s account supports password lifetimes.
1 2 3 4 5 6 7 8 9 |
## passwd -n or --minimum DAYS options sudo chage -l myuser ## get myuser password info ## Minimum number of days between password change : 0 sudo passwd -n 30 myuser ## set minimum days between password change for myuser sudo chage -l myuser ## get myuser password info ## Minimum number of days between password change : 30 |
You can use passwd -x or –maximum DAYS options to set the maximum password lifetime, in days, if the user’s account supports password lifetimes.
1 2 3 4 5 6 7 8 9 |
## passwd -x or --maximum DAYS options sudo chage -l myuser ## get myuser password info ## Maximum number of days between password change : 0 sudo passwd -x 30 myuser ## set maximum days between password change for myuser sudo chage -l myuser ## get myuser password info ## Maximum number of days between password change : 30 |
You can use passwd -w or –warning DAYS options to set the number of days in advance the user will begin receiving warnings that her password will expire if the user’s account supports password lifetimes.
1 2 3 4 5 6 7 8 9 |
## passwd -w or --warning DAYS options sudo chage -l myuser ## get myuser password info ## Number of days of warning before password expires : 7 sudo passwd -w 30 myuser ## set number of days of warning before password expires for myuser sudo chage -l myuser ## get myuser password info ## Number of days of warning before password expires : 30 |
You can use passwd -i or –inactive DAYS options to set the number of days which will pass before an expired password for this account will be taken to mean that the account is inactive and should be disabled if the user’s account supports password lifetimes.
1 2 3 4 5 6 7 8 9 |
## passwd -i or --inactive DAYS options sudo chage -l myuser ## get myuser password info ## Password inactive : never sudo passwd -i 30 myuser ## set password inactive days for myuser sudo chage -l myuser ## get myuser password info ## Password inactive : some date |
You can use passwd -k or –keep options to enable user to update his password only when it expired. If the password is still active then the user will not be able to update his password. But I am unable to repro this option in my RHEL based system. In some other blog, its mentioned that this option only works on Debian based system. If you have an idea, please write in the comment section.
1 2 3 4 5 6 7 8 9 10 11 |
## passwd -k or --keep options sudo passwd -k myuser ## enable password update only when expired su - myuser ## login as myuser passwd ## try to update the password exit sudo userdel -rf myuser ## delete the user |
Hope you have enjoyed this article. In the next blog post, we will discuss chage command in Linux.