Linux文件权限控制-setfacl

描述

setfacl 是Linux操作系统中设置文件访问控制列表工具。与通过改变文件或目录的属主(u)属组(g)的传统控制权限方式不同,setfacl允许在不改变属主属组的情况下,对指定用户或者用户组进行授权。

一般参数

[root@localhost ~]# setfacl  --help
setfacl 2.2.53 -- set file access control lists
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ... } file ...
  -m, --modify=acl        modify the current ACL(s) of file(s)
  -M, --modify-file=file  read ACL entries to modify from file
  -x, --remove=acl        remove entries from the ACL(s) of file(s)
  -X, --remove-file=file  read ACL entries to remove from file
  -b, --remove-all        remove all extended ACL entries
  -k, --remove-default    remove the default ACL
      --set=acl           set the ACL of file(s), replacing the current ACL
      --set-file=file     read ACL entries to set from file
      --mask              do recalculate the effective rights mask
  -n, --no-mask           don't recalculate the effective rights mask
  -d, --default           operations apply to the default ACL
  -R, --recursive         recurse into subdirectories
  -L, --logical           logical walk, follow symbolic links
  -P, --physical          physical walk, do not follow symbolic links
      --restore=file      restore ACLs (inverse of `getfacl -R')
      --test              test mode (ACLs are not modified)
  -v, --version           print version and exit
  -h, --help              this help text

参数释义

修改类参数

-m, --modify=acl
修改当前文件或目录的 ACL 条目。
示例:

setfacl -m u:username:rwx file.txt(为用户 username 添加读、写、执行权限)
setfacl -m u:1000:rwx file.txt (为uid等于 1000 的用户添加读、写、执行权限)
setfacl -m g:groupname:rwx file.txt (为用户组 groupname 添加读、写、执行权限)
setfacl -m g:1000:rwx file.txt (为gid等于 1000 的用户组 添加读、写、执行权限)

-M, --modify-file=file
从文件中读取 ACL 条目并修改当前文件或目录的 ACL。
示例:

setfacl -M acls.txt file.txt(从 acls.txt 文件中读取 ACL 条目并应用到 file.txt)。

-x, --remove=acl
从文件或目录的 ACL 中移除指定的条目。
示例:

setfacl -x u:username file.txt(移除用户 username 的 ACL 条目)。

-X, --remove-file=file
从文件中读取要移除的 ACL 条目,并从目标文件或目录中移除这些条目。
示例:

setfacl -X acls.txt file.txt(从 acls.txt 文件中读取要移除的 ACL 条目)。

删除类参数

-b, --remove-all
删除所有扩展的 ACL 条目,恢复为默认的权限(即只有基本的 rwx 权限)。
示例:

setfacl -b file.txt(删除 file.txt 的所有扩展 ACL 条目)。

-k, --remove-default
删除目录的默认 ACL(默认 ACL 用于新创建的文件或子目录)。
示例:

setfacl -k /path/to/dir(删除目录的默认 ACL)。

设置类参数

--set=acl
完全替换文件或目录的当前 ACL,设置新的 ACL 条目。
示例:

setfacl --set=u:username:rwx file.txt(完全替换 file.txt 的 ACL,为用户 username 设置读、写、执行权限)。

--set-file=file
从文件中读取 ACL 条目并完全替换目标文件或目录的 ACL。
示例:

setfacl --set-file=acls.txt file.txt(从 acls.txt 文件中读取 ACL 条目并替换 file.txt 的 ACL)。

权限掩码相关

--mask
重新计算 ACL 的有效权限掩码(mask)。掩码决定了用户或组的实际有效权限。
示例:

setfacl --mask file.txt(重新计算 file.txt 的权限掩码)。

-n, --no-mask
不重新计算权限掩码。
示例:

setfacl -n -m u:username:rwx file.txt(添加用户权限但不计算掩码)。

默认 ACL 相关

-d, --default
操作应用于目录的默认 ACL。默认 ACL 会影响新创建的文件或子目录的权限。
示例:

setfacl -d -m u:username:rwx /path/to/dir(为目录设置默认 ACL,新文件或子目录会继承这些权限)。

递归类参数

-R, --recursive
递归地对目录及其子目录和文件应用 ACL 操作。
示例:

setfacl -R -m u:username:rwx /path/to/dir(递归地为目录及其内容设置 ACL)。

-L, --logical
逻辑遍历(默认行为),跟随符号链接。
示例:

setfacl -L -m u:username:rwx /path/to/dir(处理符号链接指向的目标文件)。

-P, --physical
物理遍历,不跟随符号链接。
示例:

setfacl -P -m u:username:rwx /path/to/dir(仅处理符号链接本身,而不是其指向的目标)。

其他参数

--restore=file
从备份文件中恢复 ACL(与 getfacl -R 命令的输出兼容)。
示例:

setfacl --restore=backup.acls(从 backup.acls 文件中恢复 ACL)。

--test
测试模式,仅模拟操作,不实际修改 ACL。
示例:

setfacl --test -m u:username:rwx file.txt(测试是否可以成功设置 ACL)。

应用场景

NFS SERVER,当不想改变共享目录的属主属组时,可使用setfacl对客户端用户的uid去单独授权

posted @ 2025-04-11 11:13  Linux小飞象  阅读(153)  评论(0)    收藏  举报