zoukankan      html  css  js  c++  java
  • Centos下ACL(访问控制列表)介绍(转)

    我们知道,在Linux操作系统中,传统的权限管理分是以三种身份(属主、属組以及其它人)搭配三种权限(可读、可写以及可执行),并且搭配三种特殊权限(SUID,SGID,SBIT),来实现对系统的安全保护。但是,随着业务和需求的发展和扩大,仅有的这种模式已经不能满足当前复杂环境下的权限控制需求。

    比如,当前有一个/data目录,现在需要A組成员能够可写,B組成员仅读,C組成员可读可写可执行,此时怎么办呢?

    对于以上的需求,仅仅依托现有的传统权限管理模式,是无法实现的。为了解决该类型的问题,Linux 开发出了一套新的文件系统权限管理方法,叫做 文件访问控制列表 ACL(Access Control Lists)。通过使用 ACL,可以完美解决如上类型的需求问题。

    那么下来来看,什么是访问控制列表?

    什么是ACL

    ACL 是 Access Control List 的缩写,主要目的是针对在传统的三种身份和三种权限之外,提供更加细化的局部权限设定。官方手册来讲,它主要针对用户、用户组、以及掩码方面控制权限。
    简单去理解就是,ACL 可以针对单个用户、单个用户组来进行权限细化的控制。而在windows系统上,没有这个ACL,ACL是类Unix(Unix-like)操作系统权限的额外支持项目,因此要使用ACL必须要有文件系统的支持才行。主要包括ReiserFS, EXT2/EXT3/ext4, JFS, XFS等文件系统。

    文件系统是否支持ACL

    需要注意的是,由于ACL是必须依托文件系统的,因此并不是每个文件系统都支持ACL。比如我们 win 平台的 NTFS 文件系统,FAT32 文件系统是不支持ACL的。在Linux平台上,常见的支持ACL的文件系统有如下几类,如 EXT2/EXT3/ext4, JFS, XFS等等。

    那么,如何查看你的系统是否支持 ACL 呢?

    我们可以通过如下操作来查看:

    [root@lh ~]# tune2fs -l /dev/vda1 | grep options
    Default mount options:    user_xattr acl
    [root@lh ~]# dumpe2fs /dev/vda1 | grep options
    dumpe2fs 1.41.12 (17-May-2010)
    Default mount options:    user_xattr acl

    以上两条命令任选一个即可!

    如果在输出的信息,默认挂载选项中有acl这个标识,就代表你的文件系统是支持的。

    假设,你的文件系统不支持或者支持但是并没有显示这个acl标识怎么办呢?针对此种情况,我们可以通过使用tune2fs来为他添加,或者mount去添加都可以。

    [root@lh ~]# tune2fs -o acl /dev/vda1
    tune2fs 1.41.12 (17-May-2010)

    ACL相关命令详解

    介绍完了 ACL 是什么,也说了如何使文件系统支持 ACL 的功能,下面就来说说如何操作。

    ACL的相关的操作主要有 3 个命令,分别是 getface、setfacl和chacl,常用的主要是getfacl 和 setfacl。

    getfacl    查看文件/目录的ACL设定内容
    setfacl    设置文件/目录的ACL内容
    chacl      查看和更改文件/目录的ACL内容,由于日常有setfacl,因此chacl从来不用,故本文不作介绍

    getfacl 一般都是直接在后面跟你所要查看的文件或者目录的路径,因此掌握如何查看即可。操作如下:

    [root@lh ~]# getfacl /tmp
    getfacl: Removing leading ‘/’ from absolute path names
    # file: tmp
    # owner: root
    # group: root
    # flags: –t
    user::rwx
    group::rwx
    other::rwx
    [root@lh ~]# getfacl /etc/passwd
    getfacl: Removing leading ‘/’ from absolute path names
    # file: etc/passwd
    # owner: root
    # group: root
    user::rw-
    group::r–
    other::r–

    setfacl 是使用最多的,基本 ACL 方面的操作都是它,因此它的选项也是蛮多的。首先来setfacl的使用语法:

    setfacl [-bkRd] [{-m|-x} acl参数] 文件/目录路径
    选项介绍:
    -b :删除所有的 acl 参数
    -k :删除预设的 acl 参数
    -R :递归设置后面的 acl 参数
    -d :设置预设的 acl 参数(只对目录有效,在该目录新建的文件也会使用此ACL默认值)
    -m :设置(修改)后面 acl 参数(添加acl条目)
    -x :删除后面指定的 acl 参数

    ACL 参数主要由3部分组成,组成结构如下:

    三种身份:对应身份名:三种权限
    [u|g|o]:[用户名|用户组名]:[rwx]

    实例练习:

    下面来看几个实例,来理解学习 ACL 的操作:

    现在在/mnt目录,有文件test和目录dir,它们的权限都是600,属主和属組都是root。

    [root@lh mnt]# touch test
    [root@lh mnt]# mkdir dir
    [root@lh mnt]# chmod 600 test
    [root@lh mnt]# chmod 600 dir
    [root@lh mnt]# ll
    total 4
    drw——-. 2 root root 4096 Jul  4 17:56 dir
    -rw——-. 1 root root    0 Jul  4 17:56 test

    现在要求完成如下要求:

    1、为文件 test 增加 acl 权限,使 sunsky 用户可以可读可写

    [root@lh mnt]# setfacl -m u:sunsky:rw test
    [root@lh mnt]# getfacl test
    # file: test
    # owner: root
    # group: root
    user::rw-
    user:sunsky:rw-
    group::—
    mask::rw-
    other::—
    [root@lh mnt]# su – sunsky  # 切换到sunsky用户下,进行测试
    Wellcome to Linux World
    [sunsky@lh ~]$ echo 1 >> /mnt/test  # 很明显能写入数据
    [sunsky@lh ~]$ cat /mnt/test

    2、为文件 test 增加 acl 权限,使 sun 組的所有用户都能读该文件

    [root@lh mnt]# setfacl -m g:sun:r test
    [root@lh mnt]# getfacl test
    # file: test
    # owner: root
    # group: root
    user::rw-
    user:sunsky:rw-
    group::—
    group:sun:r–
    mask::rw-
    other::—
     
    [root@lh mnt]# su – sun
    Wellcome to Linux World
    [sun@lh ~]$ cat /mnt/test # 很明显能查看test文件的内容
    1
    [sun@lh ~]$ echo 2 >> /mnt/test  # 由于我们没有给sun組成员更改权限,因此不能更改
    -bash: /mnt/test: Permission denied

    3、为目录 dir 增加 acl 权限,使 sun 組的所有用户都能够对该目录可读可写可执行

    [root@lh mnt]# setfacl -m g:sun:rwx dir
    [root@lh mnt]# getfacl dir
    # file: dir
    # owner: root
    # group: root
    user::rw-
    group::—
    group:sun:rwx
    mask::rwx
    other::—
     
    [root@lh mnt]# su – sun  # 切换到sun用户下,进行测试
    [sun@lh ~]$ echo “date” >> /mnt/dir/date.sh
    [sun@lh ~]$ bash /mnt/dir/date.sh
    Fri Jul  4 18:01:48 CST 2014

    4、删除文件 test 上,关于 sun 組的 acl 权限

    [root@lh mnt]# setfacl -x g:sun test
    [root@lh mnt]# getfacl test
    # file: test
    # owner: root
    # group: root
    user::rw-
    user:sunsky:rw-
    group::—
    mask::rw-
    other::—

    5、删除目录 dir 的所有 ACL 权限

    [root@lh mnt]# setfacl -b dir
    [root@lh mnt]# getfacl dir
    # file: dir
    # owner: root
    # group: root
    user::rw-
    group::—
    other::—

    6、为目录 dir 增加默认ACL权限,使 dir 目录下新创建的文件或目录,都默认拥有 sunsky 用户可读可写可执行

    [root@lh mnt]# setfacl -m d:u:sunsky:rwx dir
    [root@lh mnt]# getfacl dir
    # file: dir
    # owner: root
    # group: root
    user::rw-
    group::—
    other::—
    default:user::rw-
    default:user:sunsky:rwx
    default:group::—
    default:mask::rwx
    default:other::—
    [root@lh mnt]# touch /mnt/dir/sunsky
    [root@lh mnt]# getfacl /mnt/dir/sunsky
    getfacl: Removing leading ‘/’ from absolute path names
    # file: mnt/dir/sunsky
    # owner: root
    # group: root
    user::rw-
    user:sunsky:rwx                 #effective:rw-
    group::—
    mask::rw-
    other::—

    在第六题中,我们发现,在user:sunsky:rwx后面多了一个 #effective:rw-,这是为什么呢?我们在切换到sunsky用户下,看看它是否有执行该文件的权限!

    [root@lh mnt]# su – sunsky
    Wellcome to Linux World
    [sunsky@lh ~]$ bash /mnt/dir/sunsky
    bash: /mnt/dir/sunsky: Permission denied

    很明显,尽管我们使用setfacl给了sunsky对dir目录下默认新生成的文件可读可写可执行的权限,但是依旧是没有执行权限的。这是为什么呢?

    我们发现多了输出#effective:rw-,它是由于什么出来的呢?

    effective生效的为rw,他是受我们输出中的mask影响的。但是我们发现,我们并没有设置过mask啊,为啥他默认变成rw了。这里我就来介绍一下mask!

     有效权限(mask)就是acl权限设置的极限值,也就是你所设置的acl权限一定是mask的一个子集,如果超出mask范围会将超出的权限去掉,设置格式:m:权限 
    如果遇到设置的用户权限与 mask 权限冲突,则用户的权限为# effective 权限

    那么,一旦一个文件被设置了 ACL,其文件原属组部分的权限将变为 MASK 权限,而并非原来的属组权限。如果其文件原先属組权限为空,那么当你设置了mask权限之后,你的属組权限也相应改变为其mask对应的权限。

          下面,我们就继续进行第六题的实验!

    [root@lh mnt]# setfacl -m m::rwx /mnt/dir/
    [root@lh mnt]# getfacl /mnt/dir/
    getfacl: Removing leading ‘/’ from absolute path names
    # file: mnt/dir/# owner: root
    # group: root
    user::rw-
    group::—
    mask::rwx
    other::—
    default:user::rw-
    default:user:sunsky:rwx
    default:group::—
    default:mask::rwx
    default:other::—
     
    [root@lh mnt]# su – sunsky
    Wellcome to Linux World
    [sunsky@lh ~]$ bash /mnt/dir/sunsky
    bash: /mnt/dir/sunsky: Permission denied

    奇怪了,为什么我已经更改了mask为rwx了,并且effective也不再出现了,为何现在sunsky依旧拿不到执行权限呢?

    我们现在去看下/mnt/dir/sunsky文件的ACL权限吧。

    [root@lh mnt]# getfacl /mnt/dir/sunsky
    getfacl: Removing leading ‘/’ from absolute path names
    # file: mnt/dir/sunsky
    # owner: root
    # group: root
    user::rw-
    user:sunsky:rwx                 #effective:rw-
    group::—
    mask::rw-
    other::—

    通过查看我们发现,/mnt/dir/sunsky文件中,对于 sunsky的acl权限设置竟然还是 # effective:rw- ,这是为什么呢?

    原来,我们刚才修改 /mnt/dir 的 mask 仅仅只针对/mnt/dir目录下新生成的文件有效,并且由于文件在创建时,受到传统权限的 umask 值的影响,已经拥有了属組的权限,所以就使得 ACL 的mask设置失效。因此,此时我们通过使用上面提到的-R 递归选项,将/mnt/dir目录下的所有文件重新修改一次 ACL 的mask权限,就能解决该问题!

    [root@lh mnt]# su – sunsky
    Wellcome to Linux World
    [sunsky@lh ~]$ echo date > /mnt/dir/sunsky
    [sunsky@lh ~]$ bash /mnt/dir/sunsky
    Fri Jul  4 18:32:11 CST 2014

    以上就是 setfacl 的日常管理的所有操作了!相信只要大家将以上的操作掌握,以后只要用到ACL的地方就不会窘迫了。

  • 相关阅读:
    使用JS模拟锚点跳转
    js如何获取url参数
    k64 datasheet学习笔记52---Universal Asynchronous Receiver/Transmitter (UART)
    k64 datasheet学习笔记11---Port Control and Interrupts (PORT)
    k64 datasheet学习笔记10---Signal Multiplexing and Signal Descriptions
    k64 datasheet学习笔记50---GPIO
    k64 datasheet学习笔记4---Clock distribution
    k64 datasheet学习笔记3---Chip Configuration之Human machine interfaces
    k64 datasheet学习笔记3---Chip Configuration之Communication interfaces
    k64 datasheet学习笔记3---Chip Configuration之Times
  • 原文地址:https://www.cnblogs.com/zhangmingcheng/p/6255523.html
Copyright © 2011-2022 走看看