ACL权限详解

1.ACL简介
2.前期准备
3.ACL的基本操作:添加和修改
4.ACL的其他功能:删除和覆盖
5.目录的默认ACL
6.备份和恢复ACL
7.结束语


1.ACL简介

用户权限管理始终是Linux系统管理中最重要的环节。大家对Linux/Unix的UGO权限管理方式一定不陌生,还有最常用的chmod命令。为了实现一些比较复杂的权限管理,往往不得不创建很多的组,并加以详细的记录和区分。有一种方法可以实现灵活的权限管理(文件的额外赋权机制)除了文件的所有者,所属组和其他人,可以对更多的用户设置权限,这就是访问控制列表(Access Control List)。


2.前期准备

支持ACL需要内核和文件系统的支持。现在2.6内核以上版本配合EXT3/EXT4, JFS, XFS, ReiserFS等文件系统都是可以支持ACL。下面我们使用CentOS6.8和Ext4文件系统开始对Linux的ACL的体验。


首先创建一个1M的空白文件:
[root@anonymous mnt]#dd if=/dev/zero of=testacl bs=1024 count=1000
1000+0 records in
1000+0 records out
1024000 bytes (1.0 MB) copied, 0.00832275 s, 123 MB/s
[root@anonymous mnt]#du -sh testacl
1000K testacl

和一个loop设备联系在一起:
[root@anonymous mnt]#losetup /dev/loop1 testacl

创建一个ext4文件系统:
[root@anonymous mnt]#mkfs.ext4 /dev/loop1
mke2fs 1.41.12 (17-May-2010)
Discarding device blocks: done                           
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128 inodes, 1000 blocks
50 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=1048576
1 block group
8192 blocks per group, 8192 fragments per group
128 inodes per group
Writing inode tables: done                           
Filesystem too small for a journal
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

挂载新建的文件系统:
[root@anonymous mnt]#mount -o rw,acl /dev/loop1 /mnt/testdir
[root@anonymous mnt]#cd testdir/
[root@anonymous testdir]#ls
lost+found

现在我已经得到了一个小型的文件系统。而且是支持ACL的。并且即使彻底损坏也不会影响硬盘上其他有价值的数据。因此可以开始我们的ACL体验了。


3.ACL的基本操作:添加和修改

首先新建一个文件作为实施ACL的对象:
[root@anonymous testdir]#touch file1
[root@anonymous testdir]#ll
total 13
-rw-r–r–. 1 root root     0 Mar 11 15:48 file1
drwx——. 2 root root 12288 Mar 11 15:42 lost+found

然后看一下这个文件缺省的ACL,这时这个文件除了通常的UGO的权限之外,并没有ACL:
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r–
other::r–

下面添加几个用户和组,待会儿我将使用ACL赋予它们不同的权限:
[root@anonymous testdir]#groupadd grp1
[root@anonymous testdir]#useradd user1
[root@anonymous testdir]#useradd user2
[root@anonymous testdir]#usermod -G grp1 user1

我们将用户切换到user1,发现并不能在此目录下创建文件:
[root@anonymous testdir]#su user1
[user1@anonymous testdir]$echo “test” >> file1
bash: file1: Permission denied

失败了,由于file1并不允许除了root以外的用户写,现在我们通过修改file1的ACL赋予user1足够的权限:
[root@anonymous testdir]#setfacl -m u:user1:rw file1
[root@anonymous testdir]#su user1
[user1@anonymous testdir]$echo “test” >> file1
[user1@anonymous testdir]$cat file1
test

修改成功了,用户user1可以对file1做读写操作了。再次查看file1的ACL:
[user1@anonymous testdir]$getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rw-
group::r–
mask::rw-
other::r–

ls一下:
[root@anonymous testdir]#ls -l file1
-rw-rw-r–+ 1 root root 5 Mar 11 15:59 file1

可以看到一个”+”,就在通常我们看到的权限位的旁边。这说明了file1设置了ACL, 接下来我们修改一下grp1的权限,同时给tgrp1这个组以读的权限:
[root@anonymous testdir]#setfacl -m u:user1:rwx,g:grp1:r file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rwx
group::r–
group:grp1:r–
mask::rwx
other::r–

这个时候user1已经有了执行的权限,而grp1这个组也获得了读取文件内容的权限。有人可能已经注意到了两个问题:首先,file1的组权限从r–变成了rw-。其次,mask是什么?为什么也变化了呢?我们先从mask说起。如果说ACL的优先级高于UGO,那么mask就是最后一道防线。它决定了一个用户或组能够得到的最大的权限。这样我们在不破坏已有ACL的定义的基础上,可以临时提高或是降低安全级别:
[root@anonymous testdir]#setfacl -m mask::r file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rwx   #effective:r–
group::r–
group:grp1:r–
mask::r–
other::r–

[root@anonymous testdir]#ls -l file1
-rw-r–r–+ 1 root root 5 Mar 11 15:59 file1

在user1对应的ACL项的后边出现了effective的字样,这是实际user1得到的权限。Mask只对其他用户和组的权限有影响,对owner和other的权限是没有任何影响的。执行ls的结果也显示UGO的设置也有了对应的变化。因为在使用了ACL的情况下,group的权限显示的就是当前的mask。


4.ACL的其他功能:删除和覆盖

如何删除已有的ACL项呢?
[root@anonymous testdir]#setfacl -x g:grp1 file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rwx
group::r–
mask::rwx
other::r–

我们看到grp1的权限已经被去掉了。如果需要去掉所有的ACL可以用-b选项。所有的ACL项都会被去掉。
[root@anonymous testdir]#setfacl -b  file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r–
other::r–

我们可以用–set 设置一些新的ACL项,并把原有的ACL项全部都覆盖掉。和-m不同,-m选项只是修改已有的配置或是新增加一些。–set选项会把原有的ACL项都删除,用新的替代,需要注意的是一定要包含UGO的设置,不能象-m一样只是添加ACL就可以了。比如下边这一段:
[root@anonymous testdir]#setfacl –set u::rw,u:user1:rw,g::r,o::- file1
[root@anonymous testdir]#getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:user1:rw-
group::r–
mask::rw-
other::—

o::-是另一个需要注意的地方。其实完整的写法是other::—,正如u::rw的完整写法是user::rw-。通常我们可以把”-“省略,但是当权限位只包含”-“时,必须至少保留一个。如果写成了o::,就会出现错误。


5.目录的默认ACL

如果我们希望在一个目录中新建的文件和目录都使用同一个预定的ACL,那么我们可以使用默认(Default) ACL。在对一个目录设置了默认的ACL以后,每个在目录中创建的文件都会自动继承目录的默认ACL作为自己的ACL。用setfacl的-d选项就可以做到这一点:
[root@anonymous mnt]#setfacl -d –set g:grp1:rwx testdir
[root@anonymous mnt]#getfacl testdir
# file: testdir
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:group::r-x
default:group:grp1:rwx
default:mask::rwx
default:other::r-x

可以看到默认ACL已经被设置了。建立一个文件试试:
[root@anonymous mnt]#touch testdir/file2
[root@anonymous mnt]#getfacl testdir/file2
# file: testdir/file2
# owner: root
# group: root
user::rw-
group::r-x   #effective:r–
group:grp1:rwx   #effective:rw-
mask::rw-
other::r–
file2自动继承了testdir对grp1设置的ACL。只是由于mask的存在使得grp1只能获得rw-权限。


6.备份和恢复ACL

主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上-p参数。但是tar等常见的备份工具是不会保留目录和文件的ACL信息的。如果希望备份和恢复带有ACL的文件和目录,那么可以先把ACL备份到一个文件里。以后用–restore选项来回复这个文件中保存的ACL信息:

[root@anonymous mnt]#getfacl -R testdir > test.acl
[root@anonymous mnt]#ls -l test.acl
-rw-r–r–. 1 root root 649 Mar 11 16:29 test.acl

我们用-b选项删除所有的ACL数据,来模拟从备份中回复的文件和目录:
[root@anonymous mnt]#setfacl -R -b testdir
[root@anonymous mnt]#getfacl -R testdir
# file: testdir
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

# file: testdir/file1
# owner: root
# group: root
user::rw-
group::r–
other::—

现在我们从test.acl中恢复被删除的ACL信息:
[root@anonymous mnt]#setfacl –restore test.acl
[root@anonymous mnt]#getfacl -R testdir
# file: testdir
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:group::r-x
default:group:grp1:rwx
default:mask::rwx
default:other::r-x
# file: testdir/file2
# owner: root
# group: root
user::rw-
group::r-x    #effective:r–
group:grp1:rwx   #effective:rw-
mask::rw-
other::r–


7.结束语

刚开始学习ACL的时候感觉非常的痛苦,因为对原理及概念的理解有误,造成了很大的困惑,好在从网上参考了很多资料以及下了很大功夫将其消化吸收。学习Linux唯有多练多记才能达到自己所预想的目标。


8.参考资料


http://www.bestbits.at/richacl/?cm_mc_uid=29552251466514872527098&cm_mc_sid_50200000=1489219374
http://www.cnblogs.com/dkblog/archive/2011/10/21/2219765.html

原创文章,作者:mrlapulga,如若转载,请注明出处:http://www.178linux.com/70972

(0)
上一篇 2017-03-11 16:27
下一篇 2017-03-11 18:15

相关推荐

  • 正则表达式以及Linux文本搜索工具grep

    一正则表达式 regual Expression  简称REGEXP 定义:由一类特殊字符以及文本字所编写的模式,其中有些字符不表示其字面意义,而用于统配和统配功能 分类:     基本正则表达式:BRE 贪婪模式(尽可能长地去匹配符合模式的内容     扩展表达式:E…

    Linux干货 2016-08-07
  • shell与kernel的理解

    Shell 的英文释义是外壳,与kernel 内核名词遥相呼应,一外一内,一壳一核。内核就像瑞士银行的金库,存放着客户的黄金等众多的(硬件)资产,闲杂人等(包括客户)当然是严格禁止入内的,而作为客户要存取金库中的资产则需要通过专门的特定管理人员代为操作完成,并把存取(操作)的结果呈现给客户。在Linux操作系统中,shell的职能就类似于金库的操作人员,客户…

    Linux干货 2016-02-14
  • 【26期】Linux第七周学习小总结

        今天给大家的分享是本周的一个有趣的小内容,LVM逻辑卷相关的,为什么分享这个内容呢?主要是刚没有接触或者说深入接触Linux时,有人在群里提问,如果不用虚拟机,那Centos本身有快照吗?当时说的答案很热闹,不过大部分还是倾向于没有快照,本周学习的逻辑卷管理器章节里,就这个问题给了一个准确答案,那我们一起来看一下吧!  …

    2017-08-26
  • linux网络管理

    在 /etc/udev/rules.d/路径下有个70-persistent-ipoib.rules文件,;里边有关于网卡信息的配置,,,在里边可以更改网卡的名字 注意:修改完之后要在/etc/sysconfig/network-scripts/ifcfg-eth0中修改,不然不能使用。 在Centos6以后有一个服务NetworkManager,他可以自动…

    Linux干货 2016-09-06
  • 网络基础

    网络小白一个,针对网络的基础知识无从下手,因此本文引用了鸟哥私房菜中的众多概念性知识,在此谢过,本文主要从OSI七层协议,TCP协议和一些网络中专业词汇进行解析,并通过子网掩码的划分,网络接口的配置、路由配置等实例来加深对网络的理解。 一、OSI七层协议:     OSI七层协议的由来:   由于网络链接过程…

    Linux干货 2016-09-07
  • 第三周作业

    列出当前系统上所有已经登录的用户名,同一个用户登录多次,则只显示一次 [root@app02 ~]# who |grep -o -E '^\<[a-zA-Z]+\>'|sort -u root XIAOXIN 2. 取出最后登录到当前系统的用户相关信息 [r…

    Linux干货 2016-08-29

评论列表(1条)

  • 马哥教育
    马哥教育 2017-03-20 09:09

    咱,建议命令行换个颜色,看起来听吃力的,太花了