6.12任务

3.4 usermod命令

usermod可以更改用户的属性。 使用-u选项可以更改用户的uid,使用-g选项可以更改用户的gid(数字和名字均可),使用-d来指定家目录(不删除已有的,不创建新的),使用-s指定shell。这些用法和useradd的用法是一样的。

[root@localhost ~]# usermod -u 1020 -g 1001 -d /home/lhy_usermod -s /sbin/nologin lhy07
[root@localhost ~]# tail -n1 /etc/passwd
lhy07:x:1020:1001::/home/lhy_usermod:/sbin/nologin
[root@localhost ~]# ls /home/
lhy01  lhy02  lhy03  lhy06  lhy07  lhy111

补充一个usermod的-G选项(useradd最好要和-g连用),给用户添加扩展组,它会移除你没有列出的组,除非使用-a选项(append)

-G, --groups GROUP1\[,GROUP2,…\[,GROUPN\]\]\] A list of supplementary groups which the user is also a member of. Each group is separated from the next by a comma, with no intervening whitespace. The groups are subject to the same restrictions as the group given with the -g option. 
If the user is currently a member of a group which is not listed, the user will be removed from the group. This behaviour can be changed via the -a option, which appends the user to the current supplementary group list.

使用id命令查看一下用户的uid,gid,groups

[root@localhost ~]# id lhy07
uid=1020(lhy07) gid=1001(lhy02) groups=1001(lhy02)

一个用户可以属于多个组,但是gid只有一个,其他的组可以多个。

[root@localhost ~]# usermod -g 1001,1013 lhy07
usermod: group '1001,1013' does not exist
[root@localhost ~]# usermod -G 1001,1013 lhy07
[root@localhost ~]# id lhy07
uid=1020(lhy07) gid=1001(lhy02) groups=1001(lhy02),1013(lhy07)
[root@localhost ~]# usermod -G 1002,1013 lhy07
[root@localhost ~]# id lhy07
uid=1020(lhy07) gid=1001(lhy02) groups=1001(lhy02),1002(lhy06),1013(lhy07)
[root@localhost ~]# usermod -G 1007 lhy07
[root@localhost ~]# id lhy07
uid=1020(lhy07) gid=1001(lhy02) groups=1001(lhy02),1007(grp2)

3.5用户密码管理

passwd命令可以更改用户密码,它有一个set_uid的特殊权限。

使用方法很简单,就是passwd+username。

更改完密码之后,在配置文件/etc/shadow下的第二列就会出现一个加密的字符串,如果没有密码就显示“!!”两个叹号。

[root@localhost ~]# tail /etc/shadow
dbus:!!:17693::::::
polkitd:!!:17693::::::
postfix:!!:17693::::::
sshd:!!:17693::::::
lhy01:$6$VyBTUdCM$XjFdH4m0VLdC/TfgJw62N9nBUMV/HrAUMTLZylf9TlH8OBJJP9Ur0nd6PY6VGP7DM.o6mjyNdtitU5BX5whxU1:17693:0:99999:7:::
lhy02:!!:17693:0:99999:7:::
lhy04:!!:17693:0:99999:7:::
lhy05:!!:17693:0:99999:7:::
lhy06:!!:17693:0:99999:7:::
lhy07:!!:17693:0:99999:7:::

还有一些密码位置是*星号。不论是叹号还是星号,都表示此用户的密码是被锁定的,此时是不能登录的。(叹号的可以用root用户的su命令登录)

[root@localhost ~]# head /etc/shadow
root:$6$i2BzN./bOmTRb9Bu$VvElyUIWT8zdQ2IKwRqsKWNhrYaI3m4utEKI.ESptQ9okg9/yMVvS0e.fzf2dbtUty4jSm.tQAHNBhdImfEDI/::0:99999:7:::
bin:*:17110:0:99999:7:::
daemon:*:17110:0:99999:7:::
adm:*:17110:0:99999:7:::
lp:*:17110:0:99999:7:::
sync:*:17110:0:99999:7:::
shutdown:*:17110:0:99999:7:::
halt:*:17110:0:99999:7:::
mail:*:17110:0:99999:7:::
operator:*:17110:0:99999:7:::

-l选项(lock)可以锁定一个用户的密码(root用户依然可以登录),可以看到加密的密码前有两个叹号

[root@localhost ~]# passwd -l lhy01
Locking password for user lhy01.
passwd: Success
[root@localhost ~]# cat /etc/shadow | grep lhy01
lhy01:!!$6$VyBTUdCM$XjFdH4m0VLdC/TfgJw62N9nBUMV/HrAUMTLZylf9TlH8OBJJP9Ur0nd6PY6VGP7DM.o6mjyNdtitU5BX5whxU1:17693:0:99999:7:::

使用-u选项(unlock)解锁。

对一个用户的锁定,同样可以使用usermod命令的-L选项。只不过它只显示一个叹号。

[root@localhost ~]# usermod -L lhy01
[root@localhost ~]# cat /etc/shadow | grep lhy01
lhy01:!$6$VyBTUdCM$XjFdH4m0VLdC/TfgJw62N9nBUMV/HrAUMTLZylf9TlH8OBJJP9Ur0nd6PY6VGP7DM.o6mjyNdtitU5BX5whxU1:17693:0:99999:7:::

同样-U选项解锁。

上述两个命令可以相互加锁解锁。

这个命令最重要的是,管理员用shell脚本给普通用户批量设置初始密码的时候,可以使用--stdin选项。使用此选项可以通过管道符|来达到一条命令无交互的设置密码。

[root@localhost ~]# echo "hongyangxxx" | passwd --stdin lhy02
Changing password for user lhy02.
passwd: all authentication tokens updated successfully.

如果想达到同样的效果,而不是用--stdin选项,也可以使用echo的-e选项(转义)

[root@localhost ~]# echo -e "123456789\n123456789" | passwd lhy05
Changing password for user lhy05.
New password: BAD PASSWORD: The password fails the dictionary check - it is too simplistic/systematic
Retype new password: passwd: all authentication tokens updated successfully.

虽然会有提示说密码不安全,但是密码已经生效了。

×建议密码10位起,包括数字,大小写字母以及特殊符号,尽量避免规律性。

3.6mkpasswd命令

mkpasswd命令可以生成一个随机字符串作为你的密码。

需要安装expect包。

[root@localhost ~]# yum install -y expect

默认生成9位带大小写字母,数字和特殊符号的密码。

也可以使用选项生成特定密码。

-l(length)长度

-s(special)最少特殊符号位

-d(digital)最少数字位

这样管理员在批量生成大量用户的时候,就可以创建没有规律的密码了。

转载于:https://my.oschina.net/u/3866688/blog/1829111

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值