• Linux的ACL权限


    什么是 ACL 权限?

    在普通权限中,用户对文件只有三种身份,就是属主、属组和其他人;每种用户身份拥有读(read)、写(write)和执行(execute)三种权限。但是在实际工作中,这三种身份实在是不够用,我们举个例子来看看。

     

    图 1 的根目录中有一个 /project 目录,这是班级的项目目录。班级中的每个学员都可以访问和修改这个目录,老师也需要对这个目录拥有访问和修改权限,其他班级的学员当然不能访问这个目录。需要怎么规划这个目录的权限呢?应该这样:老师使用 root 用户,作为这个目录的属主,权限为 rwx;班级所有的学员都加入 tgroup 组,使 tgroup 组作为 /project 目录的属组,权限是 rwx;其他人的权限设定为 0。这样这个目录的权限就可以符合我们的项目开发要求了。

    有一天,班里来了一位试听的学员 st,她必须能够访问 /project 目录,所以必须对这个目录拥有 r 和 x 权限;但是她又没有学习过以前的课程,所以不能赋予她 w 权限,怕她改错了目录中的内容,所以学员 st 的权限就是 r-x。可是如何分配她的身份呢?变为属主?当然不行,要不 root 该放哪里?加入 tgroup 组?也不行,因为 tgroup 组的权限是 rwx,而我们要求学员 st 的权限是 r-x。如果把其他人的权限改为 r-x 呢?这样一来,其他班级的所有学员都可以访问 /project 目录了。

    当出现这种情况时,普通权限中的三种身份就不够用了。ACL 权限就是为了解决这个问题的。在使用 ACL 权限给用户 st 陚予权限时,st 既不是 /project 目录的属主,也不是属组,仅仅赋予用户 st 针对此目录的 r-x 权限。这有些类似于 Windows 系统中分配权限的方式,单独指定用户并单独分配权限,这样就解决了用户身份不足的问题。

    ACL是Access Control List(访问控制列表)的缩写,不过在Linux系统中,ACL用于设定用户针对文件的权限,而不是在交换路由器中用来控制数据访问的功能(类似于防火墙)

    开启 ACL

    dumpe2fs 命令是查询指定分区详细文件系统信息的命令

    [root@iZbp143t3oxhfc3ar7jey0Z ~]# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    devtmpfs        1.9G     0  1.9G   0% /dev
    tmpfs           1.9G     0  1.9G   0% /dev/shm
    tmpfs           1.9G  580K  1.9G   1% /run
    tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
    /dev/vda1        40G  6.6G   31G  18% /
    tmpfs           379M     0  379M   0% /run/user/0
    [root@iZbp143t3oxhfc3ar7jey0Z ~]# dumpe2fs -h /dev/vda1
    dumpe2fs 1.42.9 (28-Dec-2013)
    Filesystem volume name:   <none>
    Last mounted on:          /
    Filesystem UUID:          1114fe9e-2309-4580-b183-d778e6d97397
    Filesystem magic number:  0xEF53
    Filesystem revision #:    1 (dynamic)
    Filesystem features:      has_journal ext_attr resize_inode dir_index filetype n                                                                                        eeds_recovery extent flex_bg sparse_super large_file huge_file uninit_bg dir_nli                                                                                        nk extra_isize
    Filesystem flags:         signed_directory_hash
    Default mount options:    user_xattr acl
    Filesystem state:         clean
    Errors behavior:          Continue
    Filesystem OS type:       Linux
    Inode count:              2621440
    Block count:              10484164

    如上所示,已经开启了ACL权限,假如没有开启的话,可以如下所示

    [root@localhost ~]# mount -o remount,acl /
    #重新挂载根分区,并挂载加入 acl 权限

    如果想永久开启的话,可以通过修改/etc/fstab 文件,永久开启 ACL 权限:

    [root@localhost ~]# vi /etc/fstab
    UUID=c2ca6f57-b15c-43ea-bca0-f239083d8bd2 / ext4 defaults ,acl 1 1
    #加入 acl
    [root@localhost ~]# mount -o remount /
    #重新挂载文件系统或重启动系统,使修改生效

    ACL演示

     常用的命令如下:

    setfacl -m u:用户名:权限 文件名
    setfacl -m g:组名:权限 文件名
    setfacl -m u:aa:rwx /test 给 test 目录赋予 aa 是读写执行的 ACL 权限
    setfacl -m u:cc:rx -R soft/ 赋予递归 ACL 权限,只能赋予目录 -R 递归
    setfacl -m d:u:aa:rwx -R /test ACL 默认权限。 注意:默认权限只能赋予目录
    注意:如果给目录赋予 acl 权限,两条命令都要输入
    递归与默认的区别:
    setfacl -m u:cc:rx -R soft/ 只对已经存在的文件生效
    setfacl -m d:u:aa:rwx -R /test 只对以后新建的文件生效

    演示如下:

    [root@iZbp143t3oxhfc3ar7jey0Z /]# chmod 750 /www
    [root@iZbp143t3oxhfc3ar7jey0Z /]# useradd st
    [root@iZbp143t3oxhfc3ar7jey0Z /]# passwd st
    Changing password for user st.
    New password:
    BAD PASSWORD: The password is shorter than 8 characters
    Retype new password:
    passwd: all authentication tokens updated successfully.
    [root@iZbp143t3oxhfc3ar7jey0Z /]# setf
    setfacl   setfiles  setfont
    [root@iZbp143t3oxhfc3ar7jey0Z /]# setfacl -m u:st:5 /www/
    [root@iZbp143t3oxhfc3ar7jey0Z /]# ll -d /www/
    drwxr-x---+ 2 root root 4096 Mar 25 20:14 /www/
    [root@iZbp143t3oxhfc3ar7jey0Z /]# getfacl /www/
    getfacl: Removing leading '/' from absolute path names
    # file: www/
    # owner: root
    # group: root
    user::rwx
    user:st:r-x
    group::r-x
    mask::r-x
    other::---
    [root@iZbp143t3oxhfc3ar7jey0Z /]# setfacl -m u:st:5 -R /www/
    [root@iZbp143t3oxhfc3ar7jey0Z www]# ll
    total 0
    -rw-r-xr--+ 1 root root 0 Mar 25 20:41 abc
    -rw-r-xr--+ 1 root root 0 Mar 25 20:41 bcd
    [root@iZbp143t3oxhfc3ar7jey0Z www]# getfacl abc
    # file: abc
    # owner: root
    # group: root
    user::rw-
    user:st:r-x
    group::r--
    mask::r-x
    other::r--
    
    [root@iZbp143t3oxhfc3ar7jey0Z www]# setfacl -m d:u:st:5 -R /www/
    [root@iZbp143t3oxhfc3ar7jey0Z www]# getfacl /www/
    getfacl: Removing leading '/' from absolute path names
    # file: www/
    # owner: root
    # group: root
    user::rwx
    user:st:r-x
    group::r-x
    mask::r-x
    other::---
    default:user::rwx
    default:user:st:r-x
    default:group::r-x
    default:mask::r-x
    default:other::---
    
    [root@iZbp143t3oxhfc3ar7jey0Z www]# touch qaz
    [root@iZbp143t3oxhfc3ar7jey0Z www]# ll
    total 0
    -rw-r-xr--+ 1 root root 0 Mar 25 20:41 abc
    -rw-r-xr--+ 1 root root 0 Mar 25 20:41 bcd
    -rw-r-----+ 1 root root 0 Mar 25 20:46 qaz
    [root@iZbp143t3oxhfc3ar7jey0Z www]# su - st
    [st@iZbp143t3oxhfc3ar7jey0Z ~]$ cd /www/
    [st@iZbp143t3oxhfc3ar7jey0Z www]$ ll
    total 0
    -rw-r-xr--+ 1 root root 0 Mar 25 20:41 abc
    -rw-r-xr--+ 1 root root 0 Mar 25 20:41 bcd
    -rw-r-----+ 1 root root 0 Mar 25 20:46 qaz
    [st@iZbp143t3oxhfc3ar7jey0Z www]$ touch wer
    touch: cannot touch ‘wer’: Permission denied
    [st@iZbp143t3oxhfc3ar7jey0Z www]$ exit
    logout
    [root@iZbp143t3oxhfc3ar7jey0Z www]#

    最大有效权限 mask

     它的权限是和user:st:r-x进行与操作,只有共同有的,才会有权限,一般默认mask权限都是rwx,与我们所设定的权限相与就是我们设定的权限。

    删除 ACL 权限

    [root@iZbp143t3oxhfc3ar7jey0Z www]# setfacl -x u:st /www/  #删除指定用户和用户组的 ACL 权限
    [root@iZbp143t3oxhfc3ar7jey0Z www]# getfacl /www/
    getfacl: Removing leading '/' from absolute path names
    # file: www/
    # owner: root
    # group: root
    user::rwx
    group::r-x
    mask::r-x
    other::---
    default:user::rwx
    default:user:st:r-x
    default:group::r-x
    default:mask::r-x
    default:other::---
     
    [root@iZbp143t3oxhfc3ar7jey0Z www]# setfacl -b /www/  #会删除文件的所有的 ACL 权限
    [root@iZbp143t3oxhfc3ar7jey0Z www]#

    注:其实ACL当递归赋予权限的时候,就会有权限溢出的问题,因为目录要x权限,才能cd,这样里面的文件就有x权限,但是就有了可执行的权限了。

  • 相关阅读:
    类中以双下划线開始的方法
    Dynamics CRM2016 新功能之从CRM APP中导出数据至EXCEL
    敏捷项目管理实践
    Hibernate环境搭建
    ubuntu12.04更新软件源时出现校验和不符
    修炼你自己
    http自己定义超时检測方法、主动抛出异常
    sql server 2008安装图解
    Ural 1353 Milliard Vasya&#39;s Function(DP)
    待字闺中之构造最大数分析
  • 原文地址:https://www.cnblogs.com/dalianpai/p/12570717.html
Copyright © 2020-2023  润新知