passwd命令
passwd对于普通用户来说,可以修改自己的密码,对于root用户来说,可以修改所有用户的密码。
passwd如果直接回车的话,则表示的是修改自己的密码,如果passwd username,则表示修改username这个用户的密码。
示例1:修改root用户自己的密码
[root@Server3 ~]# passwd Changing password for user root. New password: BAD PASSWORD: it is based on a dictionary word Retype new password: passwd: all authentication tokens updated successfully. [root@Server3 ~]# 说明:这里修改的是root用户自己的密码。
示例2:修改user1的密码
[root@Server3 ~]# passwd user1 Changing password for user user1. New password: BAD PASSWORD: it is based on a dictionary word BAD PASSWORD: is too simple Retype new password: passwd: all authentication tokens updated successfully. [root@Server3 ~]#
选项说明:
--stdin:从标准输入读取数据最为用户的密码,在批量创建用户的时候比较有用。
示例1:从标准输入读取一个字符串redhat最为user1的密码。
[root@Server3 ~]# echo -n "redhat" | passwd --stdin user1 Changing password for user user1. passwd: all authentication tokens updated successfully. [root@Server3 ~]#
-l:锁定用户。
-u:解锁用户。
示例3:
[root@Server3 ~]# passwd -l user1 Locking password for user user1. passwd: Success [root@Server3 ~]# passwd -u user1 Unlocking password for user user1. passwd: Success [root@Server3 ~]#
-d:快速的删除一个账户的密码。
示例4:
[root@Server3 ~]# grep user1 /etc/shadow user1:$6$JOPSA8NA$FuNTu23XsKkYMrhgygd8N9LJFDfgfeQzuBLp/xCJNhIXUif.Fichgd51HIren/4ZVN3QqO/mOdZKdeHtLaUXS/:16252:0:99999:7:10:16290: [root@Server3 ~]# passwd -d user1 Removing password for user user1. passwd: Success [root@Server3 ~]# grep user1 /etc/shadow user1::16252:0:99999:7:10:16290: [root@Server3 ~]#
-e:快速的使一个账户的密码过期,当用户下次登录的时候,强制用户修改密码。
示例5:
[root@Server3 ~]# passwd -e user1 Expiring password for user user1. passwd: Success [root@Server3 ~]# su - user2 [user2@Server3 ~]$ su - user1 Password: You are required to change your password immediately (root enforced) Changing password for user1. (current) UNIX password:
-n:密码使用的最短期限。
示例6:
[root@Server3 ~]# chage -l user1 Last password change : Jul 01, 2014 Password expires : never Password inactive : never Account expires : Aug 08, 2014 Minimum number of days between password change : 0 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7 [root@Server3 ~]# passwd -n 3 user1 Adjusting aging data for user user1. passwd: Success [root@Server3 ~]# chage -l user1 Last password change : Jul 01, 2014 Password expires : never Password inactive : never Account expires : Aug 08, 2014 Minimum number of days between password change : 3 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7 [root@Server3 ~]#
-x:密码使用的最大期限。默认为99999
示例7:
[root@Server3 ~]# chage -l user1 Last password change : Jul 01, 2014 Password expires : never Password inactive : never Account expires : Aug 08, 2014 Minimum number of days between password change : 3 Maximum number of days between password change : 99999 Number of days of warning before password expires : 7 [root@Server3 ~]# passwd -x 10 user1 Adjusting aging data for user user1. passwd: Success [root@Server3 ~]# chage -l user1 Last password change : Jul 01, 2014 Password expires : Jul 11, 2014 Password inactive : Jul 21, 2014 Account expires : Aug 08, 2014 Minimum number of days between password change : 3 Maximum number of days between password change : 10 Number of days of warning before password expires : 7 [root@Server3 ~]#
-w:密码到期前多少天接收警告。默认为7天。
[root@Server3 ~]# chage -l user1 Last password change : Jul 01, 2014 Password expires : Jul 11, 2014 Password inactive : Jul 21, 2014 Account expires : Aug 08, 2014 Minimum number of days between password change : 3 Maximum number of days between password change : 10 Number of days of warning before password expires : 7 [root@Server3 ~]# passwd -w 5 user1 Adjusting aging data for user user1. passwd: Success [root@Server3 ~]# chage -l user1 Last password change : Jul 01, 2014 Password expires : Jul 11, 2014 Password inactive : Jul 21, 2014 Account expires : Aug 08, 2014 Minimum number of days between password change : 3 Maximum number of days between password change : 10 Number of days of warning before password expires : 5 [root@Server3 ~]#
-i:密码过期多少天后,账户将被禁用。
[root@Server3 ~]# grep user1 /etc/shadow user1:$6$DP3GKHzb$rLZel.UO2qrO90rEdRPrbCvvP9kF86NJn64Y.tl6WknJrBUL3miflgLOIMO5mfJpP1pEG9JMefKnYSdofviYG0:16252:3:10:5:10:16290: [root@Server3 ~]# passwd -i 100 user1 Adjusting aging data for user user1. passwd: Success [root@Server3 ~]# grep user1 /etc/shadow user1:$6$DP3GKHzb$rLZel.UO2qrO90rEdRPrbCvvP9kF86NJn64Y.tl6WknJrBUL3miflgLOIMO5mfJpP1pEG9JMefKnYSdofviYG0:16252:3:10:5:100:16290: [root@Server3 ~]#
-S:查看账户的密码状态信息。
[root@Server3 ~]# passwd -S user1 user1 PS 2014-07-01 3 10 5 100 (Password set, SHA512 crypt.) [root@Server3 ~]# 说明: 分别表示:username PS 上一次修改密码的时间 min max warning inactive 密码加密方式