• Linux命令之getfacl和setfacl命令


    一、命令简介

      getfacl命令来自于英文词组”get file access control list“的缩写,其功能是用于显示文件或目录的ACL策略。对指定的文件或目录进行精准的权限控制,FACL是不二之选。对于每个文件,getfacl显示文件名、所有者、组和访问控制列表(ACL)。如果目录具有默认ACL,则获取facl还显示默认ACL。非目录不能具有默认ACL。
      setfacl,​顾名思义,设置文件或者目录访问控制列表,即​ACL​规则。这一般是用于对文件或者目录执行扩展授权,比如文件属于用户user1,但是希望给user2也添加授权,让user2也可以对文件进行操作,我们就可以通过setfacl命令实现。

    二、getfacl使用示例

    1、查看getfacl版本

    [root@s142 ~]# getfacl -v
    getfacl 2.2.51

    2、获取getfacl命令帮助

    [root@s142 ~]# getfacl --help
    getfacl 2.2.51 – get file access control lists
    Usage: getfacl [-aceEsRLPtpndvh] file …

    3、查看文件所有权限

    [root@s142 home]# getfacl -a /home
    getfacl: Removing leading ‘/’ from absolute path names
    # file: home
    # owner: root
    # group: root
    user::rwx
    group::r-x
    other::r-x

    4、查看目录默认权限

      看到如下,说明目录没有设置默认权限。

    [root@s142 home]# getfacl -d /home/abc
    getfacl: Removing leading ‘/’ from absolute path names
    # file: home/abc
    # owner: root
    # group: root

    5、按照物理路径查询

      看到如下,abd是软链接,使用-P参数按照物理路径查询无输出,使用-L参数查询输出abd及下面文件的权限ACL规则。
    在这里插入图片描述

    6、按照逻辑路径查询

    在这里插入图片描述

    7、不显示注释头

    [root@s142 home]# getfacl -c /home
    getfacl: Removing leading ‘/’ from absolute path names
    user::rwx
    group::r-x
    other::r-x

    8、显示路径包含根/

    [root@s142 home]# getfacl -p /home
    # file: /home
    # owner: root
    # group: root
    user::rwx
    group::r-x
    other::r-x

    9、显示用户和用户组ID

    在这里插入图片描述

    10、表格格式输出权限ACL

    在这里插入图片描述

    三、setfacl使用示例

    1、查看命令版本

    [root@s142 home]# setfacl -v
    setfacl 2.2.51

    2、获取命令帮助

    [root@s142 home]# setfacl --help
    setfacl 2.2.51 – set file access control lists
    Usage: setfacl [-bkndRLP] { -m|-M|-x|-X … } file …

    3、修改文件ACL

      使用setfacl -m u:username:perms files设置权限,u:username:perms是针对用户授权,也可以使用g:groupname:perms对用户组授权,还可以使用m::perms的方式对所有用户和组进行授权,使用o::perms对其它用户组进行授权。
    在这里插入图片描述

    [root@s142 home]# setfacl -m u:wuhs:rwx /home/abd/abc
    [root@s142 home]# getfacl -a /home/abd/abc
    getfacl: Removing leading ‘/’ from absolute path names
    # file: home/abd/abc
    # owner: root
    # group: root
    user::rw-
    user:wuhs:rwx
    group::r–
    mask::rwx
    other::r–

    4、从ACL移除条目

      使用setfacl -x参数移除指定的ACL条目,一次只可以移除一条。

    [root@s142 home]# setfacl -x u:wuhs /home/abd/abc
    [root@s142 home]# getfacl /home/abd/abc
    getfacl: Removing leading ‘/’ from absolute path names
    # file: home/abd/abc
    # owner: root
    # group: root
    user::rw-
    group::r–
    mask::r–
    other::r–

    5、拷贝文件权限到另外一个文件

      我们可以setfacl结合getfacl命令使用,将文件A的权限拷贝到文件B的ACL规则中去,使用set-file=-的方式完成ACL权限拷贝。

    [root@s142 home]# setfacl -m u:wuhs:6 /home/abc/abc
    [root@s142 home]# getfacl /home/abc/abc | setfacl --set-file=- /home/abc/abd
    getfacl: Removing leading ‘/’ from absolute path names
    [root@s142 home]# getfacl /home/abc/abd
    getfacl: Removing leading ‘/’ from absolute path names
    # file: home/abc/abd
    # owner: root
    # group: root
    user::rw-
    user:wuhs:rw-
    group::r–
    mask::rw-
    other::r–

    6、设置默认ACL规则

      使用-d和-m参数一起使用完成默认ACL的设置,还可以结合-R参数,这样目录下的所有子目录权限也与设置目录权限一致。
    在这里插入图片描述

    [root@s142 home]# setfacl -d -R -m u::7 /home/abc
    [root@s142 home]# setfacl -d -R -m g::5 /home/abc
    [root@s142 home]# setfacl -d -R -m o::4 /home/abc
    [root@s142 home]# getfacl /home/abc
    getfacl: Removing leading ‘/’ from absolute path names
    # file: home/abc
    # owner: root
    # group: root
    user::rwx
    group::r-x
    other::r-x
    default:user::rwx
    default:group::r-x
    default:other::r–

    7、清除默认ACL规则

    [root@s142 home]# setfacl -k /home/abc
    [root@s142 home]# getfacl /home/abc
    getfacl: Removing leading ‘/’ from absolute path names
    # file: home/abc
    # owner: root
    # group: root
    user::rwx
    group::r-x
    other::r-x

    8、清除所有扩展ACL规则

      使用-b参数是清除所有的扩展ACL规则,创建文件属主关联的基本规则是不会清除的。
    在这里插入图片描述

    [root@s142 home]# setfacl -b /home/abc

    9、通过文件完成ACL授权

      可以使用-M参数指定授权ACL文件,一次完成多条目授权规则授权,我们也可以同时对多个文件或者目录授权。
    在这里插入图片描述

    四、参数说明

    1、使用语法

    用法:
    $ getfacl [参数] file …
    $ setfacl --restore=file 或者 setfacl [参数] file …

    2、getfacl参数说明

    参数参数说明
    -a, --access仅显示文件的ACL
    -d, --default仅显示文件的ACL
    -c, --omit-header不显示注释标题
    -e, --all-effective显示所有的有效权限
    -E, --no-effective显示没有的有效权限
    -s, --skip-base跳过文件,只具有基本条目(实际测试使用此参数后,输出为空)
    -R, --recursive递归查询
    -L, --logical按照逻辑路径,包括软链接
    -P, --physical按照物理路径,不包括软链接
    -t, --tabular使用表格输出格式
    -n, --numeric显示用户的UID和组群的GID
    -p, --absolute-names保留路径中开始的根"/"
    -v, --version显示版本信息
    -h, --help获取命令帮助信息

    3、setfacl参数说明

    参数参数说明
    -m, --modify=acl更改文件的访问控制列表
    -M, --modify-file=file从文件读取访问控制列表条目更改
    -x, --remove=acl从文件访问控制列表移除条目
    -X, --remove-file=file从文件读取访问控制列表条目并删除
    -b, --remove-all删除所有扩展访问控制列表条目
    -k, --remove-default移除默认访问控制列表
    –set=acl设定替换当前的文件访问控制列表
    –set-file=file从文件中读取访问控制列表条目设定
    –mask重新计算有效权限掩码
    -n, --no-mask不重新计算有效权限掩码
    -d, --default应用到默认访问控制列表的操作
    -R, --recursive递归操作子目录
    -L, --logical按照逻辑路径,包括软链接
    -P, --physical按照物理路径,不包括软链接
    –restore=file恢复访问控制列表
    –test测试模式,并不真正修改访问控制列表属性
    -v, --version查看版本信息
    -h, --help显示帮助信息
  • 相关阅读:
    (17)Verilog实现二进制格雷码互转
    实施 DevSecOps 最佳实践
    小谈设计模式(28)—解释器模式
    给你一个项目,你将如何开展性能测试工作?
    车企数智化丨帷幄SDP数字化护航经销商业务健康度
    mysql索引覆盖之innodb和myisam效率问题
    MySQL实现的一点总结(四)
    这个选择,是一笔划算的投资
    论文笔记:LBCF: A Large-Scale Budget-Constrained Causal Forest Algorithm
    Linux:实用操作
  • 原文地址:https://blog.csdn.net/carefree2005/article/details/126287340