Linux命令之:setfacl和getfacl

命令总结之:setfacl和getfacl

acl:access control list,实现灵活的权限管理

除了文件的所有者,所属组合其他人,可以对更多的用户设置权限

acl生效顺序:所有者、自定义用户、自定义组、其他人

1、首先我们查看man帮助文档说明

[root@centos7 sixijie]# man setfacl

根据man文档节选出来几个我们会经常用到的功能和选项加以说明:

    setfacl - set file access control lists

    The --set and --set-file options set the ACL of a file or a directory. The previous ACL is replaced.   ACL  entries
    for this operation must include permissions

    The -m (--modify) and -M (--modify-file) options modify the ACL of a file or directory.  ACL entries for this oper‐
    ation must include permissions.

    The -x (--remove) and -X (--remove-file) options remove ACL entries. It is not an error to remove  an  entry  which
    does  not  exist.   Only  ACL entries without the perms field are accepted as parameters, unless POSIXLY_CORRECT is
    defined.

    -b, --remove-all
       Remove all extended ACL entries. The base ACL entries of the owner, group and others are retained.

    -k, --remove-default
       Remove the Default ACL. If no Default ACL exists, no warnings are issued.

    -R, --recursive
       Apply operations to all files and directories recursively. This option cannot be mixed with `--restore'.

    -   If the file name parameter is a single dash, setfacl reads a list of files from standard input.

2、-m和-x选项分别为modify(设定)和remove(移除)acl权限

[root@centos7 sixijie]# setfacl -m u:sixijie:rwx f1
[root@centos7 sixijie]# getfacl f1
[root@centos7 sixijie]# setfacl -x u:sixijie f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl Linux命令之:setfacl和getfacl

3、-M和-X选项可以通过文件批量设定acl和移除acl

acl.txt:
u:user1:rw
u:user2:r
u:user3:rwx

[root@centos7 sixijie]# setfacl -M acl.txt f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl

acl.del
u:user1
u:user2

[root@centos7 sixijie]# setfacl -X acl.del f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl

4、–set和–set-file选项:

注意:--set和--set-file会把原有的ACL表项都删除    

--set-file

man手册中有这样一句话:

Copying the ACL of one file to another
          getfacl file1 | setfacl --set-file=- file2

因此
[root@centos7 sixijie]# getfacl f1 | setfacl --set-file=- f2
上述命令即复制f1的ACL给f2
[root@centos7 sixijie]# getfacl f2

Linux命令之:setfacl和getfacl

--set选项

注:一定要包含UGO的设置

[root@centos7 sixijie]# setfacl --set u::rw,u:sixijie:rwx,g::r,o::rw f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl

5、-b选项:清除所有ACL权限

[root@centos7 sixijie]# setfacl -b f1
[root@centos7 sixijie]# getfacl f1

Linux命令之:setfacl和getfacl

6、设置默认的ACL权限及其删除

(1)设置默认ACL 一般只针对目录
[root@centos7 sixijie]# setfacl -m d:u:sixijie:rw,d:g:hadoop:r dir1
[root@centos7 sixijie]# getfacl dir1

Linux命令之:setfacl和getfacl

(2)我们在dir1/目录下创建一个文件和目录
[root@centos7 sixijie]# cd dir1/
[root@centos7 dir1]# touch f3
[root@centos7 dir1]# mkdir d2
[root@centos7 dir1]# getfacl f3
[root@centos7 dir1]# getfacl d2

Linux命令之:setfacl和getfacl

我们可以看到dir1目录下的文件及其子目录继承了父目录dir1的ACL权限,这就叫做默认的ACL

删除的方法很简单:一个-k选项即可
[root@centos7 sixijie]# setfacl -k dir1/
[root@centos7 sixijie]# getfacl dir1/

Linux命令之:setfacl和getfacl

7、mask介绍

[root@centos7 tmp]# man acl
节选其中的一段话:acl_mask条目表示最大的访问权限

ACL_MASK        The ACL_MASK entry denotes the maximum access rights 
                that can be granted by entries of type ACL_USER, ACL_GROUP_OBJ, or ACL_GROUP.

所以我们可以在getfacl中看到这样的条目:effective:Mode

[root@centos7 ~]# setfacl -m u:sixijie:rx,g:user1:rwx f1
[root@centos7 ~]# getfacl f1

设置mask
[root@centos7 ~]# setfacl -m m:r f1
[root@centos7 ~]# getfacl f1

Linux命令之:setfacl和getfacl

可见自定义用户,自定义组及其所属组的权限不能大于mask设置的权限

一个小的知识点:setfacl和chmod设置的所属组的权限可以相互覆盖,当二者设置的权限不一致时,以使用getfacl看到的“#effective:”后的权限为准

8、备份和恢复ACL

主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上-p 参数。但是tar等常见的备份工具是不会保留目录和文件的ACL信息

方法如下:

[root@centos7 ~]# getfacl -R /tmp/dir1 > acl.txt
[root@centos7 ~]# setfacl -R -b /tmp/dir1
[root@centos7 ~]# setfacl -R --set-file=acl.txt /tmp/dir1
[root@centos7 ~]# getfacl -R /tmp/dir1

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

(0)
sixijiesixijie
上一篇 2016-08-04 14:39
下一篇 2016-08-04 14:40

相关推荐

  • 博客作业网络班22期+第5周(9.5-9.11)

    1、显示当前系统上root、fedora或user1用户的默认shell [root@MyCloudServer wjb]# egrep '^(root|fedora|user1)\>' /etc/passwd | cut -d: -f7/bin/bash 2、找出/etc/rc.d/init.d/functions文件中某单词后面…

    Linux干货 2016-09-15
  • 马哥教育网络班N22期+第7周课程练习

    1、创建一个10G分区,并格式为ext4文件系统; (1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl; mke2fs -t ext4 -b 2048 -L MYDATA -m 2 /dev/sdb1 tune2…

    Linux干货 2016-10-24
  • 05linux用户和组的权限总结

    1、文件的权限分类 文件的权限对象分三类:属主(u)、属组(g)、其他(o),每个对象都有rwx,读写执行三类权限。 对于文件 r:可查看文件内容 w:可修改其类容 x:可把此文件提请内核启动为一个进程 对于目录 r:可使用ls查看此目录中文件列表 w:可在此目录中创建和删除文件 x:可使用ls查看目录中文件列表,可以cd进入此目录 X:只给目录x权限,不给…

    Linux干货 2016-11-27
  • 通过FTP服务怒刷基础功法熟练度(匿名篇)

        Linux门派多种多样,那么本次就讲讲本人刷基本命令熟练度的方法。FTP原理什么的都不说了,网上有很多。直接上酸菜~学徒水平,大师勿笑。     本篇搭载的是FTP匿名用户访问,同时可以在服务器上进行创建删除等操作。危险系数有点点大,仅推荐用来刷命令熟练度使用。我用的Li…

    2017-07-25
  • N26-第一周博客作业

    1.描述计算机的组成及其功能 完整的计算机系统由硬件和软件两部分组成。 现在大部分的计算机为冯诺依曼体系,主要有五个组成部分:运算器、控制器、存储器、输入设备、输出设备,以下为详细描述。(计算机的CPU由运算器、控制器和一二三层缓存等构成) 运算器:对数据进行算术运算和逻辑运算(对数据进行加工处理) 控制器:分析指令,控制协调输入、输出操作对内存的访问。 存…

    Linux干货 2017-01-02
  • N28-第三周博客作业

    1、列出当前系统上所有已经登录的用户的用户名,注意:同一个用户登录多次,则只显示一次即可。
    2、取出最后登录到当前系统的用户的相关信息。
    3、取出当前系统上被用户当作其默认shell的最多的那个shell。
    4、将/etc/passwd中的第三个字段数值最大的后10个用户的信息全部改为大写后保存至/tmp/maxusers.txt文件中。
    5、取出当前主机的IP地址,提示:对ifconfig命令的结果进行切分。
    6、列出/etc目录下所有以.conf结尾的文件的文件名,并将其名字转换为大写后保存至/tmp/etc.conf文件中。
    7、显示/var目录下一级子目录或文件的总个数。
    8、取出/etc/group文件中第三个字段数值最小的10个组的名字。
    9、将/etc/fstab和/etc/issue文件的内容合并为同一个内容后保存至/tmp/etc.test文件中。
    10、请总结描述用户和组管理类命令的使用方法并完成以下练习:
    (1)、创建组distro,其GID为2016;
    (2)、创建用户mandriva, 其ID号为1005;基本组为distro;
    (3)、创建用户mageia,其ID号为1100,家目录为/home/linux;
    (4)、给用户mageia添加密码,密码为mageedu;
    (5)、删除mandriva,但保留其家目录;
    (6)、创建用户slackware,其ID号为2002,基本组为distro,附加组peguin;
    (7)、修改slackware的默认shell为/bin/tcsh;
    (8)、为用户slackware新增附加组admins;

    Linux干货 2017-12-19