Go...

当前位置: 首页>>世界杯太太团

细说Linux的用户和组之详解用户和组的分类

一、Linux用户分类

Linux具有三种用户:

超级管理员root:具有最高权限,UID=0 GID=0系统用户(System Account):主要服务于应用,维护系统运行,不能登录。普通用户(login-Account):登录用户

root和普通用户都比较熟悉,稍微介绍下系统用户。

典型系统用户:bin:x:1:1:bin:/bin:/sbin/nologindaemon:x:2:2:daemon:/sbin:/sbin/nologinadm:x:3:4:adm:/var/adm:/sbin/nologinshutdown:x:6:0:shutdown:/sbin:/sbin/shutdownhalt:x:7:0:halt:/sbin:/sbin/haltmail:x:8:12:mail:/var/spool/mail:/sbin/nologinuucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologinoperator:x:11:0:operator:/root:/sbin/nologingames:x:12:100:games:/usr/games:/sbin/nologingopher:x:13:30:gopher:/var/gopher:/sbin/nologinftp:x:14:50:FTP User:/var/ftp:/sbin/nologinnobody:x:99:99:Nobody:/:/sbin/nologin

不禁会问,为什么会有系统用户? Linux系统的大部分权限和安全的管理依赖于对于文件权限(读、写、执行)的管理,而用户是能够获取系统资源的权限的集合,文件权限的拥有者为用户; 当应用需要访问/操作/拥有系统的资源时,Linux就通过用户来控制/实现,这些用户就是系统用户。例如:sys:The sys user owns the default mounting point for the Distributed File Service (DFS) cache, which must exist before you can install or configure DFS on a client. The /usr/sys directory can also store installation images.ftp:Used for anonymous FTP access.nobody:Owns no files and is sometimes used as a default user for unprivileged operations.

二、Linux组

PS:此处的用户全是指普通用户,即登录用户。

组和用户的关系Linux系统以组Group方式管理用户,用户和组的对应关系为多对多,即某个用户可加入/属于一个或多个组,某个组可以有0个、1个或多个用户。

组的分类

从用户的角度,分为主组和附属组。主组:也被称为primary group、first group或initial login group,用户的默认组,用户的gid所标识的组。附属组:也被称为Secondary group或supplementary group,用户的附加组。

通过id命令可查看当前用户的主组和附属组

[root@localhost ~]# id rootuid=0(root) gid=0(root) groups=0(root),1(bin),2(daemon),3(sys),4(adm),6(disk),10(wheel)

[root@localhost ~]# id gguid=503(gg) gid=503(gg) groups=503(gg)

[root@localhost ~]# id mmuid=502(mm) gid=500(jww) groups=500(jww)

gid标识主组,groups表示用户所属的全部组(主组和附属组)

1. 用户必须有且只能有一个主组,可以有0个、1个或多个附属组,就如我们一定会有一个用来安家的房子(类同主组),还可以有n个用于投资或其他打算的房子(附属组)。

2. 主组也为默认组,当用户own某个文件或目录时,默认该文件或目录的group owner为该用户的主组(当然,可以通过chgrp修改group owner)。

3. 在/etc/passwd文件中,记录行第四个字段为gid,即用户的主组id。

4. 在/etc/group文件中,记录行第四个字段为组的成员,不显示将该组作为主组的组成员,只显示将该组作为附属组的组成员,因此,/etc/group的记录行的第四个字段没有完整地列出该组的全部成员。

5. 当通过useradd命令创建新用户时,可以通过-g参数指定已存在的某个组为其主组,若没有使用-g参数,则系统自动创建名称和用户名相同的组作为该用户的主组(前提是variable in /etc/login.defs的USERGROUPS_ENAB属性值为yes),如命令手册的useradd关于-g参数的描述所示: -g, --gid GROUP The group name or number of the user′s initial login group. The group name must exist. A group number must refer to an already existing group.

If not specified, the bahavior of useradd will depend on the USERGROUPS_ENAB variable in /etc/login.defs. If this variable is set to yes (or -U/--user-group is specified on the command line), a group will be created for the user, with the same name as her loginname. If the variable is set to no (or -N/--no-user-group is specified on the command line), useradd will set the primary group of the new user to the value specified by the GROUP variable in /etc/default/useradd, or 100 by default.

另外,可通过usermod -g 将普通用户的主组/gid设置为系统中存在的任意某个组(永久性);也可以通过newgrp暂时性变更当前用户的主组/gid(只对于当前login session有效,非永久性)。通过usermod -G 设置普通用户的附属组。