访问控制列表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. 设计模式        设计模式(Design pattern)是一套被反复使用、多数人知晓的、经过分类编目的、代码设计经验的总结。使用设计模式是为了可重用代码、让代码更容易被他人理解、保证代码可靠性。 毫无疑问,设计模式于己于他人于系统都是多赢的,设计模式使代码编制真正工程化,设计模式是软件工程的基石,如同大厦…

    Linux干货 2015-06-18
  • sed命令初识

           sed是一种流编辑器,它是文本处理中非常常用的工具,能够配合正则表达式使用。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有 改变,除非你使用重定向存储输出。Sed主要用来自…

    Linux干货 2017-12-03
  • 软件包管理–RPM YUM

    包管理器 二进制应用程序的组成部分 二进制文件,配置文件,说明文档,库文件 程序包管理器 Debian  deb文件  dpkg包管理器 Redhat  rpm文件  rpm包管理器                   &nbsp…

    Linux干货 2016-08-21
  • Linux Bash脚本编程练习

    马哥教育网络班23期+第九周课堂练习 Linux Bash脚本编程练习 练习 1.写一个脚本,判断当前系统上所有用户的shell是否为可登录shell (即用户的shell不是/sbin/nologin) 分别统计这2类用户的个数,通过字符串比较来实现 脚本: 测试结果:  。。。 2.写一个脚本 (1)获取当前主机的主机名 保存于hostname…

    Linux干货 2016-11-28
  • 压缩、解压缩和归档工具

    1  compress和uncompress 用法:     compress [-dfvcVr] [-b maxbits] [file…] 选项:     -d   解压缩     -c   结果…

    Linux干货 2016-08-18
  • yum 仓库

    Yum 仓库     yum源就是一个软件集合地,你只需要搜索并安装你想要的软件,它会帮你解决大部分软件的依赖问题。本地源比如说光盘里面一般会附带一些软件,这个时候就可以把光盘当成本地源来安装软件。网络源比如说aliyun的镜像网站,这就属于网络源,可以通过互联网把软件下载下来并安装。   yum 仓库 &n…

    2017-06-24