Linux ACL简介
用户权限管理始终是 Unix 系统管理中最重要的环节。大家对 Linux/Unix 的 UGO 权限管理方式一定不陌生,还有最常用的 chmod 命令。为了实现一些比较复杂的权限管理,往往不得不创建很多的组,并加以详细的记录和区分(很多时候就是管理员的噩梦)。可以针对某一个用户对某一文件指定一个权限,恐怕管理员都期待的功能。比如对某一个特定的文件,用户A可以读取,用户B所在的组可以修改,惟独用户B不可以……。于是就有了IEEE POSIX 1003.1e这个ACL的标准。所谓ACL,就是Access Control List,一个文件/目录的访问控制列表,可以针对任意指定的用户/组分配RWX权限。现在主流的商业Unix系统都支持ACL。FreeBSD也提供了对ACL的支持。Linux在这个方面也不会落后,从2.6版内核开始支持ACL。
准备工作
省略XFS和CephFS的挂载过程,对于CephFS需要开启posix acl的开关(否则不可以使用setfacl/getfacl),可以在配置文件里面或者使用socket file动态打开。
CephFS配置项准备
# ceph.conf [global]添加
fuse default permissions = 0
client acl type = posix_acl
用户组和用户添加
## 添加新的用户组testg1
[root@xt1 test_acl]# groupadd testg1
## 添加两个新用户
[root@xt1 test_acl]# useradd testu1
[root@xt1 test_acl]# useradd testu2
# 把testu1的附加群组设置为testg1
[root@xt1 test_acl]# usermod -G testg1 testu1
# 把guanyunfei,yawei的附加群组设置为root
[root@xt1 test_acl]# usermod -G root guanyunfei
[root@xt1 test_acl]# usermod -G root yawei
文件的ACL
ACL基本操作:添加修改
- Linux XFS
[root@xt1 mnt]# touch mike
[root@xt1 mnt]# ll mike
-rw-r--r-- 1 root root 0 2月 11 17:17 mike
[root@xt1 mnt]# getfacl mike
# file: mike
# owner: root
# group: root
user::rw-
group::r--
other::r--
权限解释:
文件属主(root) | 文件所属组内其他用户(root) | 其他组用户 |
---|---|---|
rw | r | r |
切换到testu1用户来写该文件,但是不允许!
[root@xt1 ~]# su - testu1
上一次登录:一 2月 11 16:03:19 CST 2019pts/13 上
[testu1@xt1 ~]$ echo "1111" >> /mnt/mike
-bash: /mnt/mike: Permission denied
因为file1并不允许除了root以外的用户写。我们现在就通过修改file1的ACL赋予testu1足够的权限:
[root@xt1 mnt]# setfacl -m u:testu1:rw mike
[root@xt1 mnt]# getfacl mike
# file: mike
# owner: root
# group: root
user::rw-
user:testu1:rw-
group::r--
mask::rw-
other::r--
[root@xt1 mnt]# ll mike
-rw-rw-r--+ 1 root root 0 2月 11 17:17 mike
权限解释:
文件属主(root) | 文件所属组内其他用户(root) | 其他组用户 | 附加用户(testu1) | 掩码 |
---|---|---|---|---|
rw | r | r | rw | rw |
当文件设置ACL规则后,权限位的第二组显示的不再是属主同组对该文件的访问权限,而是显示ACL的掩码。 所以当ls看到权限位后面有‘+’号时,应该使用getfacl查看文件属主所在组的权限。
用testu1用户写该文件:成功,因为testu1拥有rw权限
[testu1@xt1 ~]$ echo "1111" >> /mnt/mike
用testu2用户写该文件:失败,因为testu2不拥有rw权限
[testu2@xt1 mnt]$ echo "2222" >> mike
-bash: mike: Permission denied
-
CephFS Jewel
挂载点为/mnt/seven/
[root@xt1 seven]# touch mike
[root@xt1 seven]# ll mike
-rw-r--r-- 1 root root 0 2月 11 18:07 mike
[root@xt1 seven]# getfacl mike
# file: mike
# owner: root
# group: root
user::rw-
group::r--
other::r--
权限解释:
文件属主(root) | 文件所属组内其他用户(root) | 其他组用户 |
---|---|---|
rw | r | r |
用testu1用户写该文件:失败,原理同linux XFS一样,因为testu1不具备w权限
[testu1@xt1 seven]$ echo "testu1" >> mike
-bash: mike: Permission denied
现在就通过修改mike的ACL赋予testu1足够的权限:
[root@xt1 seven]# setfacl -m u:testu1:rw mike
[root@xt1 seven]# getfacl mike
# file: mike
# owner: root
# group: root
user::rw-
user:testu1:rw-
group::r--
mask::rw-
other::r--
[root@xt1 seven]# ll mike
-rw-rw-r--+ 1 root root 0 2月 11 18:07 mike
权限解释:
文件属主(root) | 文件所属组内其他用户(root) | 其他组用户 | 附加用户(testu1) | 掩码 |
---|---|---|---|---|
rw | r | r | rw | rw |
用testu1用户写该文件:成功,CephFS不支持对文件读写控制的ACL
[testu1@xt1 seven]$ echo "testu1" >> mike
小结:CephFS Jewel 支持对文件读写控制的ACL
-
CephFS Luminous
把上面的实验在Luminous上做一遍:
[root@localhost seven]# touch /mnt/seven/mike
# 用testu1用户来写该文件:失败,因为testu1并没有权限
[testu1@localhost seven]$ echo "1111" >> mike
-bash: mike: Permission denied
现在修改mike的ACL赋予testu1写权限,testu1写入成功
[root@localhost ~]# setfacl -m u:testu1:rw /mnt/seven/mike
root@localhost ~]# getfacl /mnt/seven/mike
getfacl: Removing leading '/' from absolute path names
# file: mnt/seven/mike
# owner: root
# group: root
user::rw-
user:testu1:rw-
group::r--
mask::rw-
other::r--
[testu1@localhost seven]$ echo "testu1" >> mike
[testu1@localhost sevenl]$ cat /mnt/seven/mike
testu1
小结:CephFS Luminous 支持文件的ACL属性!
ACL其他功能(文件):删除和覆盖
-
Linux XFS
删除一个文件的ACL项
[root@xt1 mnt]# setfacl -x u:guanyunfei mike
[root@xt1 mnt]# getfacl /mnt/mike
getfacl: Removing leading '/' from absolute path names
# file: mnt/mike
# owner: root
# group: root
user::rw-
user:testu1:rw-
group::r--
group:testg1:r--
mask::rw-
other::r--
# group:testg1:r-- 表示这个组的用户只能是r--权限。顾testu1可以读写,而testu2只可以读取该文件
[testu1@xt1 mnt]$ echo "testu1" >> mike
[testu1@xt1 mnt]$ cat mike
1111
testu1
[testu2@xt1 mnt]$ echo "testu2" >> mike
bash: mike: 权限不够
[testu2@xt1 mnt]$ cat mike
1111
testu1
#删除一个组的acl权限
[root@xt1 ~]# setfacl -x g:testg1 /mnt/mike
[root@xt1 ~]# getfacl /mnt/mike
getfacl: Removing leading '/' from absolute path names
# file: mnt/mike
# owner: root
# group: root
user::rw-
user:testu1:rw-
group::r--
mask::rw-
other::r--
我们发现group:testg1:r--这一行不见了(但是testu2依然不可以写,只有它被赋予特殊权限)
#删除一个用户的acl权限
[root@xt1 ~]# setfacl -x u:testu1 /mnt/mike
[root@xt1 ~]# getfacl /mnt/mike
getfacl: Removing leading '/' from absolute path names
# file: mnt/mike
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--
uesr:testu1:rw这一行不见了,现在testu1不能写了
[testu1@xt1 mnt]$ echo "testu1" >> mike
-bash: mike: Permission denied
# 去掉所有的acl可以用-b选项
[root@xt1 ~]# setfacl -b /mnt/mike
[root@xt1 ~]# getfacl /mnt/mike
getfacl: Removing leading '/' from absolute path names
# file: mnt/mike
# owner: root
# group: root
user::rw-
group::r--
other::r--
目录的ACL
-
Linux XFS
root用户创建的目录不允许其他用户创建文件和写数据
[root@xt1 mnt]# mkdir test_acl
[root@xt1 mnt]# getfacl test_acl/
# file: test_acl/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
drwxr-xr-x 2 root root 6 2月 12 14:17 test_acl
# 使用yawei用户(附加群组为root)创建文件:失败
[yawei@xt1 ~]$ touch /mnt/test_acl/kk_test
touch: cannot touch ‘/mnt/test_acl/kk_test’: Permission denied
# 使用别组用户testu1创建文件:失败
[testu1@xt1 mnt]$ touch /mnt/test_acl/kk_test
touch: cannot touch ‘/mnt/test_acl/kk_test’: Permission denied
但是都可以做ls操作
修改目录acl使用户testu1可以创建文件和写数据
[root@xt1 mnt]# getfacl /mnt/test_acl
getfacl: Removing leading '/' from absolute path names
# file: mnt/test_acl
# owner: root
# group: root
user::rwx
user:testu1:rwx
group::r-x
mask::rwx
other::r-x
drwxrwxr-x+ 2 root root 36 2月 12 14:26 test_acl
权限解释:
文件属主(root) | 文件所属组内其他用户(root) | 其他组用户 | 附加用户(testu1) | 掩码 |
---|---|---|---|---|
rwx | r-x | r-x | rwx | rwx |
用testu1用户可以创建,删除文件并且可以读写数据:因为testu1拥有rwx权限
[testu1@xt1 mnt]$ touch /mnt/test_acl/kk_test
[testu1@xt1 mnt]$ getfacl /mnt/test_acl/kk_test
getfacl: Removing leading '/' from absolute path names
# file: mnt/test_acl/kk_test
# owner: testu1
# group: testu1
user::rw-
group::rw-
other::r--
[testu1@xt1 mnt]$ echo "testu1" >> /mnt/test_acl/kk_test
[testu1@xt1 mnt]$ rm -rf /mnt/test_acl/kk_test
-
CephFS Jewel
把上面的测试过程在CephFS里面实验一遍:
mkdir /mnt/seven/test_acl
root@xt1 seven]# getfacl /mnt/seven/test_acl/
getfacl: Removing leading '/' from absolute path names
# file: mnt/seven/test_acl/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
drwxr-xr-x 1 root root 0 2月 12 14:37 test_acl
# 使用yawei用户(附加群组为root)创建文件:失败
[yawei@xt1 ~]$ touch /mnt/seven/test_acl/kk_test
touch: cannot touch ‘/mnt/seven/test_acl/kk_test’: Permission denied
# 使用别组用户testu1创建文件:失败
[testu1@xt1 mnt]$ touch /mnt/seven/test_acl/kk_test
touch: cannot touch ‘/mnt/seven/test_acl/kk_test’: Permission denied
修改目录acl使用户testu1可以创建文件和写数据
[root@xt1 seven]# setfacl -m u:testu1:rwx /mnt/seven/test_acl/
[root@xt1 seven]# getfacl /mnt/seven/test_acl/
getfacl: Removing leading '/' from absolute path names
# file: mnt/seven/test_acl/
# owner: root
# group: root
user::rwx
user:testu1:rwx
group::r-x
mask::rwx
other::r-x
权限解释:
文件属主(root) | 文件所属组内其他用户(root) | 其他组用户 | 附加用户(testu1) | 掩码 |
---|---|---|---|---|
rwx | r-x | r-x | rwx | rwx |
用户testu1可以创建文件:说明CephFS jewel支持目录的ACL属性
[testu1@xt1 mnt]$ touch /mnt/seven/test_acl/kk_test
-
CephFS Luminous
把上述实验在Luminous版做一遍:得出结论Luminous版CephFS支持目录的ACL。
# 在root用户创建目录
[root@localhost ~]# mkdir /mnt/seven/test_acl
[root@localhost ~]# getfacl /mnt/seven/test_acl/
getfacl: Removing leading '/' from absolute path names
# file: mnt/seven/test_acl/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
# 在testu1用户创建文件:权限不允许
[testu1@localhost test_acl]$ touch kk_test
touch: cannot touch ‘kk_test’: Permission denied
修改目录ACL给testu1用户足够权限后,testu1可以创建,删除文件,并且读写数据!
[root@localhost ~]# setfacl -m u:testu1:rwx /mnt/seven/test_acl/
[root@localhost ~]# getfacl /mnt/seven/test_acl/
getfacl: Removing leading '/' from absolute path names
# file: mnt/seven/test_acl/
# owner: root
# group: root
user::rwx
user:testu1:rwx
group::r-x
mask::rwx
other::r-x
# 在testu1用户创建文件
[testu1@localhost test_acl]$ touch kk_test
[testu1@localhost test_acl]$ echo "1111" >> kk_test
小结: CephFS Jewel 和Luminous 均支持对目录的ACL!
实验结果
文件系统 | 文件ACL | 目录ACL |
---|---|---|
Linux XFS | 严格支持 | 严格支持 |
CephFS Jewel | 不能严格支持 | 不能严格支持 |
CephFS Luminous | 严格支持 | 严格支持 |
总结
CephFS Jewel 和 Luminous 的filesystem都支持POSIX ACL。
参考文献:
https://www.ibm.com/developerworks/cn/linux/l-acl/index.html
网友评论