getfacl

时间:2023-03-09 13:25:11
getfacl

ACL即Access Control List 主要的目的是提供传统的owner,group,others的read,write,execute权限之外的具体权限设置,ACL可以针对单一用户、单一文件或目录来进行r,w,x的权限控制,对于需要特殊权限的使用状况有一定帮助。如,某一个文件,不让单一的某个用户访问。

ACL使用两个命令来对其进行控制

getfacl:取得某个文件/目录的ACL设置项目

setfacl:设置某个文件/目录的ACL设置项目

setfacl 参数

-m:设置后续acl参数

-x:删除后续acl参数

-b:删除全部的acl参数

-k:删除默认的acl参数

-R:递归设置acl,包括子目录

-d:设置默认acl

例:创建一文件test,将其权限修改为777,并查看其默认ACL权限配置

[root@Self-study ~]# touch /test

[root@Self-study ~]# chmod 777 /test

[root@Self-study ~]# getfacl /test             //获得文件的ACL权限

getfacl: Removing leading '/' from absolute path names

# file: test                                //文件名

# owner: root                             //文件所属者

# group: root                             //文件所属组

user::rwx                                //文件所属者权限

group::rwx                               //同组用户权限

other::rwx                               //其它者权限

可以看到其它者的权限也是可读可写可执行,可以自行测试,现在我们修改其ACL策略,使用用户code只有读取的权限

[root@Self-study ~]# setfacl -m u:code:r /test

[root@Self-study ~]# ll /test

-rwxrwxrwx+ 1 root root 1 Apr 11 07:25 /test            //可以看到权限的最后多了一个”+”号

现在再次查看一下此文件的ACL属性

[root@Self-study ~]# getfacl /test

getfacl: Removing leading '/' from absolute path names

# file: test

# owner: root

# group: root

user::rwx

user:code:r--                           //可以看到code单独的权限为r--

group::rwx

mask::rwx

other::rwx

注:code的权限并不是只根据ACL配置来决定的,它是由code用户基本权限与配置的ACL权限的“与”运算决定的,即other:rwx 与 code:r--  = code:r--

现在使用code用户,测试是否可写

在写文件时,会出现-- INSERT -- W10: Warning: Changing a readonly file提示。

除了对单个用户进行设置外,还可以对用户组、有效权限(mask)进行设置如对用户组设置:    g:[用户组]:[rwx]

注:有效权限(mask) 即用户或组所设置的权限必须要存在于mask的权限设置范围内才会生效

如上面的/test文件,已经有了可读权限,如果我们把它的有效权限修改为只有写权限,则设置的acl权限不在有效权限之内,则用户code就不可能再查看/test文件中的内容了

[root@Self-study ~]# setfacl -m m:w /test              //设置有效权限为只写

可以查看/test acl属性

[root@Self-study ~]# getfacl /test

getfacl: Removing leading '/' from absolute path names

# file: test

# owner: root

# group: root

user::rwx

user:code:r-- #effective:---

group::rwx #effective:-w-

mask::-w-                       //可以看到有效权限已经修改成功

other::rwx

使用code用户查看文件内容,首先使用root用户写入一些内容,会使测试更加直观

[root@Self-study ~]# echo "this is a test getfacl " > /test

[code@Self-study ~]$ vim /test

"/test" [Permission Denied]                     //可以在最下面看到不允许访问的提示,并且看不到任何内容
取消acl权限

[root@Self-study ~]# setfacl -x u:code /test             //取消/test对用户code的权限

[root@Self-study ~]# setfacl -x m /test                 //恢复有效权限

[root@Self-study ~]# getfacl /test

getfacl: Removing leading '/' from absolute path names

# file: test

# owner: root

# group: root

user::rwx

group::rwx

other::rwx

[root@Self-study ~]# ll /test

-rwxrwxrwx 1 root root 24 Apr 11 08:01 /test              //已经可以正常使用