在 Linux 上获取新文件以继承组权限

Joh*_*ate 117 permissions directory group

我在 Linux 服务器上遇到权限问题。我习惯了BSD。当一个目录由一个组拥有时,拥有它的用户不在 www-data 中,在其中创建的文件将归该组所有。这很重要,因为我希望网络服务器可以读取文件(我不会以 root 身份运行),但这样用户仍然可以将新文件放入目录中。我不能将用户放在 www-data 中,因为这样他们就可以阅读所有其他用户的网站。

我希望网络服务器读取所有网站,我希望用户能够更改自己的网站。

目前文件夹的权限是这样设置的....

drwxr-x--- 3 john www-data 4096 Feb 17 21:27 john
Run Code Online (Sandbox Code Playgroud)

以这种方式工作的权限是 BSD 上的标准行为。我如何让 Linux 做到这一点?

slm*_*slm 161

听起来您正在描述setgid 位功能,当设置了它的目录时,将强制在其中创建的任何新文件将它们的组设置为在父目录上设置的同一组。

例子

$ whoami
saml

$ groups
saml wheel wireshark
Run Code Online (Sandbox Code Playgroud)

使用权限 + 所有权设置目录

$ sudo mkdir --mode=u+rwx,g+rs,g-w,o-rwx somedir
$ sudo chown saml.apache somedir
$ ll -d somedir/
drwxr-s---. 2 saml apache 4096 Feb 17 20:10 somedir/
Run Code Online (Sandbox Code Playgroud)

在此目录中将文件作为 saml 触摸

$ whoami
saml

$ touch somedir/afile
$ ll somedir/afile 
-rw-rw-r--. 1 saml apache 0 Feb 17 20:11 somedir/afile
Run Code Online (Sandbox Code Playgroud)

这将为您提供您想要的大致内容。如果你真的想要你所描述的内容,我认为你需要求助于访问控制列表功能来获得它(ACL)。

ACL

如果您想对在目录下创建的文件的权限进行更多控制somedir,可以添加以下 ACL 规则来设置默认权限,如下所示。

$ ll -d somedir
drwxr-s---. 2 saml apache 4096 Feb 17 20:46 somedir
Run Code Online (Sandbox Code Playgroud)

设置权限

$ sudo setfacl -Rdm g:apache:rx somedir
$ ll -d somedir/
drwxr-s---+ 2 saml apache 4096 Feb 17 20:46 somedir/
Run Code Online (Sandbox Code Playgroud)

请注意+末尾的 ,这意味着该目录应用了 ACL。

$ getfacl somedir
# file: somedir
# owner: saml
# group: apache
# flags: -s-
user::rwx
group::r-x
other::---
default:user::rwx
default:group::r-x
default:group:apache:r-x
default:mask::r-x
default:other::---
Run Code Online (Sandbox Code Playgroud)

$ touch somedir/afile
$ ll somedir/afile 
-rw-r-----+ 1 saml apache 0 Feb 17 21:27 somedir/afile
$ 

$ getfacl somedir/afile
# file: somedir/afile
# owner: saml
# group: apache
user::rw-
group::r-x              #effective:r--
group:apache:r-x        #effective:r--
mask::r--
other::---
Run Code Online (Sandbox Code Playgroud)

注意默认权限 ( setfacl -Rdm) 设置为默认 ( ) 权限r-x( g:apache:rx)。这会强制任何新文件只r启用它们的位。


alo*_*rez 68

TL:博士;要使文件继承容器文件夹的组,请执行以下操作:

$ chmod g+s somefolder
Run Code Online (Sandbox Code Playgroud)

注意:它隐含在接受的答案中,这只是一个片段。

  • setgid 意味着 *new* 文件和文件夹将具有正确的组,但请记住,如果您将文件移动到树中,它们将不会配置正确的所有者。ACL 方法解决了这个问题(一般来说)。 (4认同)
  • 我必须为设置添加`-R`以向下传播目录树 (2认同)

use*_*039 11

作为 slm 答案的补充,请注意,在 ext2/3/4 文件系统上,您可以通过使用bsdgroups分区上的挂载选项来复制您描述的 BSD 行为。从mount(1)手册页:

grpid|bsdgroups and nogrpid|sysvgroups
              These options define what group id a newly  created  file  gets.
              When  grpid  is  set,  it takes the group id of the directory in
              which it is created; otherwise (the default) it takes the  fsgid
              of  the current process, unless the directory has the setgid bit
              set, in which case it takes the gid from the  parent  directory,
              and also gets the setgid bit set if it is a directory itself.
Run Code Online (Sandbox Code Playgroud)