Linux 权限配置管理
修改文件所属用户组
使用 chgrp 可以改变一个文件的所属用户组,目标组名必须已经存在于 /etc/group。
例如修改 dir1 文件夹的所属组为 tss:
[root@101c7 ~]$ chgrp tss dir1 ; ll
total 4
-rw-------. 1 root root 1260 Sep  7 13:38 anaconda-ks.cfg
drwxr-xr-x. 2 ftp  tss    16 Sep  8 23:29 dir1
使用选项 -R 可以递归修改目录 dir1 下内容的所属组:
[root@101c7 ~]$ chgrp input -R dir1 ; ll dir1
total 0
-rw-r--r--. 1 root input 0 Sep  8 23:29 xx
修改文件所有者
使用chown命令可以修改文件或目录的所有者和所属组。目标用户名必须已经存在于/etc/passwd文件中。
例如,将文件夹dir1的所有者设置为lp,所属组设置为tape:
[root@101c7 ~]$ chown lp:tape dir1 ; ll
total 4
-rw-------. 1 root root 1260 Sep  7 13:38 anaconda-ks.cfg
drwxr-xr-x. 2 lp   tape   16 Sep  8 23:29 dir1
使用-R参数可以递归修改目录下的文件或子目录的所有者和所属组:
[root@101c7 ~]$ chown -R ftp:ftp dir1 ; ll dir1
total 0
-rw-r--r--. 1 ftp ftp 0 Sep  8 23:29 xx
修改文件权限
使用chmod设置权限(只有文件所有者和 root 能执行),可以使用数字或字符来更改,数字类型采用数字累加来代表:
| 数字 | 字符 | 权限(文件/目录) | 
|---|---|---|
| 4 | r | 读取文件内容/查看目录下的文件或目录名称(无法看到属性) | 
| 2 | w | 修改文件内容(不包含删除)/在目录下新建、删除、重命名文件或目录 | 
| 1 | x | 文件可以被系统执行/进入目录、遍历目录 | 
特殊权限:
| 数字 | 权限(文件/目录) | 
|---|---|
| 4 | SUID(用在文件上) | 
| 2 | SGID | 
| 1 | SBIT(用在目录上) | 
例如修改dir1文件夹的权限为rwxrw---x:
[root@101c7 ~]$ chmod 761 dir1 ; ll
total 4
-rw-------. 1 root root 1260 Sep  7 13:38 anaconda-ks.cfg
drwxrw---x. 2 ftp  tss    16 Sep  8 23:29 dir1
用字符类型改变文件权限方式:
- 用
u、g、o、a分别表示所有者、组、其他人和所有人。 - 用
+、-、=分别表示增加、去除、设置权限。如果没有指定权限,则该权限不会发生变化。 
例如设置 dir1 文件夹及其下的所有文件权限为 rwxrwxr--:
[root@101c7 ~]$ chmod -R u=rwx,g=rwx,o=r dir1
[root@101c7 ~]$ ll dir1
total 0
-rwxrwxr--. 1 ftp root 0 Sep  8 23:29 xx
设置 dir 文件夹禁止进入:
[root@101c7 ~]$ chmod a-x dir1 ; ll
total 4
-rw-------. 1 root root 1260 Sep  7 13:38 anaconda-ks.cfg
drw-rw-r--. 2 ftp  tss    16 Sep  8 23:29 dir1
要给文件 2 设置 SUID/SGID 的权限(也可以使用 u+s,g+s,o+t 命令来设置 SUID、SGID、SBIT),可以使用以下命令:
[root@101c7 ~]$ chmod 6755 2; ls -l 2
-rwsr-sr-x. 1 root root 0 Sep  9 07:57 2
当用户没有执行权限却设置了特殊权限时,特殊权限中的 s 和 t 会变成 S 和 T,表示无效权限。例如:
[root@101c7 ~]$ chmod 7666 2; ll 2
-rwSrwSrwT. 1 root root 0 Sep  9 08:57 2
修改默认权限
文件创建时默认(umask 为 000 的情况)权限是 666,文件夹是 777,可以使用 umask 来对这一权限进一步削弱。
root 用户的 umask 默认为 022,一般用户为 002。使用 umask 修改的值必须写入到 .profile 文件中才能一直生效。
查看目前用户在新建文件和目录时的权限默认值:
[root@101c7 audit]$ umask
0022
上述命令表示新建文件不包括属组和其他用户的写权限。如果要添加属组的写权限,需要定义 umask 值:
[root@101c7 audit]$ umask 002 ; umask
0002
访问控制列表
在默认权限无法满足要求时,可以考虑使用访问控制列表。
ACL 访问控制列表
ACL 是 Access Control List 的缩写,主要用于提供传统权限之外的具体权限设置。ACL 可以针对单个用户或组、单个文件或目录进行权限控制。
默认情况下,系统会自动启用 ACL 支持。可以使用 dumpe2fs -h 命令查询挂载参数:
[root@101c7 ~]$ mount | grep sd
/dev/sda1 on /boot type xfs (rw,relatime,seclabel,attr2,inode64,noquota)
/dev/sdb4 on /root/sdb4m type ext3 (rw,relatime,seclabel,data=ordered)
/dev/sdb5 on /ext333 type ext3 (rw,relatime,seclabel,data=ordered)
[root@101c7 ~]$ dumpe2fs -h /dev/sdb4 | grep acl
dumpe2fs 1.42.9 (28-Dec-2013)
Default mount options:    user_xattr acl
若要手动挂载 ACL,可以使用 mount -o remount,acl / 命令,或者修改启动配置 /etc/fstab。
设置 ACL
可以使用 setfacl 来设置某个文件或目录的 ACL 规定,命令用法如下:
setfacl [-bkRd] [{-m|-x} acl 参数] 目标
参数说明:
| 参数 | 说明 | 
|---|---|
| -m | 设置后续的 ACL 参数给文件使用 | 
| -x | 删除后续的 ACL 参数 | 
| -b | 删除所有的 ACL 设置参数 | 
| -k | 删除默认的 ACL 参数 | 
| -R | 递归设置 | 
| -d | 设置默认 ACL 参数,只对目录有效,目录下新建的数据都会用此默认值 | 
针对单个用户设置,例如给用户 user1 设置目录 dir1 的读取和执行权限:
[root@101c7 sdb4m]$ setfacl -m u:user1:rx dir1
[root@101c7 sdb4m]$ ll -d dir1
drwxr-xr-x+ 2 root root 2048 Sep 13 07:34 dir1
可以看到权限部分多了一个加号 +。
查询 ACL
要查询特殊权限,可以使用 getfacl 命令,该命令参数与 setfacl 相同。例如,要查询 dir1 的权限内容:
[root@101c7 sdb4m]$ getfacl dir1
# file: dir1
# owner: root
# group: root
user::rwx
user:user1:r-x
group::r-x
mask::r-x
other::r--
其中,user、group、other 代表普通权限。另外,还有一条刚设置的 user1 权限。
有效权限
ACL 中的 mask 规定了最大权限,也就是单独设置的权限不能超过 mask 显示的权限。
首先,给用户组 user1 设置目录 dir1 的写权限:
[root@101c7 sdb4m]$ setfacl -m g:user1:w dir1
然后,将 dir1 的 mask 权限设置为只读:
[root@101c7 sdb4m]$ setfacl -m m:r dir1
[root@101c7 sdb4m]$ getfacl dir1
# file: dir1
# owner: root
# group: root
user::rwx
user:user1:r-x                  #effective:r--
group::r-x                      #effective:r--
group:user1:-w-                 #effective:---
mask::r--
other::r-x
这样,effective 后面显示的才是有效权限。
权限继承
可以使用 d:[ug]:用户列表:[rwx] 命令设置 ACL 权限被后续子文件夹和目录继承:
[root@101c7 sdb4m]$ setfacl -m d:g:user1:w dir1
[root@101c7 sdb4m]$ mkdir dir2 ; ls -ld dir2 ; getfacl dir2
drwxrwxr-x+ 2 root root 2048 Sep 13 07:56 dir2
# file: dir2
# owner: root
# group: root
user::rwx
group::r-x
group:user1:-w-
mask::rwx
other::r-x
default:user::rwx
default:group::r-x
default:group:user1:-w-
default:mask::rwx
default:other::r-x
这样设置后,dir2 目录会继承 dir1 目录的 ACL 权限。