如何让多个用户访问 chmod() 同一个文件?

Jac*_*own 3 linux chmod gnu posix setfacl

我有一个系统,其中多个用户正在运行chmod相同文件的应用程序。我尝试使用setacl将两个用户添加为文件的用户所有者来执行此操作,但它不起作用。由于chmod系统调用失败,应用程序出错。

见示例:

[jacob@macbook-debian ~/Projects/test] getfacl bin/testfile
# file: bin/testfile
# owner: root
# group: root
user::rwx
user:jacob:rwx
user:jason:rwx
group::r-x
group:www-data:rwx
mask::rwx
other::r-x


[jacob@macbook-debian ~/Projects/test] chmod 0755 bin/testfile
chmod: changing permissions of 'bin/testfile': Operation not permitted
Run Code Online (Sandbox Code Playgroud)

Tho*_*mas 5

在添加用户时使用 Linux ACL,您不会将他们添加为文件的所有者。这只是打开/修改和写入该文件的特权。
当调用chownchmod有效用户ID将是例如jacob与其中一个将针对在值相匹配owner。如果不匹配,您的命令将失败。

man 2 chmod.

调用进程的有效 UID 必须与文件的所有者匹配,或者该进程必须具有特权(Linux:它必须具有 CAP_FOWNER 功能)。

另一种方法是设置调用 process/binary 所描述的功能chmod。但这会带来很大的安全问题,因为每个人都可以使用此命令更改权限。
是一个关于用户更细粒度的功能访问的线程,但它似乎不是很直接。

根据您的用例的限制,您可能希望添加sudo规则供用户使用,chmod或者您评估为什么用户必须chmod在他们不拥有的文件上运行。也许umask在文件创建期间使用就足够了。

如果您的用户属于对该目录具有写权限的组,您还可以复制有问题的文件,删除原始文件并将副本移至原始名称。这将导致用户将拥有复制的文件并且可以执行chmod.

[user@localhost testdir]$ ll
total 12K
drwxrwxr-x  2 root user 4.0K Jul 14 11:49 .
drwxr-xr-x  3 user user 4.0K Jul 14 11:47 ..
-rw-rw----+ 1 root user    5 Jul 14 11:41 testfile
[user@localhost testdir]$ getfacl testfile 
# file: testfile
# owner: root
# group: user
user::rw-
user:user:rw-
group::rw-
group:user:rw-
mask::rw-
other::---

[user@localhost testdir]$ chmod 777 testfile
chmod: changing permissions of 'testfile': Operation not permitted
[user@localhost testdir]$ cp -a testfile testfile.copy
[user@localhost testdir]$ getfacl *
# file: testfile
# owner: root
# group: user
user::rw-
user:user:rw-
group::rw-
group:user:rw-
mask::rw-
other::---

# file: testfile.copy
# owner: user
# group: user
user::rw-
user:user:rw-
group::rw-
group:user:rw-
mask::rw-
other::---

[user@localhost testdir]$ mv testfile.copy testfile
[user@localhost testdir]$ ll
total 12K
drwxrwxr-x  2 root user 4.0K Jul 14 11:50 .
drwxr-xr-x  3 user user 4.0K Jul 14 11:47 ..
-rw-rw----+ 1 user user    5 Jul 14 11:41 testfile

[user@localhost testdir]$ chmod 777 testfile
[user@localhost testdir]$ ll
total 12K
drwxrwxr-x  2 root user 4.0K Jul 14 11:50 .
drwxr-xr-x  3 user user 4.0K Jul 14 11:47 ..
-rwxrwxrwx+ 1 user user    5 Jul 14 11:41 testfile
Run Code Online (Sandbox Code Playgroud)