linux中setfacl命令,Linux命令之:setfacl和getfacl

linux中setfacl命令,Linux命令之:setfacl和getfacl命令总结之:setfacl和getfaclacl:accesscontrollist,实现灵活的权限管理除了文件的所有者,所属组合其他人,可以对更多的用户设置权限acl生效顺序:所有者、自定义用户、自定义组、其他人1、首先我们查看man帮助文档说明[root@centos7sixijie]#mansetfacl根据man文档节选出来几个我们会经常用到的功能和选项加以说明:setfacl…

大家好,又见面了,我是你们的朋友全栈君。

命令总结之: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

3c8ce6380daa33c267ccb0e2e66a02de.png 

3c8ce6380daa33c267ccb0e2e66a02de.png

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

3c8ce6380daa33c267ccb0e2e66a02de.png

acl.del

u:user1

u:user2

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

[root@centos7 sixijie]# getfacl f1

3c8ce6380daa33c267ccb0e2e66a02de.png

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

3c8ce6380daa33c267ccb0e2e66a02de.png

–set选项

注:一定要包含UGO的设置

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

[root@centos7 sixijie]# getfacl f1

3c8ce6380daa33c267ccb0e2e66a02de.png

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

[root@centos7 sixijie]# setfacl -b f1

[root@centos7 sixijie]# getfacl f1

3c8ce6380daa33c267ccb0e2e66a02de.png

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

(1)设置默认ACL 一般只针对目录

[root@centos7 sixijie]# setfacl -m d:u:sixijie:rw,d:g:hadoop:r dir1

[root@centos7 sixijie]# getfacl dir1

3c8ce6380daa33c267ccb0e2e66a02de.png

(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

3c8ce6380daa33c267ccb0e2e66a02de.png

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

删除的方法很简单:一个-k选项即可

[root@centos7 sixijie]# setfacl -k dir1/

[root@centos7 sixijie]# getfacl dir1/

3c8ce6380daa33c267ccb0e2e66a02de.png

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

3c8ce6380daa33c267ccb0e2e66a02de.png

可见自定义用户,自定义组及其所属组的权限不能大于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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/150702.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月22日 下午6:00
下一篇 2022年6月22日 下午6:00


相关推荐

  • Clawdbot 8万Star的背后:AI 助理真的来了么(附macOS本地尝鲜教程-有手就行版)

    Clawdbot 8万Star的背后:AI 助理真的来了么(附macOS本地尝鲜教程-有手就行版)

    2026年3月12日
    1
  • Matlab之贝塞尔函数

    Matlab之贝塞尔函数贝塞尔函数在 matlab 中已经有了函数 可以直接用 那么我们就没有必要重复发明轮子 以下内容摘自某文档 第一类贝塞尔函数在 MatLab 中用 besselj NU Z 来表示 用 MatLab 的仿真代码是 clear nbsp clc format nbsp longx 0 0 01 20 y 0 besselj 0 x y 1 besselj 1 x y 2

    2026年3月19日
    2
  • 数字游戏13524_数位计数器

    数字游戏13524_数位计数器由于科协里最近真的很流行数字游戏。某人又命名了一种取模数,这种数字必须满足各位数字之和 mod N 为 0。现在大家又要玩游戏了,指定一个整数闭区间 [a.b],问这个区间内有多少个取模数。输入格式输入包含多组测试数据,每组数据占一行。每组数据包含三个整数 a,b,N。输出格式对于每个测试数据输出一行结果,表示区间内各位数字和 mod N 为 0 的数的个数。数据范围1≤a,b≤231−1,1≤N<100输入样例:1 19 9输出样例:2#include<bits

    2022年8月9日
    7
  • OpenClaw 预置技能完全指南

    OpenClaw 预置技能完全指南

    2026年3月13日
    4
  • 自动阅读 到底 能不能赚钱

    自动阅读 到底 能不能赚钱

    2021年11月11日
    51
  • c语言函数指针的用法_函数指针作为形参

    c语言函数指针的用法_函数指针作为形参前言函数指针和指针函数,在学习 C 语言的时候遇到这两个东西简直头疼,当然还有更头疼的,比如什么函数指针函数、指针函数指针、数组指针、指针数组、函数指针数组等等,描述越长其定义就越复杂,当然理解起来就越难,特别是刚开始学习这门语言的童鞋,估计碰到这些东西就已经要崩溃了,然后好不容易死记硬背下来应付考试或者面试,然后过了几天发现,又是根本不会用,也不知道该在哪些地方用,这就尴尬了。今天这里只讲两…

    2022年8月18日
    7

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号