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

时间:2023-03-10 02:40:46
linux学习(十一)用户和用户组管理

一、用户文件

文件:/etc/passwd

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

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

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

二、密码文件

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

[root@iZ25lzba47vZ ~]# tail -n3 /etc/shadow
ruanwenwu:!!:::::::
apache:!!:::::::
test:$$H7i3JVvg$96lyFVtEnHK6tt20Lx50ZOQW6QVzK.UGWfderquBpdYhWzv33GWAvX8UyT7aaaxfxEE81tq2dsHxDQrs3CKT4/:::::::

这个文件,每行也是代表一个用户,也是被分号分割,成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::
test:x::
grp1:x::
[root@iZ25lzba47vZ ~]# groupadd grp2
[root@iZ25lzba47vZ ~]# !t
tail -n3 /etc/group
test:x::
grp1:x::
grp2:x::

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

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

3.2 删除组:

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

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

[root@iZ25lzba47vZ ~]# useradd -g grp2 test2
[root@iZ25lzba47vZ ~]# tail -n3 /etc/passwd
test:x::::/home/test:/bin/bash
grp1:x::::/home/grp1:/bin/bash
test2:x::::/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  -d /home/ud/ -s /sbin/nologin test3
[root@iZ25lzba47vZ ~]# !t
tail -n3 /etc/passwd
grp1:x::::/home/grp1:/bin/bash
test2:x::::/home/test2:/bin/bash
test3:x::::/home/ud/:/sbin/nologin

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

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

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

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

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

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

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

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

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

4.2 删除用户

[root@iZ25lzba47vZ ~]# userdel test3
[root@iZ25lzba47vZ ~]# ls -ld /home/ud
drwx------ grp2 Oct : /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

五、用户密码管理

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

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

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

解锁:

[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:!$$H7i3JVvg$96lyFVtEnHK6tt20Lx50ZOQW6QVzK.UGWfderquBpdYhWzv33GWAvX8UyT7aaaxfxEE81tq2dsHxDQrs3CKT4/:::::::
grp1:!!:::::::
test6:!!:::::::
[root@iZ25lzba47vZ ~]# usermod -U test
[root@iZ25lzba47vZ ~]# !t
tail -n3 /etc/shadow
test:$$H7i3JVvg$96lyFVtEnHK6tt20Lx50ZOQW6QVzK.UGWfderquBpdYhWzv33GWAvX8UyT7aaaxfxEE81tq2dsHxDQrs3CKT4/:::::::
grp1:!!:::::::
test6:!!:::::::