自动授予文件夹内新创建文件的权限

Ibr*_*him 7 permissions command-line directory acl

我有一个权限设置为 777 的文件夹,但是当我在该文件夹中添加任何文件或文件夹解压缩时。提取的文件或文件夹权限永远不会改变。目标文件夹权限是 777,我希望我将在该文件夹中添加的内容将自动获得 777 的权限。

当我通过在该文件夹中提取 .zip 添加任何文件或文件夹时,提取的文件夹/文件权限不会自动更改。我总是必须 chmod 才能新添加文件/文件夹!

c0r*_*0rp 14

您想要的是ACL -访问控制列表

访问控制列表 (ACL) 为文件系统提供了额外的、更灵活的权限机制。它旨在协助 UNIX 文件权限。ACL 允许您为任何用户或组授予对任何光盘资源的权限。

acl包应该已经安装,检查一下运行:dpkg -s acl

要使用 ACL,您应该为您的文件系统启用它。但它可以已经启用。要检查它,请使用tune2fs -l. 替代/dev/sda6您的系统:

$ tune2fs -l /dev/sda6 | grep "Default mount options:"
Default mount options:    user_xattr acl
Run Code Online (Sandbox Code Playgroud)

如果您看到acl字 - 它已为设备启用/dev/sda6

如果您没有看到acl字 - 运行tune2fs -o acl /dev/sda6以启用它。


要修改 ACL,请使用setfacl命令。要添加权限,请使用setfacl -m

为用户设置权限:

$ setfacl -m "u:username:rwx" /path/to/folder
Run Code Online (Sandbox Code Playgroud)

这将为rwx用户设置ACLusername到 /path/to/folder。这意味着,此文件夹中创建的所有文件都会有rwx许可username


设置组权限:

$ setfacl -m "g:groupname:rwx" /path/to/folder
Run Code Online (Sandbox Code Playgroud)

这会将rwxACL设置为组groupname到 /path/to/folder。这意味着在此文件夹中创建的所有文件都将具有rwxgroup 权限groupname


为其他人设置权限:

$ setfacl -m "o:rwx" /path/to/folder
Run Code Online (Sandbox Code Playgroud)

这将设置rwxACL,其他到 /path/to/folder。这意味着在此文件夹中创建的所有文件都将具有rwx其他权限。


检查权限:

$ getfacl /path/to/folder
Run Code Online (Sandbox Code Playgroud)

结合acl

$ setfacl -m u:username:rwx,g:groupname:rwx,o:rwx /path/to/folder
Run Code Online (Sandbox Code Playgroud)

默认访问控制列表

 The new object inherits the default ACL of the containing directory as its
 access ACL.

 If no default ACL is associated with a directory, the mode parameter to the func?
 tions creating file objects and the file creation mask (see umask(2)) are used to
 determine the ACL of the new object:

 The new object is assigned an access ACL containing entries of tag types
 ACL_USER_OBJ, ACL_GROUP_OBJ, and ACL_OTHER. The permissions of these entries
 are set to the permissions specified by the file creation mask.
Run Code Online (Sandbox Code Playgroud)

因此,如果您设置默认 ACL,它将是首选ACL。这意味着如果为useror设置 ACL group,新创建的文件无论如何都会继承默认的acl 。小心使用默认 ACL。

要设置默认 acl 使用-d密钥,

$ setfacl -d -m u::rwx,g::rwx,o::rwx /path/to/folder
Run Code Online (Sandbox Code Playgroud)

default用词

$ setfacl -m default:u::rwx,default:g::rwx,default:o::rwx /path/to/folder
Run Code Online (Sandbox Code Playgroud)

小心设置默认 ACL。例如,如果设置如下:

$ setfacl -d -m o:--x /path/to/folder
Run Code Online (Sandbox Code Playgroud)

现在获取这个 ACL

$ getfacl /path/to/folder
# file: path/to/folder
# owner: c0rp
# group: c0rp
user::rwx
group::rwx
other::--x
default:user::rwx
default:group::rwx
default:other::--x
Run Code Online (Sandbox Code Playgroud)

组和用户的默认 ACL 将是rwx自动的!

删除ACL

$ setfacl -b /path/to/folder
Run Code Online (Sandbox Code Playgroud)

这将从文件夹中删除所有 ACL


最后

如果您只是系统中的用户,我建议使用默认 ACL。

$ setfacl -d -m u::rwx,g::rwx,o::rwx /path/to/folder
Run Code Online (Sandbox Code Playgroud)

这将对 /path/to/folder 执行您想要的操作

来源

archlinux - https://wiki.archlinux.org/index.php/Access_Control_Lists

help.ubuntu - https://help.ubuntu.com/community/FilePermissionsACLs