如何在目录上递归设置权限(启用 ACL)?

Rog*_*ach 30 linux bash acl permissions

例如,我想给我的同事写访问某个目录的权限。假设其中的子目录具有访问权限 775,文件 664,并且目录中还有一些可执行文件 - 775。

现在我想添加写权限。使用 chmod,我可以尝试类似

chmod o+w -R mydir/
Run Code Online (Sandbox Code Playgroud)

但这并不酷,因为我不想让 dir 成为世界可写的 - 我只想授予某些用户访问权限,所以我想使用 ACL。但是有没有一种简单的方法来设置这些权限?在我看来,我需要分别处理至少三种情况(目录、文件、可执行文件):

find -type d -exec setfacl -m u:colleague:rwx {} \;
find -type f -executable -exec setfacl -m u:colleague:rwx {} \;
find -type f \! -executable -exec setfacl -m u:colleague:rw {} \;
Run Code Online (Sandbox Code Playgroud)

对于这样一个简单的任务,似乎有相当多的代码行。有没有更好的办法?

uml*_*ute 47

setfacl有一个递归选项 ( -R),就像chmod

  -R, --recursive
      Apply operations to all files and directories recursively. This
      option cannot be mixed with `--restore'.
Run Code Online (Sandbox Code Playgroud)

它还允许使用大写-xX权限,这意味着:

  execute only if the file is a directory or already has
  execute permission for some user (X)
Run Code Online (Sandbox Code Playgroud)

所以做以下应该工作:

setfacl -R -m u:colleague:rwX .
Run Code Online (Sandbox Code Playgroud)

(所有报价都来自man setfaclACL-52年2月2日为Debian自带)

  • 那么 OP 将他们的问题标记为 [linux] 很好 (7认同)
  • setfacl 在 FreeBSD 9 中没有“-R”标志 (6认同)

Fra*_*iat 6

正如 umläute 所提到的,setfacl -R带有大写“X”的命令是要走的路,比如:

setfacl -R -m u:colleague:rwX .
Run Code Online (Sandbox Code Playgroud)

但是,对于那些需要以递归方式重新应用 ACL 的用户(例如 Windows 中的“对子目录重新应用权限”)。

find . -mindepth 1 | xargs -n 50 setfacl -b --set-file=<(getfacl . | sed -e 's/x$/X/')
Run Code Online (Sandbox Code Playgroud)

可以拆分该命令以避免错误,例如setfacl: foobar: Only directories can have default ACLs.

find . -mindepth 1 -type d| xargs -n 50 setfacl -b --set-file=<(getfacl . | sed -e 's/x$/X/')
find . -mindepth 1 -type f| xargs -n 50 setfacl -b --set-file=<(getfacl . | grep -v '^default:' | sed -e 's/x$/X/')
Run Code Online (Sandbox Code Playgroud)

请注意,语法<( something )Process Substitution,它特定于 bash。如果您使用另一个 shell,您可能需要创建一个临时文件。