权限管理-ACL
作者:尹正杰
版权声明:原创作品,谢绝转载!否则将追究法律责任。
一.ACL权限简介与开启
1.ACL权限简介
比如在根下有一个目录(”/yinzhengjie“),这个目录的所有者和所属组都是root,查看其权限是770,这意味着,只有root用户或是在root组里面的用户才能操作这个文件。如果有一个人不是root又不是root这个组里面的用户,但是就是有一个需求想要看”/yinzhengjie“这个目录下的内容该如何呢?
Linux在这一点的处理上和windows有点像,它的操作方法就是给这个用户加权限即可。让其有访问这个目录的权限即可,这个时候我们就得学习一下ACL啦。
2.查看分区ACL权限是否开启
要想利用ACL这个权限,还得看分区是否支持ACL权限哟~
1 [root@yinzhengjie ~]# df -h 2 Filesystem Size Used Avail Use% Mounted on 3 /dev/sda2 18G 2.8G 14G 17% / 4 tmpfs 491M 224K 491M 1% /dev/shm 5 /dev/sda1 283M 28M 240M 11% /boot 6 [root@yinzhengjie ~]# 7 [root@yinzhengjie ~]# 8 [root@yinzhengjie ~]# 9 [root@yinzhengjie ~]# dumpe2fs -h /dev/sda2 | grep "Default mount options" 10 dumpe2fs 1.41.12 (17-May-2010) 11 Default mount options: user_xattr acl 12 [root@yinzhengjie ~]#

温馨提示:如果上图中没有ACL权限的话,也不要慌,还是有解决方案的。
a>.临时开启分区ACL权限:(重新挂载根分区,并挂载加入acl权限。)
[root@yinzhengjie ~]# mount -o remount,acl /
[root@yinzhengjie ~]#
b>.编辑"/etc/fstab"配置文件(它是开机自动挂载的文件。)
修改这个配置文件一定要小心,一旦修改错误,系统就会崩溃,你将无法启动你的操作系统哟,因为你挂载根就会挂载失败啊,当然,还是可以通过救援模式进行修复的。

二.查看与设定ACL权限
1.查看ACL命令(getfacl)

2.设定ACL权限的命令(setfacl)
我们先看看常用的几个参数的解释:

1 [root@yinzhengjie ~]# mkdir /yinzhengjie/
2 [root@yinzhengjie ~]# useradd canglaoshi
3 [root@yinzhengjie ~]# useradd jichimingbu
4 [root@yinzhengjie ~]# groupadd AVgroup
5 [root@yinzhengjie ~]# gpasswd -a canglaoshi AVgroup
6 Adding user canglaoshi to group AVgroup
7 [root@yinzhengjie ~]# gpasswd -a jichimingbu AVgroup
8 Adding user jichimingbu to group AVgroup
9 [root@yinzhengjie ~]#
10 [root@yinzhengjie ~]# tail -1 /etc/group
11 AVgroup:x:504:canglaoshi,jichimingbu
12 [root@yinzhengjie ~]#
13 [root@yinzhengjie ~]# chown root:AVgroup /yinzhengjie/
14 [root@yinzhengjie ~]# chmod 770 /yinzhengjie/
15 [root@yinzhengjie ~]# ll -d /yinzhengjie/
16 drwxrwx---. 4 root AVgroup 4096 9月 28 12:56 /yinzhengjie/
17 [root@yinzhengjie ~]#
18 [root@yinzhengjie ~]#
19 [root@yinzhengjie ~]# useradd yangmi
20 [root@yinzhengjie ~]# echo "123456" | passwd --stdin yangmi
21 更改用户 yangmi 的密码 。
22 passwd: 所有的身份验证令牌已经成功更新。
23 [root@yinzhengjie ~]#
24 [root@yinzhengjie ~]# setfacl -m u:yangmi:rx /yinzhengjie/
25 [root@yinzhengjie ~]#
26 [root@yinzhengjie ~]# getfacl /yinzhengjie/
27 getfacl: Removing leading '/' from absolute path names
28 # file: yinzhengjie/
29 # owner: root
30 # group: AVgroup
31 user::rwx
32 user:yangmi:r-x
33 group::rwx
34 mask::rwx
35 other::---
36
37 [root@yinzhengjie ~]#
38 [root@yinzhengjie ~]# ll -d /yinzhengjie/
39 drwxrwx---+ 4 root AVgroup 4096 9月 28 12:56 /yinzhengjie/
40 [root@yinzhengjie ~]#
41 [root@yinzhengjie ~]# su yangmi
42 [yangmi@yinzhengjie root]$
43 [yangmi@yinzhengjie root]$ cd /yinzhengjie/
44 [yangmi@yinzhengjie yinzhengjie]$ ls
45 golang postfix
46 [yangmi@yinzhengjie yinzhengjie]$ touch {1..5}.txt
47 touch: 无法创建"1.txt": 权限不够
48 touch: 无法创建"2.txt": 权限不够
49 touch: 无法创建"3.txt": 权限不够
50 touch: 无法创建"4.txt": 权限不够
51 touch: 无法创建"5.txt": 权限不够
52 [yangmi@yinzhengjie yinzhengjie]$
创建之后,可以进行以下测试,发现之后读取权限确实没有写的权限呢

如果你想要对一个组设置ACL权限,操作和用户类似:

三.最大有效权限与删除ACL权限
1.最大有限权限mask
mask是用来指定最大有效权限的。如果我给用户赋予了ACL权限,是需要和mask的权限“相与”才能得到用户的真正权限。

mask权限一般适用于对一个文件提前做出最大权限的规划,这样即使我用acl给其他用户分配权限过高,但是只要和mask进行相与操作,最终用户得到的权限仍然在我们的控制范围之内呢。

2.删除ACL权限
1 [root@yinzhengjie ~]# setfacl -x g:movie /yinzhengjie/
2 [root@yinzhengjie ~]# getfacl /yinzhengjie/
3 getfacl: Removing leading '/' from absolute path names
4 # file: yinzhengjie/
5 # owner: root
6 # group: AVgroup
7 user::rwx
8 user:yangmi:r-x
9 group::rwx
10 mask::rwx
11 other::---
12
13 [root@yinzhengjie ~]#
14 [root@yinzhengjie ~]# setfacl -b /yinzhengjie/
15 [root@yinzhengjie ~]# getfacl /yinzhengjie/
16 getfacl: Removing leading '/' from absolute path names
17 # file: yinzhengjie/
18 # owner: root
19 # group: AVgroup
20 user::rwx
21 group::rwx
22 other::---
23
24 [root@yinzhengjie ~]# ll -d /yinzhengjie/
25 drwxrwx---. 4 root AVgroup 4096 9月 28 12:56 /yinzhengjie/
26 [root@yinzhengjie ~]#
四.默认ACL权限和递归ACL权限
1.递归ACL权限
递归是父目录在设定ACL权限时,所有的子文件和子目录也会拥有相同的ACL权限。
1 [root@yinzhengjie ~]# mkdir /yinzhengjie
2 [root@yinzhengjie ~]# touch /yinzhengjie/{1..5}.txt
3 [root@yinzhengjie ~]# ll /yinzhengjie/
4 总用量 0
5 -rw-r--r--. 1 root root 0 9月 28 23:29 1.txt
6 -rw-r--r--. 1 root root 0 9月 28 23:29 2.txt
7 -rw-r--r--. 1 root root 0 9月 28 23:29 3.txt
8 -rw-r--r--. 1 root root 0 9月 28 23:29 4.txt
9 -rw-r--r--. 1 root root 0 9月 28 23:29 5.txt
10 [root@yinzhengjie ~]#
11 [root@yinzhengjie ~]# setfacl -m u:yangmi:rx -R /yinzhengjie/
12 [root@yinzhengjie ~]#
13 [root@yinzhengjie ~]# ll /yinzhengjie/
14 总用量 20
15 -rw-r-xr--+ 1 root root 0 9月 28 23:29 1.txt
16 -rw-r-xr--+ 1 root root 0 9月 28 23:29 2.txt
17 -rw-r-xr--+ 1 root root 0 9月 28 23:29 3.txt
18 -rw-r-xr--+ 1 root root 0 9月 28 23:29 4.txt
19 -rw-r-xr--+ 1 root root 0 9月 28 23:29 5.txt
20 [root@yinzhengjie ~]#
21 [root@yinzhengjie ~]# getfacl /yinzhengjie/1.txt
22 getfacl: Removing leading '/' from absolute path names
23 # file: yinzhengjie/1.txt
24 # owner: root
25 # group: root
26 user::rw-
27 user:yangmi:r-x
28 group::r--
29 mask::r-x
30 other::r--
31
32 [root@yinzhengjie ~]#
2.默认ACL权限
上面的递归权限仅仅适用于当前,它会使得所有的子目录或子文件的权限被设置的父目录相同的ACL,但是,如果在这个父目录或子目录创建任意一个文件时,其权限仍然是系统的默认权限哟~而不会遵守之前的ACL权限哟~这个时候我们就需要设置默认权限了,只要设置了默认权限加递归权限的话,那么无论在父目录或是子目录创建文件其权限都是你设置的默认权限哟~(如果你担心权限过大,也可以给你某个目录设置mask权限即可。)