g4b*_*4b0 5 linux acl permissions debian
我正在尝试在我的ext4
文件系统(在 Debian Jessie 上)上设置一个区域,所有“工作人员”组成员都可以在其中读写现有和新创建的文件。
文件系统使用 acl 选项挂载:
$ more /etc/fstab | grep acl
UUID=f47c5337-a6e1-4554-b8c0-e05c761ec835 / ext4 noatime,errors=remount-ro,acl 0 1
Run Code Online (Sandbox Code Playgroud)
我创建了一些 bash 脚本,以便在目录上设置 ACL 并针对各种场景对其进行测试。
第一个 ( createtestdir.sh
) 创建一个测试目录,其中包含一些文件和目录:
#!/bin/bash
if [ $# != 1 ]; then
echo "usage: $0 /path/to/new/directory"
exit
fi
if [ ! -d "$1" ] && [ ! -f "$1" ] && [ ! -L "$1" ]
then
mkdir $1
mkdir $1/dir
touch $1/file
touch $1/dir/file
echo "Directory $1 created"
else
echo "$1 already exist"
fi
Run Code Online (Sandbox Code Playgroud)
第二个 ( setacl.sh
) 在指定目录上设置权限和 ACL,这里可能会出现问题:
#!/bin/bash
if [ $# != 1 ]; then
echo "usage: $0 /path/to/directory"
exit
fi
if [ -d "$1" ]; then
chgrp -R staff $1
chmod g+rwxs $1
setfacl -dm group:staff:rwx $1
/usr/bin/find $1 -type d -exec chmod g+rwxs {} +
/usr/bin/find $1 -type d -exec setfacl -dm group:staff:rwx {} +
/usr/bin/find $1 -type f -exec chmod g+rw {} +
/usr/bin/find $1 -type f -exec setfacl -m group:staff:rw {} +
echo "Permission changed"
else
echo "Directory $1 does not exist"
fi
Run Code Online (Sandbox Code Playgroud)
最后,第三个脚本 ( acltest.sh
) 测试 ACL 创建文件和目录、移动、复制、解压和存档的行为:
#!/bin/bash
if [ $# != 1 ]; then
echo "usage: $0 /path/to/directory"
exit
fi
if [ -d "$1" ]
then
#testing acl
mkdir $1/test_dir
touch $1/test_dir/file
touch $1/test_file
# testing mv
touch $1/file_from_internal
touch file_from_external
ls -l file_from_external
mv file_from_external $1
# testing tar && cp
mkdir test_tar
touch test_tar/file
tar cjf test.tar.bz2 test_tar
cp test.tar.bz2 $1
ls -ld test_tar
ls -l test.tar.bz2
rm -rf test_tar
rm test.tar.bz2
cd $1
tar xjf test.tar.bz2
echo "Test on $1 completed"
else
echo "$1 already exist"
fi
Run Code Online (Sandbox Code Playgroud)
以正确的顺序执行脚本脚本会为您提供以下输出:
$ ./createtestdir.sh Acl
Directory Acl created
$ ./setacl.sh Acl/
Permission changed
$ ./acltest.sh Acl/
-rw-r--r-- 1 gabo gabo 0 feb 24 11:58 file_from_external
drwxr-xr-x 2 gabo gabo 4096 feb 24 11:24 test_tar
-rw-r--r-- 1 gabo gabo 146 feb 24 11:24 test.tar.bz2
Test on Acl/ completed
Run Code Online (Sandbox Code Playgroud)
我打印出目录和文件的权限,分别在Acl test目录中解压缩和复制,以便它们可以与Acl test目录中的相应文件进行比较。
正如您在 ls 中看到的那样,pemissions 和 ACL 不是预期的(当然是我:)
$ ls -l Acl/
totale 16
drwxrwsr-x+ 2 gabo staff 4096 feb 24 11:24 dir
-rw-rw-r--+ 1 gabo staff 0 feb 24 11:24 file
-rw-r--r-- 1 gabo gabo 0 feb 24 11:24 file_from_external
-rw-rw-r--+ 1 gabo staff 0 feb 24 11:24 file_from_internal
drwxrwsr-x+ 2 gabo staff 4096 feb 24 11:24 test_dir
-rw-rw-r--+ 1 gabo staff 0 feb 24 11:24 test_file
drwxr-sr-x+ 2 gabo staff 4096 feb 24 11:24 test_tar
-rw-r--r--+ 1 gabo staff 146 feb 24 11:24 test.tar.bz2
Run Code Online (Sandbox Code Playgroud)
file_from_internal 没问题。它是在目录中创建的,并具有正确的权限和 ACL。
file_from_external 不正确。它是从父目录移出的,它带有旧权限,并且没有 ACL。似乎 mv 命令不适用于 ACL。
test.tar.bz2 很奇怪。它是从父目录复制的,它带有旧权限,并且它实现了错误的 ACL(文件不能被人员组写入):
$ getfacl Acl/test.tar.bz2
# file: Acl/test.tar.bz2
# owner: gabo
# group: staff
user::rw-
group::rwx #effective:r--
group:staff:rwx #effective:r--
mask::r--
other::r--
Run Code Online (Sandbox Code Playgroud)
似乎 cp 也不适用于 ACL
test_tar目录也没有组写权限,但它继承了 ACL 默认行为:/
$ getfacl Acl/test_tar
# file: Acl/test_tar
# owner: gabo
# group: staff
# flags: -s-
user::rwx
group::rwx #effective:r-x
group:staff:rwx #effective:r-x
mask::r-x
other::r-x
default:user::rwx
default:group::rwx
default:group:staff:rwx
default:mask::rwx
default:other::r-x
Run Code Online (Sandbox Code Playgroud)
如果我尝试使用不同的用户(工作人员)在 test_tar 中创建一个文件,我会收到一个权限被拒绝的错误。
我的 setacl.sh 脚本有什么问题?也许有一些我不知道的错误/问题/行为?
我想提供一个可复制的测试套件,以便在不键入大量 shell 命令的情况下在不同环境中进行检查。
这个答案仅解决您的 tar 问题。
默认情况下,tar 实用程序不保留扩展属性。由于 SELinux 上下文存储在扩展属性中,因此在归档文件时上下文可能会丢失。使用 tar --selinux 选项创建保留上下文的存档并从存档中恢复文件。