linux学习(十一)用户和用户组管理

mac2022-06-30  17

一、用户文件

文件:/etc/passwd

这个文件记录了用户了用户名,用户id,所属组,家目录,shell信息:

[root@iZ25lzba47vZ ~]# tail -n3 /etc/passwd ruanwenwu:x:1003:1003::/home/ruanwenwu:/bin/bash apache:x:48:48:Apache:/usr/share/httpd:/sbin/nologin test:x:1004:1004::/home/test:/bin/bash

这个文件被用冒号分成7段,分别为:用户名:密码:用户id:组id:注释:家目录:shell。

二、密码文件

文件:/etc/shadow。基本不修改,了解一下就好了。

[root@iZ25lzba47vZ ~]# tail -n3 /etc/shadow ruanwenwu:!!:17228:0:99999:7::: apache:!!:17250:::::: test:$6$H7i3JVvg$96lyFVtEnHK6tt20Lx50ZOQW6QVzK.UGWfderquBpdYhWzv33GWAvX8UyT7aaaxfxEE81tq2dsHxDQrs3CKT4/:17465:0:99999:7:::

这个文件,每行也是代表一个用户,也是被分号分割,成9段:

用户名,跟/etc/passwd对应。加密密码,加密算法升级为SHA512散列加密算法,如果密码位为“!!”或“*”代表没有密码,不能登录,其shell为/sbin/nologin。密码最后一次修改的时间,以1970年1月1日作为标准时间,每过一天时间戳加1两次密码修改的间隔时间,要过多少天才可以更改密码,默认是0,即不限制密码的有效期,密码多少天后到期,默认为99999。若设置成20,即20天后到期,必须修改密码,不然登录不了系统。密码到期前的警告期限,若设置为6,即到期前6天将通知用户。账号失效宽限期(和第5字段相对比)。若设置为2,到期过后2天后再不修改密码,用户锁定。账号的生命周期(要用时间戳表示),到了指定的期限,账号将失效。保留字段,没有特别意义

三、用户组管理

3.1 添加用户组

基本用法:

[root@iZ25lzba47vZ ~]# useradd grp1 [root@iZ25lzba47vZ ~]# tail -n3 /etc/group apache:x:48: test:x:1004: grp1:x:1005: [root@iZ25lzba47vZ ~]# groupadd grp2 [root@iZ25lzba47vZ ~]# !t tail -n3 /etc/group test:x:1004: grp1:x:1005: grp2:x:1006:

添加组的时候,指定组id:

[root@iZ25lzba47vZ ~]# groupadd -g 1008 grp3 [root@iZ25lzba47vZ ~]# 1t -bash: 1t: command not found [root@iZ25lzba47vZ ~]# !t tail -n3 /etc/group grp1:x:1005: grp2:x:1006: grp3:x:1008:

3.2 删除组:

[root@iZ25lzba47vZ ~]# groupdel grp3 [root@iZ25lzba47vZ ~]# !t tail -n3 /etc/group test:x:1004: grp1:x:1005: grp2:x:1006:

如果这个组里还有用户是不能被删除的:

[root@iZ25lzba47vZ ~]# useradd -g grp2 test2 [root@iZ25lzba47vZ ~]# tail -n3 /etc/passwd test:x:1004:1004::/home/test:/bin/bash grp1:x:1005:1005::/home/grp1:/bin/bash test2:x:1006:1006::/home/test2:/bin/bash [root@iZ25lzba47vZ ~]# groupdel grp2 groupdel: cannot remove the primary group of user 'test2'

可能大家都注意到了,在文件/etc/group中可以看到所有组的信息。

四、用户管理

4.1 增加用户

以比较全的参数创建:

[root@iZ25lzba47vZ ~]# useradd -g grp2 -u 3000 -d /home/ud/ -s /sbin/nologin test3 [root@iZ25lzba47vZ ~]# !t tail -n3 /etc/passwd grp1:x:1005:1005::/home/grp1:/bin/bash test2:x:1006:1006::/home/test2:/bin/bash test3:x:3000:1006::/home/ud/:/sbin/nologin

这条命令的意思是,-g指定组名,-u指定uid,-d指定家目录,-s指定shell。

如果组名不存在时会怎样呢?

[root@iZ25lzba47vZ ~]# useradd -g 4000 test4 useradd: group '4000' does not exist

添加用户时,指定扩展组。

在添加之前,我们先看一下id命令。

[root@iZ25lzba47vZ ~]# id ruanwenwu uid=1003(ruanwenwu) gid=1003(ruanwenwu) groups=1003(ruanwenwu)

id命令得到用户的uid,gid,和groups(扩展组)。

那么在添加用户时怎么指定扩展组呢?

[root@iZ25lzba47vZ ~]# useradd test6 -G test,grp1,grp2 [root@iZ25lzba47vZ ~]# id test6 uid=1006(test6) gid=1007(test6) groups=1007(test6),1004(test),1005(grp1),1006(grp2)

4.2 删除用户

[root@iZ25lzba47vZ ~]# userdel test3 [root@iZ25lzba47vZ ~]# ls -ld /home/ud drwx------ 2 3000 grp2 4096 Oct 28 23:31 /home/ud

发现,删除了用户后,它的家目录还在,如果要删除用户时,连家目录一起删掉:

[root@iZ25lzba47vZ ~]# userdel -r test2 [root@iZ25lzba47vZ ~]# ls -ld /home/test2 ls: cannot access /home/test2: No such file or directory

4.3 修改用户

usermod -g 111/组名 username

usermod -d /home/dfd usermod -s /sbin/nologin usermod -u 111 user1 usermod -G grp2 aming usermod -G grp2,user5 user1

五、用户密码管理

/etc/shadow里有!!说明该没有密码,或者被锁定,只有一个!也是被锁定。我们看看当前系统的用户的密码锁定情况:

[root@iZ25lzba47vZ ~]# tail -n3 /etc/shadow test:$6$H7i3JVvg$96lyFVtEnHK6tt20Lx50ZOQW6QVzK.UGWfderquBpdYhWzv33GWAvX8UyT7aaaxfxEE81tq2dsHxDQrs3CKT4/:17465:0:99999:7::: grp1:!!:17467:0:99999:7::: test6:!!:17467:0:99999:7:::

 

以上说明,test用户可以登录,grp1用户,test6用户 不能登录。现在我们锁定test用户: [root@iZ25lzba47vZ ~]# passwd -l test Locking password for user test. passwd: Success [root@iZ25lzba47vZ ~]# !t tail -n3 /etc/shadow test:!!$6$H7i3JVvg$96lyFVtEnHK6tt20Lx50ZOQW6QVzK.UGWfderquBpdYhWzv33GWAvX8UyT7aaaxfxEE81tq2dsHxDQrs3CKT4/:17465:0:99999:7::: grp1:!!:17467:0:99999:7::: test6:!!:17467:0:99999:7:::

 解锁:

[root@iZ25lzba47vZ ~]# passwd -u test Unlocking password for user test. passwd: Success

 usermod -L是加锁的意思,对应的usermod -U是解锁。

[root@iZ25lzba47vZ ~]# usermod -L test [root@iZ25lzba47vZ ~]# !t tail -n3 /etc/shadow test:!$6$H7i3JVvg$96lyFVtEnHK6tt20Lx50ZOQW6QVzK.UGWfderquBpdYhWzv33GWAvX8UyT7aaaxfxEE81tq2dsHxDQrs3CKT4/:17465:0:99999:7::: grp1:!!:17467:0:99999:7::: test6:!!:17467:0:99999:7::: [root@iZ25lzba47vZ ~]# usermod -U test [root@iZ25lzba47vZ ~]# !t tail -n3 /etc/shadow test:$6$H7i3JVvg$96lyFVtEnHK6tt20Lx50ZOQW6QVzK.UGWfderquBpdYhWzv33GWAvX8UyT7aaaxfxEE81tq2dsHxDQrs3CKT4/:17465:0:99999:7::: grp1:!!:17467:0:99999:7::: test6:!!:17467:0:99999:7:::

 

转载于:https://www.cnblogs.com/doubilaile/p/7748629.html

最新回复(0)