Jur*_*man 3 linux filesystems permissions chmod chown
我们运行网络服务器,我们有以下情况:
所有用户都必须对 /var/www/mysite 具有读+写访问权限。我们目前通过拥有 /var/www/site 到 www-data 的组来实现这一点。然后我们在组上设置 write + setgid 位以确保所有子目录都具有相同的权限。
现在,这一切运行良好,但我们在以下场景中遇到了问题:
bower install
. bower install
第一次运行的用户拥有该public/bower_components
目录并且没有设置 setgid 位public/scripts/src
topublic/scripts/dist
和第一个运行的用户缩小到gulp build
拥有这些文件在这两种情况下,afind path/to/dir -type d -exec chmod g+ws {} \;
确实可以缓解问题,但是否有可能首先解决此问题?我们已经在/var/www/mysite
目录上设置了 setgid 位,那么为什么不bower install
遵循这个权限集呢?
如果没有,有没有更好的方法来解决这个问题?我们曾想过在部署自动化过程中设置这些位,但如果用户忘记了 setgid 位,我们认为自动化部署也会卡住。
Posix ACL 是唯一明确的优雅方式,这就是我处理共享读/写资源冲突的方式,尤其是在基于 Web 的系统上。这是一个运行示例。
在我的示例中,我有一个名为/var/www/html/share
. 此外,我还有用户alice
, bob
,deploy
和bower
首先,我创建了一个名为的组html
,然后将用户添加到该组中。
groupadd html
for i in alice bob deploy bower; do usermod -a -G html $user; done
Run Code Online (Sandbox Code Playgroud)
现在,我已将针对该html
组的文件 ACL 添加到该文件夹中。
setfacl -m g:html:rwx /var/www/html/share
setfacl -d -m g:html:rwx /var/www/html/share
Run Code Online (Sandbox Code Playgroud)
第二个命令很重要,它会导致继承发生。
现在,我们可以测试它的行为。
# for user in alice bob bower deploy; do \
sudo -u $user touch "/var/www/html/share/file_${user}" \
done
[root@localhost html]# ls -l /var/www/html/share/
total 0
-rw-rw-r--+ 1 alice alice 0 May 13 23:08 file_alice
-rw-rw-r--+ 1 bob bob 0 May 13 23:08 file_bob
-rw-rw-r--+ 1 bower bower 0 May 13 23:08 file_bower
-rw-rw-r--+ 1 deploy deploy 0 May 13 23:08 file_deploy
[root@localhost html]#
Run Code Online (Sandbox Code Playgroud)
乍一看,文件所有权似乎都很简单,不允许一个用户与其他用户交互。但是,我们可以使用getfacl
显示更多信息的 ACL 检查 ACL 。
# getfacl file_Al
getfacl: file_Al: No such file or directory
[root@localhost share]# getfacl file_alice
# file: file_alice
# owner: alice
# group: alice
user::rw-
group::r-x #effective:r--
group:html:rwx #effective:rw-
mask::rw-
other::r--
Run Code Online (Sandbox Code Playgroud)
您可以看到该html
组可以控制这些文件。
注意:标准的 unix GROUP 权限表示针对权限的掩码。就像rw
文件一样,它也有效地rw
用于 ACL,尽管授予的实际权限是rwx
.
让我们测试其他用户可以修改/删除这些文件。
# sudo -u alice /bin/bash
[alice@localhost share]$ pwd
/var/www/html/share
[alice@localhost share]$ echo "hello world" >>file_alice
[alice@localhost share]$ echo "hello world" >>file_bob
[alice@localhost share]$ echo "hello world" >>file_deploy
[alice@localhost share]$ echo "hello world" >>file_bower
[alice@localhost share]$ ll
total 16
-rw-rw-r--+ 1 alice alice 12 May 13 23:15 file_alice
-rw-rw-r--+ 1 bob bob 12 May 13 23:15 file_bob
-rw-rw-r--+ 1 bower bower 12 May 13 23:15 file_bower
-rw-rw-r--+ 1 deploy deploy 12 May 13 23:15 file_deploy
[alice@localhost share]$ rm file_deploy
Run Code Online (Sandbox Code Playgroud)
最后,棘手的一点。当 alice 创建目录时会发生什么?
[alice@localhost share]$ mkdir dir_alice;
[alice@localhost share]$ ls -l
total 12
drwxrwxr-x+ 1 alice alice 0 May 13 23:16 dir_alice
-rw-rw-r--+ 1 alice alice 12 May 13 23:15 file_alice
-rw-rw-r--+ 1 bob bob 12 May 13 23:15 file_bob
-rw-rw-r--+ 1 bower bower 12 May 13 23:15 file_bower
[alice@localhost share]$ getfacl dir_alice
# file: dir_alice
# owner: alice
# group: alice
user::rwx
group::r-x
group:html:rwx
mask::rwx
other::r-x
default:user::rwx
default:group::r-x
default:group:html:rwx
default:mask::rwx
default:other::r-x
Run Code Online (Sandbox Code Playgroud)
因为我们传递-d
给setfacl
它指示新目录也继承(并将该继承应用于进一步的子目录)。因此,现在作为用户,deploy
我们可以在该新子目录中添加和编辑现有文件。
[alice@localhost share]$ touch dir_alice/file_indir_alice
[alice@localhost share]$ exit
exit
# sudo -u deploy /bin/bash
[deploy@localhost share]$ cd /var/www/html/share/dir_alice/
[deploy@localhost dir_alice]$ touch file_indir_deploy
[deploy@localhost dir_alice]$ rm file_indir_alice
[deploy@localhost dir_alice]$
Run Code Online (Sandbox Code Playgroud)