Linux基础(day12)

3.4 usermod命令

usermod命令介绍

  • usermod命令,更改用户属性的命令
  • 用户的信息保存在/etc/passwd文件中

usermod命令用法

  • usermod和useradd命令用法类似

usermod格式

  • usermod -u 111 username 修改用户的uid
  • usermod -g 123 username 这里的123能够是一个组名,也能够是一个gid
  • usermod -d /home/asklea username 指定它的家目录
  • usermod -s /sbin/nologin 指定它的shell
  • usermod -c 填写帐号的备注信息
  • usermod -e 帐户到期时间,格式“YYYY-MM-DD”
  • usermod -L 锁定用户,禁止登录
  • usermod -U 解锁用户,容许登陆
  • usermod -G 扩展组,指定多个组
[root@localhost /]# id hanfeng      这个组不只包括了前面的gid,还能够包括其余的扩展组
uid=1000(hanfeng) gid=1000(hanfeng) 组=1000(hanfeng)
[root@localhost /]# groupadd ha1
[root@localhost /]# usermod -G ha1 hanfeng
[root@localhost /]# id hanfeng
uid=1000(hanfeng) gid=1000(hanfeng) 组=1000(hanfeng),1001(ha1)
[root@localhost /]# useradd user3
[root@localhost /]# usermod -G user3 hanfeng
[root@localhost /]# id hanfeng      这里会发现指定-G以后,user3组替代了ha1组
uid=1000(hanfeng) gid=1000(hanfeng) 组=1000(hanfeng),1002(user3)
[root@localhost /]# usermod -G ha1,user3 hanfeng   同时添加多个组
[root@localhost /]# id hanfeng
uid=1000(hanfeng) gid=1000(hanfeng) 组=1000(hanfeng),1001(ha1),1002(user3)
一个用户,它能够属于多个组,可是这个gid只有一个,咱们就能够把除了gid以外的组,称之为扩展组
  • usermod -g 只能指定惟一一个组
[root@localhost /]# usermod -g ha1,user3 hanfeng
usermod:“ha1,user3”组不存在
[root@localhost /]# usermod -g ha1 hanfeng
[root@localhost /]# !id
id hanfeng
uid=1000(hanfeng) gid=1001(ha1) 组=1001(ha1),1002(user3)

usermod中-g和-G的区别

--g与-G的区别,-g只能只能指定一个组,而-G能够指定多个组shell

  • useradd -G 指定一个或多个扩展用户组

3.5 用户密码管理

passwd命令

  • passwd命令,修改用户密码的命令
  • 特殊权限set_uid,可使普通用户更改本身的密码
[root@localhost /]# passwd      这里直接就可更改root用户的密码
更改用户 root 的密码 。
新的 密码:
[root@localhost /]# passwd user3     在命令后面加上普通用户,便可更改密码
更改用户 user3 的密码 。
新的 密码:
从新输入新的 密码:
passwd:全部的身份验证令牌已经成功更新。
[root@localhost /]# tail /etc/shadow     会看到user3这个多了一串加密的字符串
systemd-bus-proxy:!!:17459::::::
systemd-network:!!:17459::::::
dbus:!!:17459::::::
polkitd:!!:17459::::::
tss:!!:17459::::::
postfix:!!:17459::::::
sshd:!!:17459::::::
chrony:!!:17459::::::
hanfeng:!!:17470:0:99999:7:::
user3:$6$1WEfFti.$gyRBbtnDqmAnsHDST5hIsE1oTlNhBAvasK3CTvCHIE2Bnga.mOZ7LWPR3xbfRz4vAtOlO6Z01iA47.evT90Nk0:17470:0:99999:7:::
  • 若用户密码为空,则会显示双感叹号!!,意味着这个用户不能登陆

  • 若用户密码为星号*,则表示这个用户的密码是被锁定的,不能使用的,也是不能登陆的
[root@localhost ~]# head /etc/shadow
root:$6$L6sm5Qq1aGOBJOIi$okCMP5JFEiDBpcaqreOtF54FA5.qyUTG/WfAj45Od7ONdM1Ut9buK9i4xdOcaBwUaX4x3oGd/wSJl4OWh0xOJ/::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:::

总结:密码为!!或是*号

  • 若用户的密码锁定,或是过时等状况,只要是 !! 号或 * 号,就说明这个用户不能登陆,密码时有问题的

passwd命令用法

passwd命令锁定和解锁用户

  • passwd -l username 锁定用户的密码
[root@hf-01 ~]# passwd -l hanfeng     会发现user10用户的密码被锁定了
锁定用户 hanfeng 的密码 。
passwd: 操做成功
[root@hf-01 ~]# tail -n7 /etc/shadow
hanfeng:!!$6$jV74kWmS$GPP5amnrGjHQuAxqAAxyKHmLp6lOXd64pWl1YxVu5VfJSSJPFk4DvLhWhYM1.BfmXA32leqCMLcJYPeCmdBK/.:17469:0:99999:7:::
user1:!!:17465:0:99999:7:::
haha:!!:17469:0:99999:7:::
user2:!!:17469:0:99999:7:::
user4:!!:17469:0:99999:7:::
user8:!!:17469:0:99999:7:::
user10:!!:17469:0:99999:7:::
在密码文件的/etc/shadow中,会发现hanfeng用户的密码加上两个叹号!!。说明它被锁定了
  • passwd -u user 解锁用户密码
[root@hf-01 ~]# passwd -u hanfeng
解锁用户 hanfeng 的密码。
passwd: 操做成功
[root@hf-01 ~]# tail -n7 /etc/shadow
hanfeng:$6$jV74kWmS$GPP5amnrGjHQuAxqAAxyKHmLp6lOXd64pWl1YxVu5VfJSSJPFk4DvLhWhYM1.BfmXA32leqCMLcJYPeCmdBK/.:17469:0:99999:7:::
user1:!!:17465:0:99999:7:::
haha:!!:17469:0:99999:7:::
user2:!!:17469:0:99999:7:::
user4:!!:17469:0:99999:7:::
user8:!!:17469:0:99999:7:::
user10:!!:17469:0:99999:7:::
在/etc/shadow的密码文件中,会发现hanfeng用户下的两个叹号 !! 消失了,说明该用户解锁成功。

usermod命令锁定和解锁用户

  • usermod -L username 锁定用户的密码
[root@hf-01 ~]# usermod -L hanfeng
[root@hf-01 ~]# !tail
tail -n7 /etc/shadow
hanfeng:!$6$jV74kWmS$GPP5amnrGjHQuAxqAAxyKHmLp6lOXd64pWl1YxVu5VfJSSJPFk4DvLhWhYM1.BfmXA32leqCMLcJYPeCmdBK/.:17469:0:99999:7:::
user1:!!:17465:0:99999:7:::
haha:!!:17469:0:99999:7:::
user2:!!:17469:0:99999:7:::
user4:!!:17469:0:99999:7:::
user8:!!:17469:0:99999:7:::
user10:!!:17469:0:99999:7:::
在/etc/shadow密码文件中,会发现密码位置出现了一个叹号 ! ,无论是出现一个叹号,仍是两个叹号,只要是叹号开头了,就表示该用户被锁定了,不能登陆了。
  • usermod -U username 解锁用户的密码
[root@hf-01 ~]# usermod -U hanfeng
[root@hf-01 ~]# !tail
tail -n7 /etc/shadow
hanfeng:$6$jV74kWmS$GPP5amnrGjHQuAxqAAxyKHmLp6lOXd64pWl1YxVu5VfJSSJPFk4DvLhWhYM1.BfmXA32leqCMLcJYPeCmdBK/.:17469:0:99999:7:::
user1:!!:17465:0:99999:7:::
haha:!!:17469:0:99999:7:::
user2:!!:17469:0:99999:7:::
user4:!!:17469:0:99999:7:::
user8:!!:17469:0:99999:7:::
user10:!!:17469:0:99999:7:::

passwd命令给用户设定一个密码

passwd --stdin用法

  • passwd --stdin username 给用户设定一个密码
    • 直接更改用户的密码
[root@hf-01 ~]# passwd --stdin user10
更改用户 user10 的密码 。
hanfeng
passwd:全部的身份验证令牌已经成功更新。

[root@hf-01 ~]# echo "123123" |passwd --stdin user10      这会在shell中常常使用到
更改用户 user10 的密码 。
passwd:全部的身份验证令牌已经成功更新。

echo -e的用法

  • echo -e 能够在一行命令当中出现换行符
    • 反斜杠加n字母 \n ,表示换行。
    • 反斜杠加t字母 \t ,表示table
[root@hf-01 ~]# echo "123\nsss"
123\nsss
[root@hf-01 ~]# echo -e "123\nsss"
123
sss
[root@hf-01 ~]# echo -e "123\tsss"
123	sss
  • echo -e "123456\n123456" |passwd user10 一次性更改用户的密码
[root@hf-01 ~]# echo -e "123456\n123456" |passwd user10
更改用户 user10 的密码 。
新的 密码:无效的密码: 密码少于 8 个字符
从新输入新的 密码:passwd:全部的身份验证令牌已经成功更新。
[root@hf-01 ~]# passwd user10
更改用户 user10 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
从新输入新的 密码:
passwd:全部的身份验证令牌已经成功更新。
这里会发现echo -e "***\n***" |passwd username这里更改用户密码,会比passwd username更改用户密码要方便不少,只需一次就可更改为功

设定密码的原则

原则:密码超过十位数起,要包括数字、大小写字母、特殊符号,这样才能成为一个合格的密码(密码不能有规律性,不能有本身的姓名、电话等,不然会很容易被猜到)ssh

3.6mkpasswd命令

mkpasswd命令介绍

  • mkpasswd命令,全称 make password,它是用来生成密码的一个工具
  • 安装包yum install -y expect (系统默认没有这个包,须要安装)
    • 安装这个包就可使用mkpasswd这个命令,这个命令能够生成随机的字符串,能够这这个随机字符串作成一个密码
[root@hf-01 ~]# mkpasswd    它会随机生成一个大小写、数字、符号的字符串,长度是 九位 
UgftrS^57

mkpasswd命令用法

  • mkpasswd -l number 指定生成字符串的长度 (默认为9位数)
[root@hf-01 ~]# mkpasswd -l 12  生成了长度为12位的字符串
9ue}dxdor2MM
  • mkpasswd -s number 指定字符串中有几个特殊符号
    • 也能够取消特殊符号,数字为0 便可
[root@hf-01 ~]# mkpasswd -l 12 -s 3
xx[jbMk<9'M9
[root@hf-01 ~]# mkpasswd -l 12 -s 0
bevtic7Fu7Kh
在写脚本中常常会用到mkpasswd命令,再生成后,还需记录到文本文档中去
相关文章
相关标签/搜索