授予多个用户对 Ubuntu 中文件夹的写权限

The*_*iot 87 linux security acl file-permissions

用户 tomcat6 拥有一个文件夹:

drwxr-xr-x 2 tomcat6 tomcat6 69632 2011-05-06 03:43 document
Run Code Online (Sandbox Code Playgroud)

我想允许另一个用户(ruser)对文档文件夹的写权限。这两个用户(tomcat6 和 ruser)不属于同一个组。我试过使用setfacl

sudo setfacl -m  u:ruser:rwx document
Run Code Online (Sandbox Code Playgroud)

但这给了我setfacl: document: Operation not supported错误。请帮助我。

And*_*ert 169

有两种方法可以做到这一点:将目录设置为“world”可写或为两个用户创建一个新组并使目录可写入该组。

显然,让它成为世界可写是一件坏事,所以第二个选项是可取的。

Linux 中的用户可以属于多个组。在这种情况下,您要创建一个全新的组,我们称之为tomandruser

sudo groupadd tomandruser
Run Code Online (Sandbox Code Playgroud)

现在该组存在,将两个用户添加到其中:

sudo usermod -a -G tomandruser tomcat6
sudo usermod -a -G tomandruser ruser
Run Code Online (Sandbox Code Playgroud)

现在剩下的就是设置目录的权限:

sudo chgrp -R tomandruser /path/to/the/directory
sudo chmod -R 770 /path/to/the/directory
Run Code Online (Sandbox Code Playgroud)

现在只有 tomandruser 组的成员才能读取、写入或执行目录中的任何内容。注意 chmod 和 chgrp 命令的 -R 参数:这告诉它们递归到目标目录的每个子目录并修改它找到的每个文件和目录。

您可能还想将 770 更改为诸如774您希望其他人能够读取文件,775如果您希望其他人读取和执行文件等。组分配更改在用户注销并返回之前不会生效在。

如果您还希望(您可能这样做)其中一个用户在目录内创建的新文件可由组中的其他人自动写入,请参阅此处

  • 我会避免使用 chmod 770、775 或其他任何东西。这与 _all_ 文件的权限混乱。而是使用类似`chmod -R g+w` 之类的东西来添加写入权限,而不会破坏其他所有内容。 (8认同)
  • **注意:**“在用户注销并重新登录之前,组分配更改不会生效。” 我错过了:) (4认同)
  • 您可能还想为目录设置 set-group-ID 标志,以使新文件和子目录自动归正确的组所有:`sudo find /path/to/the/directory -type d -exec chmod 2770' {}' \;` (3认同)
  • 如果您想在不更改文件夹所有权的情况下授予用户对文件夹的写访问权限,例如您不想弄乱 apache 对 public_html 文件夹的权限,该怎么办? (3认同)
  • 如果用户在那里创建了一个新文件(例如,通过“SELECT INTO OUTFILE”创建的 mysql),它会为其主要组(在本例中为“mysql”)设置权限,并且无论如何其他用户都无法访问该文件。如何解决这个问题? (2认同)

alp*_*per 5

下面的脚本显示了一个例子给r (read) / w (write) / x (execute)许可给定的文件夹路径/path/to/the/directoryUSER1USER2。如果您只想授予写访问权限,请替换rwxw.


#!/bin/bash

# Block others and people in the same group to do `r/w/x` on the give folder:    
sudo chmod 700 /path/to/the/directory 

# Give read/write/execute access to USER1 on give folder:
sudo setfacl -R -m user:USER1:rwx  /path/to/the/directory 

# Give read/write/execute access to USER2 on give folder:
sudo setfacl -R -m user:USER2:rwx  /path/to/the/directory
Run Code Online (Sandbox Code Playgroud)


Fra*_*cke 5

意见回答:

\n
    \n
  • 我喜欢将共享文件夹放在中心位置。不在别人的主文件夹中,但/srv/common甚至(对于无情的短路径......)/repo或类似的。
  • \n
  • 定义一个新组(通常针对您想要加入的所有本地用户。但是某些技术用户不喜欢wwwuser,除非有正当理由)
  • \n
  • root作为成员是件好事,也有该共享文件夹的中立所有者
  • \n
  • setGid非常重要,这样新文件就会成为公共组成员身份,因此frank:common,不frank:frank
  • \n
\n
    sudo groupadd -f common\n    usermod -aG common root\n    usermod -aG common frank\n    usermod -aG common mike\n\n    # sort of hack for instant group refresh w/o logout\n    # superuser.com/a/345051\n    su - frank\n\n    # sanity test1:\n    cat etc/group | grep common\n        common:x:1008:root,frank,mike\n    # sanity test2:\n    groups\n        frank adm cdrom ... common\n    sudo chown root:common /repo\n\n    # (if you have shareable stuff setting somewhere else,\n    #  copy it to here now)\n\n    # no right to the world, the right rights to user and group\n    chmod -R ug+rwXs,o-rwx $dest\n    # why uppercase X ? \xe2\x86\x92 unix.stackexchange.com/a/416885\n\n    # why s ? \xe2\x86\x92 superuser.com/a/277785\n    # as there is no such thing as an uppercase S (directories only)\n    # settings the s attribute on preexisting content would have to happen\n    # like so:\n    # find /repo -type d -exec chmod g+s {} \\\\\\;\n
Run Code Online (Sandbox Code Playgroud)\n