zoukankan      html  css  js  c++  java
  • Linux中权限管理之ACL权限

    1.简介:
      a.作用:
        是为了防止权限不够用的情况,一般的权限有所有者、所属组、其他人这三种,当这三种满足不了我们的需求的时候就可以使用ACL权限
      
      b.故事背景:
        一个老师,给一个班的学员上课,他在linux的根目录下面建立了一个文件夹,只允许本班级的学员对该目录进行读写执行操作,其他人都不行,这时该目录的权限一般是770(一般我们设置权限都是所有者的权限大于所属组的权限,所属组的权限大于其他人的权限,依次往下),此时有个同学想试听我们的课程,他们只有读和执行的权限,没有写的权限,那么此时该怎么分配权限呢?此时的话所属组、所有者、其他人都不满足,这是就用到ACL权限

    2.使用条件
      a.只需硬盘开启对ACL的支持即可,现在Linux上都是默认开启的
      b.查看是否支持acl权限
        1.查看自己linux上的硬盘信息
          df -h
            Filesystem Size Used Avail Use% Mounted on
            /dev/mapper/VolGroup-lv_root 6.5G 1.6G 4.7G 26% /
            tmpfs 246M 0 246M 0% /dev/shm
            /dev/sda1 477M 28M 425M 7% /boot
        2.根据硬盘信息,查询是否开启了acl
          dumpe2fs -h /dev/sda1 | grep "Default mount options"
            dumpe2fs 1.41.12 (17-May-2010)
            Default mount options: user_xattr acl
        3.如果没有开启
          临时开启:
            重新挂载跟分区,并加入acl权限
            mount -o remount,acl /
          永久开始:
            编辑开机挂载文件/etc/fstab
            将文件中的:
            UUID=546c4e25-7b06-468b-89a1-e66280c4ee98 /boot ext4 defaults 1 2
            修改为:
            UUID=546c4e25-7b06-468b-89a1-e66280c4ee98 /boot ext4 defaults,acl 1 2
            重新挂载根分区:
            mount -o remount /

    3.设置ACL权限
      setfacl 选项 文件名
        -m 设定ACL权限
        -x 删除指定的ACL权限
        -b 删除所有的ACL权限
        -d 设定默认ACL权限
        -k 删除默认ACL权限
        -R 递归设定ACL权限

      设置用户权限:
        setfacl -m u:用户名:权限 文件

      设置组权限:
        setfacl -m g:组名称:权限 文件

      设置最大有效权限:
        setfacl -m m:权限 文件
        说明:
          acl权限中有个"mask"的选项,它就是ACL权限的最大权限,现在是rwx,当你设置某个用户或组的ACL权限时,要跟mask的权限“相与”之后产生的权限才是该用户的最终权限,也就是加入mask的最大权限是rx,但是你给st用户设置的是rwx权限,此时st用户它的权限只有rx的权限,因为与最大权限“相与”得出的结果就是rx

    4.删除权限
      删除用户权限:
        setfacl -x u:用户名 文件名
      删除组权限:
        setfacl -x g:组名称 文件名
      删除整个权限:
        setfacl -b 文件名

    5.设置默认权限和递归权限
      递归权限:
        setfacl -m u:用户名:权限 -R 文件
        说明:
          递归只对该目录下面现有的子文件或目录有用,对于该目录下面新添加的子文件或目录没用
      默认权限:
        setfacl -m d:u:用户名:权限 文件
        说明:
          默认权限只对该目录下面新建的文件或目录有效,对已经存在的子文件无效

    试验:
    准备环境:
      [root@localhost temp]# useradd student1
      [root@localhost temp]# useradd student2
      [root@localhost temp]# useradd visitor1
      [root@localhost temp]# passwd student1
      [root@localhost temp]# passwd student2
      [root@localhost temp]# passwd visitor1
      [root@localhost temp]# groupadd student_group
      [root@localhost temp]# groupadd other_student
      [root@localhost temp]# mkdir project
      [root@localhost temp]# chown root:student_group project/
      [root@localhost temp]# chmod 770 project/
      [root@localhost temp]# gpasswd -a student1 student_group
      [root@localhost temp]# gpasswd -a student2 student_group

      student1有权限:
      [student1@localhost ~]$ cd /xiaol/temp/project/
      [student1@localhost project]$ touch a.txt

      visitor1没有权限
      [visitor1@localhost ~]$ cd /xiaol/temp/project/
      -bash: cd: /xiaol/temp/project/: 权限不够


    给visitor1设置权限:
      [root@localhost temp]# setfacl -m u:visitor1:rx project/

      查看权限:
      [root@localhost temp]# getfacl project/
      # file: project/  
      # owner: root
      # group: student_group
      user::rwx
      user:visitor1:r-x
      group::rwx
      mask::rwx
      other::---

      visitor1有权限了
      [visitor1@localhost ~]$ cd /xiaol/temp/project/
      [visitor1@localhost project]$

    给other_student组设置权限:
      [root@localhost temp]# setfacl -m g:other_student:rx project/

      查看权限:
      [root@localhost temp]# getfacl project/
      # file: project/
      # owner: root
      # group: student_group
      user::rwx
      user:visitor1:r-x
      group::rwx
      group:other_student:r-x
      mask::rwx
      other::---

    设置最大权限:
      [root@localhost temp]# setfacl -m m:rx project/

      查看权限
      [root@localhost temp]# getfacl project/
      # file: project/
      # owner: root
      # group: student_group
      user::rwx
      user:visitor1:r-x
      group::rwx #effective:r-x
      group:other_student:r-x
      mask::r-x
      other::---

    删除权限:
      删除visitor1权限
      [root@localhost temp]# setfacl -x u:visitor1 project/

      删除other_student权限
      [root@localhost temp]# setfacl -x g:other_student project/

      查看权限:
      [root@localhost temp]# getfacl project/
      # file: project/
      # owner: root
      # group: student_group
      user::rwx
      group::rwx
      mask::rwx
      other::---

    设置递归权限
      [root@localhost temp]# setfacl -m u:visitor1:rx -R project/

      进入project查看内部文件权限
      [root@localhost project]$ getfacl a.txt
      # file: a.txt
      # owner: student1
      # group: student1
      user::rw-
      user:visitor1:r-x
      group::rw-
      mask::rwx
      other::r--

      新建文件并查看权限
      [root@localhost project]# touch b.txt
      [root@localhost project]# getfacl b.txt
      # file: b.txt
      # owner: root
      # group: root
      user::rw-
      group::r--
      other::r--

    设置默认权限
      [root@localhost temp]# setfacl -m d:u:visitor1:rx project/

      新建文件并查看权限
      [root@localhost project]# touch c.txt
      [root@localhost project]# getfacl c.txt
      # file: c.txt
      # owner: root
      # group: root
      user::rw-
      user:visitor1:r-x #effective:r--
      group::rwx #effective:rw-
      mask::rw-
      other::---

    清理试验环境
      [root@localhost temp]# userdel -r student1
      [root@localhost temp]# userdel -r student2
      [root@localhost temp]# userdel -r visitor1
      [root@localhost temp]# groupdel student_group
      [root@localhost temp]# groupdel other_student
      [root@localhost temp]# rm -rf project/

  • 相关阅读:
    AFNetworking 3.0迁移指南
    富文本常用封装(NSAttributedString浅析)
    如何在 Objective-C 的环境下实现 defer
    iOS之深入了解控制器View的加载
    10+年程序员总结的20+条经验教训
    Foundation: NSNotificationCenter
    做一款仿映客的直播App?看我就够了
    AFNetworking源码分析
    WWDC2016 Session笔记 – Xcode 8 Auto Layout新特性
    iOS页面间传值的一些方式总结
  • 原文地址:https://www.cnblogs.com/413xiaol/p/7074780.html
Copyright © 2011-2022 走看看