设置目录中文件的默认用户名和组

设置目录中文件的默认用户名和组

使用有用的帖子我可以在文件夹中设置默认组和文件权限。

我在设置默认所有者(teamlead uid 1234)时遇到问题。

setfacl -d -m g::rwx /my/test/folder
setfacl -d -m o::rx /my/test/folder

getfacl /my/test/folder

# file: /my/test/folder
# owner: teamlead
# group: web_prod
# flags: -s-
user::rwx
group::r-x
other::r-x
default:user::rwx
default:group::rwx
default:other::r-x

接着就,随即:

[mary@boxen]# touch /my/test/folder/somefile
[mary@boxen]# ll /my/test/folder/somefile
-rw-rw-r--. 1 mary web_prod 0 Nov  6 08:58 somefile

因此,分配了正确的组,但新文件拥有创建该文件的用户的所有权。我希望新创建的文件具有 teamlead:web_prod 所有者/组。

似乎setfacl也可以用于设置默认用户。使用现有的文件夹 acl 配置(上面):

[mary@boxen]# setfacl -d -m u:1234:rwx /my/test/folder

现在以不同的用户身份创建文件。我希望它拥有 teamlead:web_prod 所有权。

[mary@boxen]# touch /my/test/folder/anotherfile
[mary@boxen]# ll /my/test/folder/anotherfile
-rw-rw-r--+ 1 mary web_prod 0 Nov  6 08:58 somefile

新文件仍然拥有创建该文件的所有者的所有权,而不是 uid 1234(teamlead)。

我所追求的是否可能,或者我解决这个问题的方式是错误的?

答案1

使用 setfacl,您可以为新创建的文件设置默认权限,但不能设置默认所有者/组。

要让新文件归特定用户所有,您需要一个 setuid 位,其作用类似于目录上的 setgid 位。不幸的是,这没有实施。

使用setfacl,您可以做一些在大多数情况下几乎相同的事情:您可以设置一个ACL,例如default:user:teamlead:rwx(例如setfacl -d -m u:teamlead:rwx foo)。这样,即使其他人拥有该文件,指定用户也可以写入新文件。

答案2

总是会创建一个新文件,该文件属于创建该文件的进程所运行的用户。(准确地说,是有效用户 ID。)这无法更改,因为允许用户创建属于其他用户的文件将是一个安全漏洞,类似于允许非 root 用户泄露文件

无论您想做什么,都不需要这样做。 ACL 足以确保以后需要读取该文件的任何人都拥有足够的权限。将文件保留为创建该文件的用户所拥有。

答案3

如果您希望使用新组创建新文件,则需要更改主要组。

为此,您可以使用用户模式和参数-g

   -g, --gid GROUP
       The group name or number of the user's new initial login group. The group must exist.
       Any file from the user's home directory owned by the previous primary group of the user will be owned by this new group.
       The group ownership of files outside of the user's home directory must be fixed manually.

例如

test2@kinakuta:/tmp$ id
uid=1002(test2) gid=1002(test2) grupos=1002(test2),1003(testgroup)
test2@kinakuta:/tmp$ touch test2
test2@kinakuta:/tmp$ ls -la test2
-rw-r--r-- 1 test2 test2 0 nov 23 22:26 test2
root@kinakuta:/tmp# usermod -g testgroup test2
root@kinakuta:/tmp# su test2
test2@kinakuta:/tmp$ touch test2_1
test2@kinakuta:/tmp$ ls -la test2_1 
-rw-r--r-- 1 test2 testgroup 0 nov 23 22:27 test2_1

答案4

在 Linux 上,您必须在父目录上具有 sgid 才能继承文件组。 (尽管 BSD 系统上的目录不需要 sgid。)

相关内容