如何强制特定目录中创建的文件的组和权限?

Mr.*_*ndo 34 linux file-permissions

对于从/var/www 目录中的testuser帐户创建的文件,我需要它们具有g+rwx作为权限,以及www-data作为组。

我怎样才能做到这一点?

我正在通过 SSH 创建文件。

use*_*686 63

要设置组,给/var/wwwsetgid的位:

chgrp www-data /var/www
chmod g+s /var/www
Run Code Online (Sandbox Code Playgroud)

还要调整子目录: find /var/www -type d -exec chmod g+s {} +

这将使所有新创建的文件继承父目录的组,而不是用户的组。


要设置默认组权限,您必须使用ACL。设置“默认”ACL:

setfacl -m "default:group::rwx" /var/www
Run Code Online (Sandbox Code Playgroud)

还要调整子目录: find /var/www -type d -exec setfacl -m d:g::rwx {} +

注意:文件系统必须启用 ACL 支持。有时它是默认开启的;在ext3ext4 上,您可能会收到“不支持操作”,在这种情况下,必须手动启用它:

  • 对于当前挂载的文件系统: mount -o remount,acl /

  • 永久 -以下方法之一

    • 在fstab文件级别:编辑/etc/fstabacl在选项字段

    • 在文件系统级别: tune2fs -o acl /dev/diskname

  • @bobpaul:不,因为`chmod` 也会获取所有文件。 (5认同)

Luk*_*ePH 5

这可能让一些人对 setgid 的“grawity”答案感到困惑,如果文件夹的组与您自己的组不同,您可能需要以 root 身份运行 chmod,但您不会收到任何指示您需要执行此操作的错误。

$ ls -ld dir
drwxrwxr-x 2 luke testgroup 4096 Mar  9 10:44 dir

$ chmod g+s dir                                    #no errors

$ ls -ld dir
drwxrwxr-x 2 luke testgroup 4096 Mar  9 10:44 dir  #but nothing changed

$ touch dir/nosudo && ls -l dir/
-rw-rw-r-- 1 luke luke 0 Mar  9 10:51 nosudo       #and the group is still wrong


$ sudo chmod g+s dir

$ ls -ld dir
drwxrwsr-x 2 luke testgroup 4096 Mar  9 10:44 dir  #the setgid bit is now on

$ touch dir/withsudo && ls -l dir/
-rw-rw-r-- 1 luke luke      0 Mar  9 10:51 nosudo
-rw-rw-r-- 1 luke testgroup 0 Mar  9 10:51 withsudo #and group is set
Run Code Online (Sandbox Code Playgroud)