chage:查看或修改用户密码有效期
root@oldboyedu ~]# chage -l oldboy ##-l 显示用户有效期信息
Last password change : Oct 07, 2020 #<==最后一次密码变化时间为2020年10月7号,-d(设置上一次密码更改的日期)可以控制该行。
Password expires : never #<==密码过期期限,never为从不。
Password inactive : never #<==密码停权时间
Account expires : never #<==用户过期时间,-E(用户过期时间,格式为YYYY-MM-DD)可以控制该行。
Minimum number of days between password change : 0 #<==密码更改之间的最小天数,-m可以控制该行。
Maximum number of days between password change : 99999 #<==密码更改之间的最大天数,-M。
Number of days of warning before password expires : 7 #<==密码过期前发出警告的天数,-W。
-E参数练习
[root@oldboyedu ~]# chage -E "2020/10/1" oldboy #<==更改用户的过期日期
[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : never
Password inactive : never
Account expires : Oct 01, 2020 #<==更改成功,过期时间为2020年10月1号。
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
范例14-17:创建新用户range,要求该用户7天内不能更改密码,
60天以后必须修改密码,过期前10天通知用户,过期后30天后禁止用户登录。
chage -m7 -M60 -W10 -I30 oldboy #<==-m(密码可更改最小天数)-M(密码有效最大天数)-l(显示用户有效期)
联系英文:
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
[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : never
Password inactive : never
Account expires : Oct 01, 2020
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
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# chage -m7 -M60 -W10 -I30 oldboy
[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : Dec 06, 2020
Password inactive : Jan 05, 2021
Account expires : Oct 01, 2020
Minimum number of days between password change : 7
Maximum number of days between password change : 60
Number of days of warning before password expires : 10
14.8 修改用户密码命令 passwd
范例 14-16:要求oldboy用户7天内不能更改密码,600天后必须修改密码,过期前100天通知用户,过期后300天禁止用户登录。
[root@oldboyedu ~]# passwd -n 70 -x 600 -w 100 -i 300 oldboy
Adjusting aging data for user oldboy.
passwd: Success
You have new mail in /var/spool/mail/root
[root@oldboyedu ~]# chage -l oldboy
Last password change : Oct 07, 2020
Password expires : May 30, 2022
Password inactive : Mar 26, 2023
Account expires : Oct 01, 2020
Minimum number of days between password change : 70
Maximum number of days between password change : 600
Number of days of warning before password expires : 100
-n, --minimum DAYS
This will set the minimum password lifetime, in days, if the user's account supports password life‐
times. Available to root only.
-x, --maximum DAYS
This will set the maximum password lifetime, in days, if the user's account supports password life‐
times. Available to root only.
-w, --warning DAYS
This will 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. Available to root only.
-i, --inactive DAYS
更改的都是/etc/shadow文件
14.12 Linux用户身份切换命令 su
那么工作中如何使用su呢?
1、先登录普通用户,没事不允许root用户。只有执行的任务需要root权限的时候才允许登录root。这才是工作中正常的维护通道。
2、su的问题
在上述情况下,用su管理,必须知道root密码,这样就造成了极大的安全隐患。比如公司10个运维,那10个运维都知道了root密码。极度不安全。
3、解决上述问题
(1)不用知道root密码还能管理服务器。
(2)最小化管理服务器。例如想关机了,就只给一个halt关机权限。
那么如何解决呢?
使用sudo命令:可以最小化的权限(单个命令),执行命令的时候拥有root用户的权限。
例如:以前学到的suid,是针对命令文件, 设置后所有用户都能以root用户身份或权限执行哪个命令。
而sudo,是针对用户,给某个用户以root身份来执行某个命令。(相对安全)
那么如何编辑、配置sudo呢?
首先需要了解:
sudo是一个提权的命令(对应权限通过读取/etc/sudoers文件实现的)
配置/etc/sudoers时,可以使用visudo命令,或vim /etc/sudoers(有严格的语法格式要求,不推荐使用)
演示范例:
[oldboy@xietangsheng ~]$ ls /root/ 没有权限,无法访问
ls: cannot open directory /root/: Permission denied
[root@xietangsheng ~]# visudo 用root执行visudo,100G到100行位置
…………省略部分内容
root ALL=(ALL) ALL
oldboy ALL=(ALL) /usr/bin/ls(把原来的ALL改成命令绝对路径,如果是多个命名,用“,”逗号隔开,注意要用命令绝对路径,不要用vim /etc/sudoser,如果非要用vim编辑,完了后要用visudo -c检查语法)
用户 主机=角色 命令
[oldboy@xietangsheng ~]$ sudo -l 查询是否拥有权限
[sudo] password for oldboy: 首次需要输入密码
Matching Defaults entries for oldboy on xietangsheng:
!visiblepw, always_set_home, match_group_by_gid,
always_query_group_plugin, env_reset, env_keep="COLORS
DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS",
env_keep+="MAIL PS1 PS2 QTDIR USERNAME LANG LC_ADDRESS
LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION
LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY
LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE",
env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET
XAUTHORITY",
secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
User oldboy may run the following commands on xietangsheng:
(ALL) /bin/ls 所有的权限
[oldboy@xietangsheng ~]$ sudo ls /root/ 执行成功
123.txt anaconda-ks.cfg oldboy.txt oldgirl passwd.txt
14.13 visudo:编辑sudoers文件工具
普通用户切换到root用户不输入密码配置方法:
visudo
oldboy ALL=(ALL) NOPASSWD: ALL
[oldboy@xietangsheng ~]$ sudo su -
Last login: Tue Mar 26 11:31:53 CST 2019 on pts/1
[root@xietangsheng ~]#
为了安全,禁止使用root远程登录linux
su与sudo用户切换原理图:
sudo工作原理流程图:7和5检查的文件是同一个文件
练习题:给普通用户oldgirl增加添加用户和删除用户的权限
[root@xietangsheng ~]# visudo
oldgirl ALL=(ALL) /usr/sbin/useradd,/usr/sbin/userdel
[root@xietangsheng ~]# visudo -c 检查语法是否正确
/etc/sudoers: parsed OK
[root@xietangsheng ~]# su - oldgirl
Last login: Tue Mar 26 11:59:13 CST 2019 on pts/1
[oldgirl@xietangsheng ~]$ sudo useradd xi 创建用户
[oldgirl@xietangsheng ~]$ cat /etc/passwd | grep xi
xi:x:1003:1003::/home/xi:/bin/bash 创建成功
[oldgirl@xietangsheng ~]$ sudo userdel -r xi 删除用户
[oldgirl@xietangsheng ~]$ cat /etc/passwd | grep xi 删除成功