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

相关推荐

  • Linux中查看文本工具

    一、文件查看 文件查看命令:cat,tac,rev,我们重点详解cat命令 cat命令连接文件并打印到标准输出设备上,cat经常用来显示文件的内容,类似于下的type命令 我们可以理解cat命令是复制标准输入到标准输出 cat命令选项: cat [OPTION]… [FILE]… -E:显示行结束符$   &nbs…

    Linux干货 2016-08-07
  • Linux初学笔记(markdown格式)

    Linux基础命令笔记

    Linux干货 2018-03-26
  • N22-第二周博客作业

    1、Linux系统上常见的文件管理类命令有哪些,其常用的使用方法及其示例演示。 常见的文件类管理命令:cp,rm,mv cp复制文件和目录: -f, –force 强制执行 -i 显示交互信息,默认cp 带-i选项 -r,-R 递归复制目录 -s 创建一个符号链接而不复制文件 -d 复制符号链接本身 cp aa.link qqq 相当于创建qqq…

    Linux干货 2016-08-22
  • screen和ehco命令常用选项

    screen的用法 screen 是个外部命令,用之前需要安装。      安装方式之一:yum 源 安装      yum install -y screen screen 是基于VT100/ANSI的仿真终端屏幕管理器    …

    Linux干货 2016-07-26
  • date命令总结

    在linux中man手册页,对date -d 参数的的说明比较模糊,一下的举例,进一步说明: #-d –date=STRING display time described by STRING not 'now' 命令: date -d nex-day +%Y%m%d    //明天日期 d…

    Linux干货 2016-08-05
  • CentOS 6系统启动流程

    Linux系统的组成 内核+根文件系统 内核功能:     进程管理、内存管理、文件系统管理、网络管理、安全功能、驱动程序 运行中的系统环境可分为两层:内核空间、用户空间     用户空间:应用程序(进程或线程)     内核空间:内核代码…

    Linux干货 2016-09-15

评论列表(1条)

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

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