咱们知道,在Linux操做系统中,传统的权限管理分是以三种身份(属主、属組以及其它人)搭配三种权限(可读、可写以及可执行),而且搭配三种特殊权限(SUID,SGID,SBIT),来实现对系统的安全保护。可是,随着业务和需求的发展和扩大,仅有的这种模式已经不能知足当前复杂环境下的权限控制需求。windows
好比,当前有一个/data目录,如今须要A組成员可以可写,B組成员仅读,C組成员可读可写可执行,此时怎么办呢?安全
对于以上的需求,仅仅依托现有的传统权限管理模式,是没法实现的。为了解决该类型的问题,Linux 开发出了一套新的文件系统权限管理方法,叫作 文件访问控制列表 ACL(Access Control Lists)。经过使用 ACL,能够完美解决如上类型的需求问题。bash
那么下来来看,什么是访问控制列表?
ide
ACL 是 Access Control List 的缩写,主要目的是针对在传统的三种身份和三种权限以外,提供更加细化的局部权限设定。官方手册来说,它主要针对用户、用户组、以及掩码方面控制权限。
简单去理解就是,ACL 能够针对单个用户、单个用户组来进行权限细化的控制。
而在windows系统上,没有这个ACL,ACL是类Unix(Unix-like)操做系统权限的额外支持项目,所以要使用ACL必需要有文件系统的支持才行。主要包括ReiserFS, EXT2/EXT3/ext4, JFS, XFS等文件系统。学习
须要注意的是,因为ACL是必须依托文件系统的,所以并非每一个文件系统都支持ACL。好比咱们 win 平台的 NTFS 文件系统,FAT32 文件系统是不支持ACL的。在Linux平台上,常见的支持ACL的文件爱你系统有以下几类,如 EXT2/EXT3/ext4, JFS, XFS等等。
测试
那么,如何查看你的系统是否支持 ACL 呢?spa
咱们能够经过以下操做来查看:操作系统
[root@lh ~]# tune2fs -l /dev/vda1 | grep options Default mount options: user_xattr acl [root@lh ~]# dumpe2fs /dev/vda1 | grep options dumpe2fs 1.41.12 (17-May-2010) Default mount options: user_xattr acl
以上两条命令任选一个便可!orm
若是在输出的信息,默认挂载选项中有acl这个标识,就表明你的文件系统是支持的。递归
假设,你的文件系统不支持或者支持可是并无显示这个acl标识怎么办呢?针对此种状况,咱们能够经过使用tune2fs来为他添加,或者mount去添加均可以。
[root@lh ~]# tune2fs -o acl /dev/vda1 tune2fs 1.41.12 (17-May-2010)
ACL相关命令详解
介绍完了 ACL 是什么,也说了如何使文件系统支持 ACL 的功能,下面就来讲说如何操做。
ACL的相关的操做主要有 3 个命令,分别是 getface、setfacl和chacl,经常使用的主要是getfacl 和 setfacl。
getfacl 查看文件/目录的ACL设定内容 setfacl 设置文件/目录的ACL内容 chacl 查看和更改文件/目录的ACL内容,因为平常有setfacl,所以chacl历来不用,故本文不做介绍
getfacl 通常都是直接在后面跟你所要查看的文件或者目录的路径,所以掌握如何查看便可。操做以下:
[root@lh ~]# getfacl /tmp getfacl: Removing leading '/' from absolute path names # file: tmp # owner: root # group: root # flags: --t user::rwx group::rwx other::rwx [root@lh ~]# getfacl /etc/passwd getfacl: Removing leading '/' from absolute path names # file: etc/passwd # owner: root # group: root user::rw- group::r-- other::r--
setfacl 是使用最多的,基本 ACL 方面的操做都是它,所以它的选项也是蛮多的。首先来setfacl的使用语法:
setfacl [-bkRd] [{-m|-x} acl参数] 文件/目录路径 选项介绍: -b :删除全部的 acl 参数 -k :删除预设的 acl 参数 -R :递归设置后面的 acl 参数 -d :设置预设的 acl 参数(只对目录有效,在该目录新建的文件也会使用此ACL默认值) -m :设置(修改)后面 acl 参数 -x :删除后面指定的 acl 参数
ACL 参数主要由3部分组成,组成结构以下:
三种身份:对应身份名:三种权限 [u|g|o]:[用户名|用户组名]:[rwx]
实例练习
下面来看几个实例,来理解学习 ACL 的操做:
如今在/mnt目录,有文件test和目录dir,它们的权限都是600,属主和属組都是root。
[root@lh mnt]# touch test [root@lh mnt]# mkdir dir [root@lh mnt]# chmod 600 test [root@lh mnt]# chmod 600 dir [root@lh mnt]# ll total 4 drw-------. 2 root root 4096 Jul 4 17:56 dir -rw-------. 1 root root 0 Jul 4 17:56 test
如今要求完成以下要求:
一、为文件 test 增长 acl 权限,使 sunsky 用户能够可读可写
[root@lh mnt]# setfacl -m u:sunsky:rw test [root@lh mnt]# getfacl test # file: test # owner: root # group: root user::rw- user:sunsky:rw- group::--- mask::rw- other::--- [root@lh mnt]# su - sunsky # 切换到sunsky用户下,进行测试 Wellcome to Linux World [sunsky@lh ~]$ echo 1 >> /mnt/test # 很明显能写入数据 [sunsky@lh ~]$ cat /mnt/test1
二、为文件 test 增长 acl 权限,使 sun 組的全部用户都能读该文件
[root@lh mnt]# setfacl -m g:sun:r test [root@lh mnt]# getfacl test # file: test # owner: root # group: root user::rw- user:sunsky:rw- group::--- group:sun:r-- mask::rw- other::--- [root@lh mnt]# su - sun Wellcome to Linux World [sun@lh ~]$ cat /mnt/test # 很明显能查看test文件的内容 1 [sun@lh ~]$ echo 2 >> /mnt/test # 因为咱们没有给sun組成员更改权限,所以不能更改 -bash: /mnt/test: Permission denied
三、为目录 dir 增长 acl 权限,使 sun 組的全部用户都可以对该目录可读可写可执行
[root@lh mnt]# setfacl -m g:sun:rw dir [root@lh mnt]# getfacl dir # file: dir # owner: root # group: root user::rw- group::--- group:sun:rwx mask::rwx other::--- [root@lh mnt]# su - sun # 切换到sun用户下,进行测试 [sun@lh ~]$ echo "date" >> /mnt/dir/date.sh [sun@lh ~]$ bash /mnt/dir/date.sh Fri Jul 4 18:01:48 CST 2014
四、删除文件 test 上,关于 sun 組的 acl 权限
[root@lh mnt]# setfacl -x g:sun test [root@lh mnt]# getfacl test # file: test # owner: root # group: root user::rw- user:sunsky:rw- group::--- mask::rw- other::---
五、删除目录 dir 的全部 ACL 权限
[root@lh mnt]# setfacl -b dir [root@lh mnt]# getfacl dir # file: dir # owner: root # group: root user::rw- group::--- other::---
六、为目录 dir 增长默认ACL权限,使 dir 目录下新建立的文件或目录,都默认拥有 sunsky 用户可读可写可执行
[root@lh mnt]# setfacl -m d:u:sunsky:rwx dir [root@lh mnt]# getfacl dir # file: dir # owner: root # group: root user::rw- group::--- other::--- default:user::rw- default:user:sunsky:rwx default:group::--- default:mask::rwx default:other::--- [root@lh mnt]# touch /mnt/dir/sunsky [root@lh mnt]# getfacl /mnt/dir/sunsky getfacl: Removing leading '/' from absolute path names # file: mnt/dir/sunsky # owner: root # group: root user::rw- user:sunsky:rwx #effective:rw- group::--- mask::rw- other::---
在第六题中,咱们发现,在user:sunsky:rwx后面多了一个 #effective:rw-,这是为何呢?咱们在切换到sunsky用户下,看看它是否有执行该文件的权限!
[root@lh mnt]# su - sunsky Wellcome to Linux World [sunsky@lh ~]$ bash /mnt/dir/sunsky bash: /mnt/dir/sunsky: Permission denied
很明显,尽管咱们使用setfacl给了sunsky对dir目录下默认新生成的文件可读可写可执行的权限,可是依旧是没有执行权限的。这是为何呢?
咱们发现多了输出#effective:rw-,它是因为什么出来的呢?
effective生效的为rw,他是受咱们输出中的mask影响的。可是咱们发现,咱们并无设置过mask啊,为啥他默认变成rw了。这里我就来介绍一下mask!
mask 的做用是为了用来限制除了属主和其余人之外的全部用户或组的权限,mask 权限为这些用户他们可能拥有的最高权限。
若是遇到设置的用户权限与 mask 权限冲突,则用户的权限为
# effective 权限
那么,一旦一个文件被设置了 ACL,其文件原属组部分的权限将变为 MASK 权限,而并不是原来的属组权限。若是其文件原先属組权限为空,那么当你设置了mask权限以后,你的属組权限也相应改变为其mask对应的权限。
下面,咱们就继续进行第六题的实验!
[root@lh mnt]# setfacl -m m::rwx /mnt/dir/ [root@lh mnt]# getfacl /mnt/dir/ getfacl: Removing leading '/' from absolute path names # file: mnt/dir/# owner: root # group: root user::rw- group::--- mask::rwx other::--- default:user::rw- default:user:sunsky:rwx default:group::--- default:mask::rwx default:other::--- [root@lh mnt]# su - sunsky Wellcome to Linux World [sunsky@lh ~]$ bash /mnt/dir/sunsky bash: /mnt/dir/sunsky: Permission denied
奇怪了,为何我已经更改了mask为rwx了,而且effective也再也不出现了,为什么如今sunsky依旧拿不到执行权限呢?
咱们如今去看下/mnt/dir/sunsky文件的ACL权限吧。
[root@lh mnt]# getfacl /mnt/dir/sunsky getfacl: Removing leading '/' from absolute path names # file: mnt/dir/sunsky # owner: root # group: root user::rw- user:sunsky:rwx #effective:rw- group::--- mask::rw- other::---
经过查看咱们发现,/mnt/dir/sunsky文件中,对于 sunsky的acl权限设置居然仍是 # effective:rw- ,这是为何呢?
原来,咱们刚才修改 /mnt/dir 的 mask 仅仅只针对/mnt/dir目录下新生成的文件有效,而且因为文件在建立时,受到传统权限的 umask 值的影响,已经拥有了属組的权限,因此就使得 ACL 的mask设置失效。所以,此时咱们经过使用上面提到的-R 递归选项,将/mnt/dir目录下的全部文件从新修改一次 ACL 的mask权限,就能解决该问题!
[root@lh mnt]# su - sunsky Wellcome to Linux World [sunsky@lh ~]$ echo date > /mnt/dir/sunsky [sunsky@lh ~]$ bash /mnt/dir/sunsky Fri Jul 4 18:32:11 CST 2014
以上就是 setfacl 的平常管理的全部操做了!相信只要你们将以上的操做掌握,之后只要用到ACL的地方就不会窘迫了。