鸟哥的linux私房菜学习笔记《二十六》ACL权限管理

来源:互联网 发布:tower windows 破解版 编辑:程序博客网 时间:2024/05/16 09:59
  1. 概念:
    ACL 是 Access Control List 的缩写,主要的目的是在提供传统的 owner,group,others 的read,write,execute 权限之外的细部权限设置。ACL 可以针对单一使用者,单一文件或目录来进行 r,w,x 的权限规范,对于需要特殊权限的使用状况非常有帮助。那 ACL 主要可以针对哪些方面来控制权限呢?他主要可以针对几个项目:
    (1)使用者 (user):可以针对使用者来设置权限;
    (2)群组 (group):针对群组为对象来设置其权限;
    (3)默认属性 (mask):还可以针对在该目录下在创建新文件/目录时,规范新数据的默认权限;
  2. 查看文件系统是否支持ACL

    [root@CentOS ~]# mount/dev/sda2 on / type ext4 (rw)[root@CentOS ~]# dumpe2fs -h /dev/sda2Default mount options:    user_xattr acl# 如果文件系统不支持ACL,可以手动:[root@CentOS ~]# mount -o remount,acl /[root@CentOS ~]# mount/dev/sda2 on / type ext4 (rw,acl)# 如果想要每次开机都生效:[root@CentOS ~]# vim /etc/fstabLABEL=/1                /                       ext4    defaults,acl    1 1 
  3. setfacl:设置某个文件/目录的ACL规定
    参数:
    -m :设置后续的 acl 参数给文件使用,不可与 -x 合用;
    -x :删除后续的 acl 参数,不可与 -m 合用;
    -b :移除“所有的” ACL 设置参数;
    -k :移除“默认的” ACL 参数,关于所谓的“默认”参数于后续范例中介绍;
    -R :递回设置 acl ,亦即包括次目录都会被设置起来;
    -d :设置“默认 acl 参数”的意思!只对目录有效,在该目录新建的数据会引用此默认值

    # 针对特定用户的方式# 设置规定:“ u:[使用者帐号列表]:[rwx] ”,例如针对 vbird1 的权限规范 rx [root@CentOS ~]# touch acl_test1[root@CentOS ~]# ll acl_test1 -rw-r--r--. 1 root root 0 Mar 18 15:27 acl_test1[root@CentOS ~]# setfacl -m u:kevin:rx acl_test1 [root@CentOS ~]# ll acl_test1 -rw-r-xr--+ 1 root root 0 Mar 18 15:27 acl_test1# 权限多了一个+[root@CentOS ~]# setfacl -m u::rwx acl_test1 [root@CentOS ~]# ll acl_test1 -rwxr-xr--+ 1 root root 0 Mar 18 15:27 acl_test1# 无用户列表,代表设置该文件所有者,所以上面显示root的权限成为rwx了
  4. getfacl:取得某个文件/目录的ACL设置项目
    参数:
    getfacl 的选项几乎与 setfacl 相同!所以鸟哥这里就免去了选项的说明

    # 列出刚才设置的权限内容[root@CentOS ~]# getfacl acl_test1 # file: acl_test1 #说明文档名而已!# owner: root     #说明此文件的拥有者,亦即 ls -l 看到的第三使用者字段# group: root     #此文件的所属群组,亦即 ls -l 看到的第四群组字段user::rwx         #使用者列表栏是空的,代表文件拥有者的权限user:kevin:r-x    #针对 vbird1 的权限设置为 rx ,与拥有者并不同!group::r--        #针对文件群组的权限设置仅有 rmask::r-x         #此文件默认的有效权限 (mask)other::r--        #其他人拥有的权限# 显示的数据前面加上 # 的,代表这个文件的默认属性,包括文件名、文件拥有者与文件所属群组。 下面出现的 user, group, mask, other 则是属于不同使用者、群组与有效权限(mask)的设置值# 针对特定用户组的方式:# 设置规范:“ g:[群组列表]:[rwx] ”,例如针对 mygroup1 的权限规范 rx[root@CentOS ~]# setfacl -m g:group1:rx acl_test1 [root@CentOS ~]# getfacl acl_test1 # file: acl_test1# owner: root# group: rootuser::rwxuser:kevin:r-xgroup::r--group:group1:r-x  #新增的部分mask::r-xother::r--# 使用者或群组所设置的权限必须要存在于 mask 的权限设置范围内才会生效,此即“有效权限 (effective permission)# 针对有效权限mask的设置方式设置规范:“ m:[rwx] ”,例如针对刚刚的文件规范为仅有 r [root@CentOS ~]# setfacl -m m:r acl_test1 [root@CentOS ~]# getfacl acl_test1 # file: acl_test1# owner: root# group: rootuser::rwxuser:kevin:r-x          #effective:r-- # kevin+mask均存在仅有r而已group::r--group:group1:r-x        #effective:r--mask::r--other::r--# kevin 与 mask 的集合发现仅有 r 存在,因此 vbird1 仅具有 r 的权限而已,并不存在 x权限!这就是 mask 的功能了!我们可以通过使用 mask 来规范最大允许的权限,就能够避免不小心开放某些权限给其他使用者或群组了。 不过,通常鸟哥都是将 mask 设置为 rwx 啦!然后再分别依据不同的用户/群组去规范她们的权限就是了。
  5. 练习
    新建用户user1,新建目录/srv/projecta,让 user1 可以进入查阅,但 myuser1不具有修改的权力

    #新建用户myuser1,新建目录/srv/projecta[root@CentOS ~]# useradd -G group1 -c "1st user" user1[root@CentOS ~]# echo "password" | passwd --stdin user1[root@CentOS ~]# mkdir /srv/projecta# 查看user1是否有权限进入目录[user1@CentOS ~]$ cd /srv/projecta/bash: cd: /srv/projecta/: Permission denied# 用root身份修改权限[root@CentOS ~]# setfacl -m u:user1:rx /srv/projecta/[root@CentOS ~]# getfacl /srv/projecta/getfacl: Removing leading '/' from absolute path names# file: srv/projecta/# owner: root# group: root# flags: -s-user::rwxuser:user1:r-x  # 查看设置结果group::rwxmask::rwxother::---# 使用user1查看权限[user1@CentOS ~]$ cd /srv/projecta/[user1@CentOS projecta]$ ll -adrwxrws---+ 2 root root 4096 Mar 18 15:00 .drwxr-xr-x. 3 root root 4096 Mar 18 15:00 ..[user1@CentOS projecta]$  touch testingtouch: cannot touch `testing': Permission denied# 确实没有写入权限

    测试ACL权限能否被子目录所继承

    # 查看是否继承[root@CentOS ~]# cd /srv/projecta/[root@CentOS projecta]# touch abc1[root@CentOS projecta]# mkdir abc2[root@CentOS projecta]# ll -d abc*-rw-r--r--. 1 root root    0 Mar 18 15:50 abc1drwxr-sr-x. 2 root root 4096 Mar 18 15:50 abc2# 权限后面没有+号,说明没有继承ACL权限。
  6. 想要继承ACL权限:
    格式:d:[u|g]:[user|group]:权限

    # 让user1在/srv/projesta下面一直具有rx的默认权限[root@CentOS ~]# setfacl -m d:u:user1:rx /srv/projecta/[root@CentOS ~]# getfacl /srv/projecta/getfacl: Removing leading '/' from absolute path names# file: srv/projecta/# owner: root# group: root# flags: -s-user::rwxuser:user1:r-xgroup::rwxmask::rwxother::---default:user::rwxdefault:user:user1:r-xdefault:group::rwxdefault:mask::rwxdefault:other::---[root@CentOS ~]# cd /srv/projecta/[root@CentOS projecta]# touch zzz1[root@CentOS projecta]# mkdir zzz2[root@CentOS projecta]# ll -d zzz*-rw-rw----+ 1 root root    0 Mar 18 15:54 zzz1drwxrws---+ 2 root root 4096 Mar 18 15:54 zzz2# 确实有继承,使用getfacl继续确认[root@CentOS projecta]# getfacl zzz2# file: zzz2# owner: root# group: root# flags: -s-user::rwxuser:user1:r-xgroup::rwxmask::rwxother::---default:user::rwxdefault:user:user1:r-xdefault:group::rwxdefault:mask::rwxdefault:other::---
  7. 想要ACL属性全部消失

    [root@CentOS projecta]# getfacl /srv/projecta/getfacl: Removing leading '/' from absolute path names# file: srv/projecta/# owner: root# group: root# flags: -s-user::rwxuser:user1:r-xgroup::rwxmask::rwxother::---default:user::rwxdefault:user:user1:r-xdefault:group::rwxdefault:mask::rwxdefault:other::---[root@CentOS projecta]# setfacl -b[root@CentOS projecta]# getfacl /srv/projecta/getfacl: Removing leading '/' from absolute path names# file: srv/projecta/# owner: root# group: root# flags: -s-user::rwxgroup::rwxother::---
0 0
原创粉丝点击