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)
mrlapulgamrlapulga
上一篇 2017-03-11
下一篇 2017-03-11

相关推荐

  • 计算机简介

    计算机的简介 电子计算机(英语:computer),亦称电脑,是一种利用「电子学」原理,根据一系列指令对数据进行处理的工具计算机种类繁多,但实际来看,计算机总体上是处理信息的工具。计算机在组成上形式不一,早期计算机的体积足有一间房屋的大小,而今天某些嵌入式计算机可能比一副「扑克牌」还小。当然,即使在今天依然有大量体积庞大的巨型计算机为特别的[科学]计算或面向…

    Linux干货 2016-10-28
  • 使用mysql-mmm实现高可用mysql读写分离

    MMM介绍:  MMM全称为Multi-Master Replication Manager for MySQL,即为主主复制管理器;根据MMM官网介绍,其工作原理类似于lvs,都是利用vip地址;但lvs只有一个组件便可以正常工作,而MMM则使用三个组件,分别是mysql-mmm、mysql-mmm-agent、mysql-mmm-monitor…

    Linux干货 2015-08-04
  • grep用法

    一、前言 grep是功能强大的文本处理工具,全称:global search regular expression and print out the line,grep一行一行使用正则表达式匹配文本,之后对匹配到的文本整行显示(除非使用特定的选项取反,如 grep -v)。 二、grep用法 grep命令用法:    &nbs…

    Linux干货 2015-08-24
  • grep,find

    显示当前系统上root、fedora、user用户的默认shell: 2.找出/etc/rc.d/init.d/functions文中单词后跟一组小括号的行 3.使用echo命令输出一个绝对路径,使用grep取出其基名: 4.找出ifconfig命令结果中的1-255之间的数字 5.写一个模式,匹配合理的ip地址 6.写一个模式,匹配出邮箱地址 7.查找/v…

    Linux干货 2016-11-27
  • linux系统故障排除总结

    常见的系统故障 1.确定文体的故障特征 2.重现故障 3.使用工具收集进一步信息,确定根源在何处 4.排除不可能的原因 5.定位故障:             从简单的问题入手      &…

    Linux干货 2016-09-15
  • N25-第9周博客作业

    1、写一个脚本,判断当前系统上所有用户的shell是否为可登录shell(即用户的shell不是/sbin/nologin);分别这两类用户的个数;通过字符串比较来实现; #!/bin/bash # 统计登录用户和非登陆用户的个数 # author: han declare -i loginnum=0 declare -i nologinnum=0 whil…

    Linux干货 2017-03-10

评论列表(1条)

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

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