权限ACL

权限ACL Access Control List

  • ACL可以更加灵活的设置权限,除了文件的所有者,所属组和其它人,可以对更多的用户设置权限
  • CentOS7 默认创建的xfs 和ext4 文件系统具有ACL
  • CentOS7 之前版本,默认手工创建的ext4 文件系统无ACL功
    能, 需手动增加(后挂载的是没有acl功能的,做系统挂载的是默认就有的)
  • tune2fs –o acl /dev/sdb1
  • mount –o acl /dev/sdb1 /mnt/test
  • tune2fs -l /dev/sdan(在centos6上可以查看分区详细信息)
  • ACL生效顺序:所有者-自定义用户-自定义组-其他人,当匹配后不会在往下匹配后面的权限也是没有用的

命令setfacl

  • 格式 setfacl [option] [u:username:rwx/g:group:rwx] [文件]
  • 选项
    • -m 是修改增加
    • -x 是删除
    • -R 递归
    • -X 批量删除用文本
    • -M 批量添加用文本
    • d d:u:username:rwx dir 在目录下创建的文件默认都会有acl
    • –set 覆盖原来的acl
    • -b 清除所有的acl

查看文件的acl权限内容 getfacl

  • 格式 getfacl file

备份和恢复acl

  • 主要的文件操作命令cp 和mv 都支持ACL ,只是cp 命令需要
    加上-p 参数。但是tar 等常见的备份工具是不会保留目录
    和文件的ACL 信息
  • getfacl -R /tmp/dir1 > acl.txt
  • setfacl -R -b /tmp/dir1
  • setfacl -R –set-file=acl.txt /tmp/dir1
  • setfacl –restore acl.txt
  • getfacl -R /tmp/dir1

操作

  • 添加tom用户对123.TXT文档有rw权限
  • [root@centos6 app]# ll
    total 4
    -rw-r--r--. 1 root g1 68 Nov 18 11:13 123.txt
    请注意权限后面有一个点
  • 现在我们开始把tom用户有读写操作
  • [root@centos6 app]# setfacl -m u:tom:rw 123.txt
    [root@centos6 app]# ll
    total 8
    -rw-rw-r--+ 1 root g1 68 Nov 18 11:13 123.txt
    现在对比之前的详细信息发现权限并没有发生任何改变,但是后面的点变成了加号
  • 这种有加号的就代表这文件里有ACL权限,用getacl命令查看文件
  • [root@centos6 app]# ll  
    total 8
    rw-rw-r--+ 1 root g1 68 Nov 18 11:13 123.txt
  • [root@centos6 app]# getfacl 123.txt 

    # file: 123.txt
    # owner: root
    # group: g1
    user::rw- 这是所有者的权限
    user:tom:rw- 这是我们刚刚添加tom权限
    group::r-- 这是所属组的权限 (这和原来的权限不一样)
    mask::rw- 权限掩码
    other::r-- 这是其他人权限
  • su tom切换到tom用户看看能写和读吗
  • [tom@centos6 app]$ cat 123.txt 
    123
    [tom@centos6 app]$ echo 'hahahah' >> 123.txt
  • 说明我们增加权限成功了
  • 让guo123用户加入g1组
  • [root@centos6 app]# id guo123
    uid=1209(guo123) gid=1209(guo123) groups=1209
    (guo123),1241(g1)
    [root@centos6 app]# su guo123
    [guo123@centos6 app]$ cat 123.txt
    123
    123
    123123123
    [guo123@centos6 app]$ echo '123456' >> 123.txt

    bash: 123.txt: Permission denied
  • 我们用ls -l 看看到是读写的,但是现在只有读没有写的权限,说明用ls -l 看到到所属组的权限不是准确的,但是在acl列表中group 是只有r权限的说明所属组是有group这个权限的控制的。
  • 用chmod 改所属组就是改变mask 掩码值

改所属组权限

  • chmod g+rwx这是改mask值,并不是改group所属组的权限
  • setfacl -m group::rw file
  • [root@centos6 app]# setfacl -m group::rw 123.txt
    [root@centos6 app]# getfacl 123.txt
    # file: 123.txt
    # owner: root
    # group: g1
    user::rw-
    user:tom:rw-
    group::rw-
    mask::rw-
    other:: ---

在添加某个组有读写权限

  • setfacl -m g:groupname:rw /file
  • [root@centos6 app]# setfacl -m g:xiaowang:rw /app/123.txt 
  • [root@centos6 app]# getfacl 123.txt 
    # file: 123.txt
    # owner: root
    # group: g1
    user::rw-
    user:tom:rw-
    group::rw-
    group:xiaowang:rw-
    mask::rw-
    other::---
  • 组的权限是相加的如果一个用户有多个组, 这几个组都在acl有不同的权限,这个用户的权限就是这几个组的权限的累加之和。

mark 值详解

  • 我们之前说用chmod 命令改所属组的时候并不是改真正的所属组而是改mark值。那现在改一下mask值不能读写
  • [root@centos6 app]# chmod g-r,g-w 123.txt 
    [root@centos6 app]# ll
    total 4
    -rw-------+ 1 root g1 0 Nov 18 12:19 123.txt
    [root@centos6 app]# getfacl 123.txt

    # file: 123.txt
    # owner: root
    # group: g1
    user::rw-
    user:tom:rw- #effective:--- 有效的权限
    group::rw- #effective:---
    group:xiaowang:rw- #effective:---
    mask::---
    other::---
  • 当mark值没有权限的时候我们发现所属组和加入acl中有权限的用户和组,的后面都显示#effective — 这样的备注 说明mark的权限设定了所属组和加入acl用户和组的最高权限,如果mask中没有的权限但是三个选项有就给去掉,他们三是不能超过mask的权限的。mark的权限对所有者和其它人不会管。

更改mask的权限

  • setfacl -m mask:rwx /file
  • chmod g+rwx /file

递归增加目录acl属性

  • setfacl -R -m u:username:rwx dir/

如何将文本中的ACL列表导入文件或者目录中

  • setfacl -M acl.txt /file
  • setfacl -X acl.txt file 批量删除
  • 注意acl.txt这个名字可以随便起,但是里面的内容一定是这种格式
  • u:username:rwx
  • g:groupname:rwx

如何设置在目录里创建的文件或者目录都继承父目录的acl

  • setfacl -m d:u:uname:rwx /file/

如何删除单个acl条目

  • setfacl -x u:name:rwx file

覆盖原有的acl

  • –set 选项会把原有的ACL 项都删除,用新的替代,需要注
    意的是一定要包含UGO 的设置,不能象-m 一样只是添加
    ACL 就可以
  • setfacl –set u::rw,u:wang:rw,g::r,o::- file1

复制文件的acl权限给另一个文件添加

  • getfacl file1 | setfacl –set-file=- file2
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章