Linux用户密码过期延期

系统+安全 Liemer_Lius 3592℃

Linux用户默认的用户有效期是永久有效。如果做过了安全加固,用户的有效期通常会设置一个固定的数值,比如90天。

有效期是为了让用户定期修改密码,增加安全性。如果不考虑安全性的问题,在密码即将到期的时候,将密码有效期直接延长下一个90天的话,就需要用一些操作来进行。

1. vim直接修改

记录密码有效期的文件是/etc/shadow,这个是root权限才能进行修改的文件。这个文件中,每一段的含义,请转到:CentOS中/etc/shadow详解

其中,冒号分割的第三段,就是最后一次修改的日期,距离1970年1月1日的天数,我们把这个数值修改为+90的数字,即可延长有效期90天。

2. chage命令修改

帮助信息:

> chage --help 
Usage: chage [options] LOGIN

Options:
  -d, --lastday LAST_DAY        set date of last password change to LAST_DAY
  -E, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -h, --help                    display this help message and exit
  -I, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -l, --list                    show account aging information
  -m, --mindays MIN_DAYS        set minimum number of days before password
                                change to MIN_DAYS
  -M, --maxdays MAX_DAYS        set maximim number of days before password
                                change to MAX_DAYS
  -R, --root CHROOT_DIR         directory to chroot into
  -W, --warndays WARN_DAYS      set expiration warning days to WARN_DAYS

重要选项:

-d:修改最后一次修改密码的时间

先查看/etc/shadow文件中,相关用户最后一次修改日期距离1970年1月1日的天数

> sudo grep ^lius /etc/shadow |awk -F: '{print $3}'
18208

 

(比如18208,+90=18298),然后用chage命令修改这个日期:

chage -d 18291 lius

-l:查看用户账号的各种时间信息

> sudo chage -l lius
Last password change					: Feb 06, 2020
Password expires					: May 06, 2020
Password inactive					: never
Account expires						: never
Minimum number of days between password change		: 6
Maximum number of days between password change		: 90
Number of days of warning before password expires	: 30						: never
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

这样,Password expires的日期向后延期了90天。

-E:注意,这里的-E是账号过期的时间,不是密码过期的时间,修改的是Account expires这个选项,和我们的预期不符(看上面标红的第二个)。

3. usermod命令

帮助信息:

> sudo usermod --help
Usage: usermod [options] LOGIN

Options:
  -c, --comment COMMENT         new value of the GECOS field
  -d, --home HOME_DIR           new home directory for the user account
  -e, --expiredate EXPIRE_DATE  set account expiration date to EXPIRE_DATE
  -f, --inactive INACTIVE       set password inactive after expiration
                                to INACTIVE
  -g, --gid GROUP               force use GROUP as new primary group
  -G, --groups GROUPS           new list of supplementary GROUPS
  -a, --append                  append the user to the supplemental GROUPS
                                mentioned by the -G option without removing
                                him/her from other groups
  -h, --help                    display this help message and exit
  -l, --login NEW_LOGIN         new value of the login name
  -L, --lock                    lock the user account
  -m, --move-home               move contents of the home directory to the
                                new location (use only with -d)
  -o, --non-unique              allow using duplicate (non-unique) UID
  -p, --password PASSWORD       use encrypted password for the new password
  -R, --root CHROOT_DIR         directory to chroot into
  -s, --shell SHELL             new login shell for the user account
  -u, --uid UID                 new UID for the user account
  -U, --unlock                  unlock the user account
  -v, --add-subuids FIRST-LAST  add range of subordinate uids
  -V, --del-subuids FIRST-LAST  remove range of subordinate uids
  -w, --add-subgids FIRST-LAST  add range of subordinate gids
  -W, --del-subgids FIRST-LAST  remove range of subordinate gids
  -Z, --selinux-user SEUSER     new SELinux user mapping for the user account

相关选项:

-e:这个-e和chage中的-E的功能相同,改变的是账号的过期时间,和需求不符。

 

转载请注明:liutianfeng.com » Linux用户密码过期延期

喜欢 (1)

评论已关闭。