访问控制列表ACL使用说明

一、什么是ACL

ACL(Access Control List)可灵活地,更细粒度地定义访问文件或目录的权限。

二、为什么使用ACL

Linux上文件系统的文件系统权限管理的对象分为三类:owner,group,other。这种分类非常简单,如果我希望有一个用户拥有不同于这三类对象的权限,或者再定义一个用户组的权限,传统的权限管理就不能实现,而ACL可以很好的解决这个问题。

三、ACL条目

 一系列ACL条目构成了ACL。条目应包含标签类型(tag type),可选择的标签修饰定语,以及权限设定。

其中标签类型分为:

 1.acl_user_obj:  文件的属主

 2.acl_user:     自定义用户

 3.acl_group_obj:  文件的所属组

 4.acl_group:    自定义用户组

 5.acl_mask:    规定了acl_user,acl_group_obj,acl_group可被授予的最大权限

 6.acl_other:    不在acl里定义的其他用户

[root@centos7 data]# getfacl f1
# file: f1
# owner: root
# group: mysql
user::rwx                #acl_user_obj
user:cutemsyu:rw-        #acl_user               
group::r-x               #acl_group_obj          #effective:r--
group:cutemsyu:rw-       #acl_group
group:fly:r--            #acl_group
mask::rw-                #acl_mask
other::---               #acl_other

 tag type 可简写,如u表示user,g表示group。

四、ACL设置和查看方法

 setfacl :设置    getfacl :查看

 格式:setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file …

     getfacl [-aceEsRLPtpndvh] file …

 1、直接设置模式:

 -m :修改权限

 -x :移除权限

 –set:设置权限,取代原来的acl权限设置,必需包含u,g,o的tag type

 例如,修改权限并查看

[root@centos7 testdir]# setfacl -m u:cutemsyu:r,g:cutemsyu:r example
[root@centos7 testdir]# getfacl example
# file: example
# owner: root
# group: root
user::rw-
user:cutemsyu:r--
group::r--
group:cutemsyu:r--
mask::r--
other::r--

 移除权限并查看

[root@centos7 testdir]# setfacl -x g:cutemsyu example
[root@centos7 testdir]# getfacl example 
# file: example
# owner: root
# group: root
user::rw-
user:cutemsyu:r--
group::r--
mask::r--
other::r--

 2、通过文件设置

 ACL支持读取文件来设置权限

 -M:修改权限

 -X:移除权限

 这里我给出一个文本acl_file,

[root@centos7 testdir]# cat acl_file 
u:cutemsyu:rw         #一行只能写一个条目
u:fly:rw
g:cutemsyu:r

 修改权限并查看

[root@centos7 testdir]# touch example2
[root@centos7 testdir]# setfacl -M acl_file example2
[root@centos7 testdir]# getfacl example2
# file: example2
# owner: root
# group: root
user::rw-
user:cutemsyu:rw-
user:fly:rw-
group::r--
group:cutemsyu:r--
mask::rw-
other::r--

 再创建一个文本用来移除权限

[root@centos7 testdir]# cat acl_file_remove 
u:cutemsyu
u:fly
g:cutemsyu

 移除权限并查看

[root@centos7 testdir]# setfacl -X acl_file_remove example2
[root@centos7 testdir]# getfacl example2
# file: example2
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--

 3、为目录设置ACL

 为目录设置ACL,设置方法相同,可用-R进行递归

 另外目录的ACL可加默认权限,不作用于目录本身,表示再此目录下新建的文件或目录继承默认权限

 例如:

[root@centos7 testdir]# setfacl -m o::-,u:fly:rwx,d:u:fly:rwx test/   #d:表示默认权限
[root@centos7 testdir]# getfacl test/
# file: test/
# owner: root
# group: root
user::rwx
user:fly:rwx
group::r-x
mask::rwx
other::---
default:user::rwx                   #未定义的默认tag type会跟随目录相应权限
default:user:fly:rwx
default:group::r-x
default:mask::rwx
default:other::---

 在此目录下新建目录和文件,查看ACL

[root@centos7 test]# mkdir dir1;touch file1;
[root@centos7 test]# getfacl dir1 file1 
# file: dir1
# owner: root
# group: root
user::rwx
user:fly:rwx
group::r-x
mask::rwx
other::---
default:user::rwx
default:user:fly:rwx
default:group::r-x
default:mask::rwx
default:other::---

# file: file1
# owner: root
# group: root
user::rw-
user:fly:rwx			#effective:rw-
group::r-x			#effective:r--
mask::rw-           #文件mask不继承x权限
other::---

 4、复制一个文件的ACL

 setfacl 支持管道输入 –set-file=-

getfacl file1|setfacl --set-file=- example      #复制file1权限到example

 5、备份和恢复ACL

 mv和cp备份文件时都会保留ACL信息,cp指令需加-p选项

 但是tar等工具工具备份文件时不会保留ACL信息,这时需要我们备份ACL信息

getfacl -R /testdir/data  >acl.bak         #递归备份ACL信息
setfacl -R --set-file=acl.bak /testdir/data        #递归恢复ACL信息

 6、清除ACL

setfacl -k dir       #清除默认acl设置
setfacl -b file      #清除所有acl设置

五、ACL和文件权限位的关系

 设置ACL的文件或目录,ls -l 查看权限位最后的 "."会变成"+"

drwxrwx---+ 2 root root 6 Aug  6 10:32 dir1

 owner位与ACL中acl_user_obj 对应

 group位与ACL中acl_mask对应

 other位与ACL中acl_other对应

六、ACL权限允许访问逻辑

 当一个线程访问一个被ACL保护的文件时,其获权逻辑如下:

 1.当线程的有效用户ID与文件owner相匹配时,如果owner拥有相应权限,则允许访问,否则拒绝。

 2.当线程的有效用户ID与acl_user相匹配时,如果mask和acl_user都具有相应权限则允许访问,否则拒绝。

 3.当线程的有效组ID或者补充组ID与文件所属组或任意acl_group相匹配时,

  (1)当ACL含有mask条目时

     如果mask和任何匹配的组拥有相应权限,则允许访问,否则拒绝

  (2)当ACL不含有mask条目时

     如果文件所属组拥有相应权限,则允许访问,否则拒绝

 4.如果acl_other拥有相应权限,则允许访问

 5.其他则拒绝

原创文章,作者:cutemsyu,如若转载,请注明出处:http://www.178linux.com/29774

(0)
cutemsyucutemsyu
上一篇 2016-08-08 16:16
下一篇 2016-08-08 16:16

相关推荐

  • 第八周练习脚本部分

    1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;在线的主机使用绿色显示;不在线的主使用红色显示; #!/bin/bash # for((i=1;i<=254;i++));do     site="172.16.250.${i}&quot…

    Linux干货 2016-12-15
  • 第五周作业

    查看链接:http://note.youdao.com/noteshare?id=1ed7b36aa41cbdc1154e3e5b54e43fce

    Linux干货 2016-09-19
  • NFS服务

    NFS服务:       NFS:Network File System 网络文件系统,基于内核的文件系统。 Sun公司开发,通过使用NFS,用户和程序可以像访 问本地文件一样访问远端系统上的文件,基于RPC(Remote Procedure Call Protocol远程过程调用)实现 。 RPC采用C/S模式。客户机请求程序…

    2017-08-14
  • 马哥教育网络班22期第四周课程练习1

    1.cp -r /etc/skel/ /home/tuser1 && chmod -R 600  /home/tuser1 2.max=`cat /etc/group |awk -F":" '{print $3}'|sort -nr| head -n1`|sed -i '$a had…

    Linux干货 2016-09-05
  • find 的使用及练习

    find是个使用频率比较高的命令。常常用它在系统特定目录下,查找具有某种特征【名字类型属主权限等】的文件。find命令的格式: find  [-path ..] -options [-print -exec -ok] path:要查找的目录路径。 ~ 表示$HOME目录 . 表示当前目录 / 表示根目录 -print :表示将结果输出到标准输出-e…

    Linux干货 2017-06-11
  • 第3周作业

    一、列出当前系统上的所有已经登录的用户的用户名 [root@bogon tmp]# who | cut -d ” ” -f1 | sort -urootwing[root@bogon tmp]# who | cut -d ” ” -f1 | uniqrootwing 二、取出最后登录到当前系统的用户相关信息 […

    Linux干货 2017-07-25