Example of the passwd command in 10 Linux
As shown in the passwd command name, it is used to change the password of the System user. If the passwd command is executed by a non-root user, it will ask the current user's password and then set the new password of the user who calls the command. When this command is executed by the Super User root, You can reset the password of any user, including the user who does not know the current password.
In this article, we will use an instance to introduce the passwd command.
Syntax:
# passwd {options} {user_name}
You can use different options in the passwd command. The list is as follows:
Example 1: change the password of a system user
When you use a non-root user to log on, for example, if I use 'linuxtechi' to log on, running the passwd command will reset the password of the currently logged on user.
[linuxtechi@linuxworld ~]$ passwdChanging password for user linuxtechi.Changing password for linuxtechi.(current) UNIX password:New password:Retype new password:passwd: all authentication tokens updated successfully.[linuxtechi@linuxworld ~]$
When you log on as the root user and run the passwd command, it will reset the root password by default. If you specify the user name after the passwd command, it resets the password of the user.
[root@linuxworld ~]#passwd
[root@linuxworld ~]#passwd linuxtechi
Note: the password of the System user is encrypted and stored in the/etc/shadow file.
Example 2: display the password status
To display the status of the user password, use the-S option after the passwd command.
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 2015-09-200999997-1(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
In the above output, the user name is displayed in the first field, and the password status is displayed in the second field (PS = password settings, LK = PASSWORD locked, NP = no password ), the third field shows the time when the password was last modified. The last four fields show the minimum and maximum periods, warning periods, and periods when the password was not used.
Example 3: display the password status of all accounts
To display the status information of all user passwords, use the "-aS" option in the passwd command, for example:
root@localhost:~#passwd-Sa
(LCTT Note: Different releases/passwd have different behaviors. CentOS6.6 is not tested successfully, but Ubuntu can .)
Example 4: Use the-d option to delete a user's password
For example, delete the password of the 'linuxtechi' user.
[root@linuxworld ~]#passwd-d linuxtechi
Removing password for user linuxtechi.
passwd:Success
[root@linuxworld ~]#
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi NP 2015-09-200999997-1(Empty password.)
[root@linuxworld ~]#
The "-d" option clears the user password and disables user logon.
Example 5: Set the password to expire immediately
Using the '-e' option in the passwd command will immediately expire the user's password, which will force the user to change the password upon next login.
[root@linuxworld ~]#passwd-e linuxtechi
Expiring password for user linuxtechi.
passwd:Success
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 1970-01-010999997-1(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
Now try to use linuxtechi user SSH to connect to the host.
Example 6: Lock the password of the System user
Use the '-l' option in the passwd command to lock the user's password. It will add "!" to the start position of the password. When his/her password is locked, the user cannot change its password.
[root@linuxworld ~]#passwd-l linuxtechi
Locking password for user linuxtechi.
passwd:Success
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi LK 2015-09-200999997-1(Password locked.)
[root@linuxworld ~]#
Example 7: Use the-u option to unlock the User Password
[root@linuxworld ~]#passwd-u linuxtechi
Unlocking password for user linuxtechi.
passwd:Success
[root@linuxworld ~]#
Example 8: Use the-I option to set the inactive time
Use the-I option in the passwd command to set the inactivity time of the System user. After the password of the user (I am using linuxtechi user) expires, the user does not change the password after 'n' days (in my case, it is 10 days, you cannot log on.
[root@linuxworld ~]#passwd-i 10 linuxtechi
Adjusting aging data for user linuxtechi.
passwd:Success
[root@linuxworld ~]#
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 2015-09-20099999710(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
Example 9: Use the-n option to set the shortest time for Password Change
In the following example, you must change the password of linuxtechi within 90 days. 0 indicates that you can change the password at any time.
[root@linuxworld ~]#passwd-n 90 linuxtechi
Adjusting aging data for user linuxtechi.
passwd:Success
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 2015-09-209099999710(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
Example 10: Use the-w option to set the warning period before the password expires.
The '-W' option is used in the passwd command to set the user's warning period. This means that his/her password will expire in n days.
[root@linuxworld ~]#passwd-w12 linuxtechi
Adjusting aging data for user linuxtechi.
passwd:Success
[root@linuxworld ~]#passwd-S linuxtechi
linuxtechi PS 2015-09-2090999991210(Passwordset, SHA512 crypt.)
[root@linuxworld ~]#
Via: http://www.linuxtechi.com/10-passwd-command-examples-in-linux/
Author: Pradeep Kumar Translator: strugglingyouth Proofreader: wxy
This article was originally compiled by LCTT and launched with the honor of Linux in China
This article permanently updates the link address: