码迷,mamicode.com
首页 > 其他好文 > 详细

权限管理之ACL

时间:2016-08-07 17:09:22      阅读:1063      评论:0      收藏:0      [点我收藏+]

标签:acl setfacl getfacl

权限管理之ACL


  • setfacl

  • getfacl


试验环境:CentOS 7.2

ACL(Access Control List) 访问控制列表,在网络中应用十分广泛,主要用来控制与过滤数据流的走向,提高网络性能,在Linux中,我们可以用setfacl来设置基于用户权限的ACL。

setfacl

主要用途

设置ACL

setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file ...
setfacl --restore=file

setfacl命令主要用来设置基于用户权限的ACL,其命令执行的优先级高于sst,而sst又高于rwx.如果将ACL, sst, rwx 比作一个三层的金字塔,则rwx 处于金字塔的最底层,是最基本的权限,中间层为sst,最高层为ACL.

常用参数

  1. -b: --remove-all, 删除所有扩展的ACL(extended ACL)规则,基本的ACL(base ACL)规则(属主、属组、其它)则被保留(retained)。

  2. -k: --remove-default, 删除默认的ACL(Default ACL),如果没有默认规则,将不提示任何信息。

  3. -n: --no-mask, 不要重新计算有效期限,setfacl会重装计算acl mask,除非mask被明确指定

  4. --mask: 重新计算有效期限,即mask被明确指定

  5. -d: --default, 设置默认的acl

  6. --restore=file: 从文件恢复备份的acl规则,此参数只能独立执行,除了--test,备份的acl文件可由 getfacl -R产生。

  7. --test: 测试模式,不会改变任何文件的acl规则,操作后的acl规则将被列出。

  8. -R: --recursive,递归对所有文件及目录进行操作,不能与--restore 混合使用

  9. -L: --logical,只跟踪符号链接文件,但忽略符号链接目录,不能与--restore 混合使用

  10. -P: --physical, 忽略所有符号链接文件,不能与--restore 混合使用

  11. -m: --modify, 后面跟具体的acl条目

  12. -M: --modify-fiel, 从文件中读取要设置的acl规则

  13. -x: --remove, 清除具体的acl条目

  14. -X: --remove-file, 从文件中读取要清除的acl规则

setfacl 接受 getfacl 输出的格式,每行至少一条规则,以# 开始的行则当作注释;在不支持ACL的文件系统上使用setfacl时,setfacl将修改文件权限位,如果acl规则并不完全匹配文件权限位,setfacl将尽自己最大的努力修改文件的权限位,来达到反映acl规则的目的,同时向standard error发出错误消息,返回值大于0.

设置ACL文件

在Linux中,只有文件的属主与root才有权限设置acl文件。

The setfacl utility recognizes the following ACL entry formats(setfacl识别以下格式的acl条目)

[d[efault]:] [u[ser]:]uid [:perms]  # 指定用户的权限,文件属主的权限(如果uid未指定)
[d[efault]:] g[roup]:gid [:perms] # 指定群组的权限,文件属组的权限(如果gid未指定)
[d[efault]:] m[ask][:] [:perms] # 指定mask, 有效权限掩码(Effective rights mask)
[d[efault]:] o[ther][:] [:perms] # 其他权限(Permissions of others)

对于 uid 与 gid, 可以是数字,可以是名字;perms 代表权限,可以是rwx, 也可以是八进制数,但不能是sst, 其执行只针对于目录与可执行文件。

EXAMPLES

Granting an additional user read access 
    setfacl -m u:lisa:r file
    Revoking  write  access  from  all  groups and all named users (using the
effective rights mask) 
    setfacl -m m::rx file
    Removing a named group entry from a file‘s ACL 
    setfacl -x g:staff file
    Copying the ACL of one file to another
    getfacl file1 | setfacl --set-file=- file2
    
Copying the access ACL into the Default ACL
    getfacl --access dir | setfacl -d -M- dir

使用示例

  1. 针对某用户设置acl

[root@localhost ~]# setfacl -m u:liansir:0 file1
[root@localhost ~]# ll file1
-rw-r--r--+ 1 root root 31 Aug  6 05:02 file1       # 出现 + 号
[root@localhost ~]# getfacl file1  # 查看acl 
# file: file1
# owner: root
# group: rootuser::rw-
user:liansir:---   # 权限成功设置为0
group::r--
mask::r--
other::r--
[root@localhost ~]#

liansir用户已经无法访问了!

技术分享

  1. 删除已经设置的acl

[root@localhost ~]# setfacl -x u:liansir file1  
[root@localhost ~]# ll file1
-rw-r--r--+ 1 root root 31 Aug  6 05:02 file1 # 查看时还有 + 号
[root@localhost ~]# getfacl file1  # 的确已删除
# file: file1
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--
[root@localhost ~]#

我们再使用 -b 选项

[root@localhost ~]# setfacl -b file1
[root@localhost ~]# ll file1
-rw-r--r--. 1 root root 31 Aug  6 05:02 file1   # 成功去掉 + 号
[root@localhost ~]#
  1. 创建批量ACL规则文件

[root@localhost ~]# cat acl.txt
u:liansir:0
u:wangcai:rw
g:it:rw
[root@localhost ~]#
  1. 读取已创建的ACL规则文件

[root@localhost ~]# setfacl -M acl.txt f1

技术分享

  1. 创建批量删除ACL规则的文件

[root@localhost ~]# cat acldel.txt 
u:liansir
u:wangcai
g:it
[root@localhost ~]#
  1. 读取批量删除ACL文件

[root@localhost ~]# setfacl -X acldel.txt f1

技术分享

在以上的试验中,难免产生这样的疑问:设置了acl在ls -l 文件时能够看见 + 号,删除(setfacl -x) acl 后还能看见 + 号,这 + 号还有什么意义呢?(在setfacl -b 后 + 号会消失)

在试验的过程中,我们发现的 + 号的意义:

技术分享

注:f1文件是设置过acl的,我们再来看从来没有设置过acl的f2,或被完全清除acl的f2

技术分享

从而,我们可以得出这样的结论:在有ACL或ACL未被完全清空的情况下,设置文件属组的权限就相当于设置ACL中mask的权限。 所谓mask,就是ACL的默认的权限掩码。

  1. 修改ACL中的mask

[root@localhost ~]# setfacl -m m::r f1

技术分享

可见,改变mask 属组的权限也跟着变了,不难得出如下结论:

在有ACL或ACL未被完全清空的情况下,改变mask就是改变属组的权限,改变属组的权限就是改变mask.

再看另一种情况:mask 高压线

设置f2的权限为666,再设置mask的权限为r,再次查看f2的权限时发现只有属组的权限由rw-变为r--了。

技术分享



同样也可以在批量修改多用户的权限时可以使用mask来限定:

技术分享

我们 su - wangcai 来验证下,注意要到/root/下访问或者 cp -a f2 /home/wangcai 再访问!

技术分享

能够正常访问!

技术分享

不可编辑!

  1. 设置默认的acl属性(Default acl)

上面所述都是Access acl, 即其作用对象为文件,接下来我们对Default acl作个简介,Default acl是指对一个目录进行默认的acl设置,目的是在此目录下新建的文件和目录都将继承此目录的acl.

在/testdir/下创建子目录dir,并创建文件:

技术分享

设置默认的ACL属性:

[root@localhost testdir]# setfacl -d -m u:liansir:rwX dir
[root@localhost testdir]# setfacl -d -m g:wang:rX dir

技术分享

在dir目录下创建文件与子目录:

技术分享

这权限是有变化,可继承的似乎有问题。。。

  1. 删除默认ACL

[root@centos7 /testdir#]setfacl -k dir

技术分享

getfacl

getfacl - get file access control lists
  1. 复制一个ACL:复制file1的ACL权限给file2

[root@centos7 /testdir#]getfacl f1 |setfacl --set-file=- f2

技术分享

  1. 查看相应目录下的所有ACL:

[root@centos7 /testdir#]getfacl -R *

技术分享

  1. 备份ACL:

[root@centos7 /testdir#]getfacl -R * > acl.bak
  1. 清除所有的ACL

[root@centos7 /testdir#]setfacl -R -b *

  1. 从备份文件中恢复ACL

[root@centos7 /testdir#]setfacl -R --set-file=/root/acl.bak *

权限管理之ACL基本就先这么多吧,不过Default acl 应用于目录上时该目录下文件与子目录的权限继承问题还有待研究下,作个记录先。

止战

2016.8.7


本文出自 “止战-连Sir” 博客,请务必保留此出处http://liansir.blog.51cto.com/9372908/1835369

权限管理之ACL

标签:acl setfacl getfacl

原文地址:http://liansir.blog.51cto.com/9372908/1835369

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!