System-Day6

System-Day6


用户账号

useradd

-u:设置 UID 标记号
-d:指定宿主目录,缺省为 /home/用户名
-g:指定所属的基本组
-G:指定所属的附加组
-e:指定账号失效时间
-s:指定用户的登录Shell

为账户设置密码的命令为passwd,管理员root可以修改任何用户的口令,所有用户(包括普通用户)都可以修改自己的口令。
常用命令选项:

-d:清空用户的密码,使之无需密码即可登录
-l:锁定用户账号 【在shadow中的对应密码串前加两个叹号 !! 】
-S:查看用户账号的状态(是否被锁定)
-u:解锁用户账号 【解除锁定时添加的两个叹号 !】
--stdin:从标准输入(比如管道)取密码

查看结果可以利用grep命令,从/etc/passwd与/etc/shadow筛选。

//创建一个用户test01 UID为600 加入ROOT组 家目录存放在/opt/test01 指定登陆shell为/sbin/nologin
[root@localhost ~]# useradd -u 600 -G root -d /opt/test01 -s /sbin/nologin test01
[root@localhost ~]# grep test01 /etc/passwd /etc/shadow
/etc/passwd:test01:x:600:600::/opt/test01:/sbin/nologin
/etc/shadow:test01:!!:17673:0:99999:7:::
[root@localhost ~]# ls -d /opt/test01/
/opt/test01/

//没有加密前第二个字段为 !!
[root@localhost ~]# grep test01 /etc/shadow|grep "\!\!"
test01:!!:17673:0:99999:7:::
//设置test01密码为test
[root@localhost ~]# echo test | passwd --stdin test01
Changing password for user test01.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# grep test01 /etc/shadow
test01:$1$3O449d3H$7El8LH0Bj1IFHCxDaNXw80:17673:0:99999:7:::

//没有锁定前
[root@localhost ~]# grep test01 /etc/shadow
test01:$1$3O449d3H$7El8LH0Bj1IFHCxDaNXw80:17673:0:99999:7:::
//锁定用户
[root@localhost ~]# passwd -l test01
Locking password for user test01.
passwd: Success
//查看用户状态,已锁定
[root@localhost ~]# passwd -S test01
test01 LK 2018-05-21 0 99999 7 -1 (Password locked.)
//你淘宝后第二个字段 前多出 !!
[root@localhost ~]# grep test01 /etc/shadow
test01:!!$1$3O449d3H$7El8LH0Bj1IFHCxDaNXw80:17673:0:99999:7:::

//解锁test01用户
[root@localhost ~]# passwd -u test01
Unlocking password for user test01.
passwd: Success
//查看当前用户状态,已解锁
[root@localhost ~]# passwd -S test01
test01 PS 2018-05-21 0 99999 7 -1 (Password set, MD5 crypt.)
//解锁后,第二个字段前面 !! 消失
[root@localhost ~]# grep test01 /etc/shadow
test01:$1$3O449d3H$7El8LH0Bj1IFHCxDaNXw80:17673:0:99999:7:::

修改、删除、查询用户

usermod
-l:更改用户的登录名
-L:锁定用户账户 【在shadow中的对应密码串前加一个叹号 ! 】
-U:解锁用户账户 【解除锁定时添加的一个叹号 !】
-u、-d、-e、-g、-G、-s:与useradd命令相同

  • 创建一个用户test02
[root@localhost ~]# useradd test02
[root@localhost ~]# grep test02 /etc/passwd /etc/shadow
/etc/passwd:test02:x:601:601::/home/test02:/bin/bash
/etc/shadow:test02:!!:17673:0:99999:7:::
  • 修改test01用户家目录为/opt/test02
[root@localhost ~]# grep test02 /etc/passwd
test02:x:601:601::/opt/test02:/bin/bash
  • 修改test02用户过期时间为2050-12-31
//未修改前
[root@localhost ~]# grep test02 /etc/shadow
test02:!!:17673:0:99999:7:::
[root@localhost ~]# usermod -e 2050-12-31 test02
//修改后
[root@localhost ~]# grep test02 /etc/shadow
test02:!!:17673:0:99999:7::29584:
  • 修改test02登陆shell为/sbin/nologin
[root@localhost ~]# grep test02 /etc/passwd
test02:x:601:601::/opt/test02:/bin/bash
[root@localhost ~]# usermod -s /sbin/nologin test02
[root@localhost ~]# grep test02 /etc/passwd
test02:x:601:601::/opt/test02:/sbin/nologin
  • 修改test02基本组为root组
[root@localhost ~]# id test02
uid=601(test02) gid=601(test02) groups=601(test02)
[root@localhost ~]# usermod -g root test02
[root@localhost ~]# id test02
uid=601(test02) gid=0(root) groups=0(root)
  • 删除test02用户保留其家目录及邮件文件
[root@localhost ~]# userdel test02
[root@localhost ~]# id test02
id: test02: No such user

组账号的管理控制

groupadd

-A:定义组管理员列表
-a:添加组成员,每次只能加一个
-d: 删除组成员,每次只能删一个
-M:定义组成员用户列表,可设置多个

  • 新建组账号testgrp 将GID设为700
[root@localhost ~]# groupadd -g 700 testgrp
[root@localhost ~]# grep testgrp /etc/group /etc/gshadow
/etc/group:testgrp:x:700:
/etc/gshadow:testgrp:!::
  • 为testgrp组添加三个成员用户 user1 user2 user3
//建立3个用户
[root@localhost ~]# useradd user1 
[root@localhost ~]# useradd user2
[root@localhost ~]# useradd user3
//未加入前
[root@localhost ~]# grep testgrp /etc/group
testgrp:x:700:

[root@localhost ~]# gpasswd -M user1,user2,user3 testgrp
//加入后
[root@localhost ~]# grep testgrp /etc/group
testgrp:x:700:user1,user2,user3
  • 从testgrp组删除一个成员
[root@localhost ~]# grep testgrp /etc/group
testgrp:x:700:user1,user2,user3
[root@localhost ~]# gpasswd -d user1 testgrp
Removing user user1 from group testgrp
[root@localhost ~]# grep testgrp /etc/group
testgrp:x:700:user2,user3
  • 重新定义testgrp 组成员列表 user4
[root@localhost ~]# useradd user4
[root@localhost ~]# id user4
uid=604(user4) gid=604(user4) groups=604(user4)
[root@localhost ~]# grep testgrp /etc/gr
grep: /etc/gr: No such file or directory
[root@localhost ~]# grep testgrp /etc/group
testgrp:x:700:user2,user3
[root@localhost ~]# gpasswd -M user3,user4 testgrp
[root@localhost ~]# grep testgrp /etc/group
testgrp:x:700:user3,user4
  • 为testgrp 组添加一个组管理员user1
[root@localhost ~]# grep testgrp /etc/gshadow
testgrp:!::user3,user4
[root@localhost ~]# gpasswd -A user1 testgrp
[root@localhost ~]# grep testgrp /etc/gshadow
testgrp:!:user1:user3,user4
  • 切换用户user1 把从自己添加为testgrp组成员 删除组成员test4
[root@localhost ~]# su - user1
[user1@localhost ~]$ whomai
-bash: whomai: command not found
[user1@localhost ~]$ whoami
user1
[user1@localhost ~]$ grep testgrp /etc/group
testgrp:x:700:user3,user4
[user1@localhost ~]$ gpasswd -a user1 testgrp
Adding user user1 to group testgrp
[user1@localhost ~]$ grep testgrp /etc/group
testgrp:x:700:user3,user4,user1
[user1@localhost ~]$ gpasswd -d user4 testgrp
Removing user user4 from group testgrp
[user1@localhost ~]$ grep test /etc/group
root:x:0:test01
test01:x:600:
testgrp:x:700:user3,user1
[user1@localhost ~]$ exit
  • 删除组账号
[root@localhost ~]# grep testgrp /etc/group
testgrp:x:700:user3,user1
[root@localhost ~]# groupdel testgrp 
[root@localhost ~]# grep testgrp /etc/group

猜你喜欢

转载自www.cnblogs.com/fina/p/9071506.html
今日推荐