8.3-ACL权限详解(命令篇)

前言

        我们都知道Linux有三种身份(owner,group,other)搭配三种权限(r,w,x)以及三种特殊权限(SUID,SGID,SBIT),

但是某些时候这些组合不能满足复杂的权限需求。


例如

      目录data的权限为:drwxr-x—,所有者与所属组均为root,在不改变所有者和所属组的前提下,要求用户yufei对该

目录有完全访问权限(rwx),但又不能让其他有用完全权限(rwx)。这个要求看似不能实现,这就看出来传统的权限

管理设置有时候也会力不从心。为了解决这样的问题,Linux开发出了一套新的文件系统权限管理方法,叫文件访问

控制列表ACL(Access Control Lists)。这时候,我们就可能通过ACL来实现。


什么是ACL

        ACL是Access Control List的缩写,主要的目的是在提供传统的owner,group,others的read,write,execute权限之外的局

部权限设定。ACL可以针对单个用户,单个文件或目录来进行r,w,x的权限设定,特别适用于需要特殊权限的使用情况。

简单来说,ACL就是可以设定特定用户或用户组对于一个文件或目录的操作权限



1.查看当前分区是否支持ACL权限

[root@localhost ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        19G  3.6G   14G  21% /

[root@localhost ~]# dumpe2fs -l /dev/sda1
...
Default mount options:    user_xattr acl

若没有acl选项,则用以下命令挂载
mount -o remount,acl /dev/sda1

2.查看acl权限

        getfacl file

[root@localhost ~]# getfacl /root/testdir/a 
getfacl: Removing leading '/' from absolute path names
# file: root/testdir/a
# owner: root
# group: root
user::rw-
group::r--
other::r--

3.设定facl权限

setfacl 选项 类型:用户:权限 file
        -m 设定acl权限
        -x 删除指定的acl权限
        -b 删除所有的acl权限
        -d 设定默认acl权限
        -k 删除默认acl权限
        -R 递归设定acl
eg:    setfacl -m u:tom:rw /testdir            #添加tom用户对/testdir有读写权限 
       setfacl -m g:alice:rw /testdir          #添加alice组对/testdir有读写权限

4.删除acl权限
    setfacl -x u:user 文件名    :删除指定的acl权限
    setfacl -b 文件名               :删除文件所有acl权限

[root@localhost ~]# setfacl -x u:tom /testdir/
[root@localhost ~]# setfacl -b /testdir/
[root@localhost ~]# getfacl /testdir/
getfacl: Removing leading '/' from absolute path names
# file: testdir/
# owner: root
# group: root
user::rwx
group::r-x
other::r-x

5.修改mask值

    setfacl -m m:rw /testdir

[root@localhost ~]# setfacl -m mask:w /root/acl/
[root@localhost ~]# getfacl /root/acl/
getfacl: Removing leading '/' from absolute path names
# file: root/acl/
# owner: root
# group: root
user::rwx
user:tom:rw-   #effective:-w-
group::r-x   #effective:---
mask::-w-
other::r-x

 

6.递归acl:针对目录下已存在的文件
      setfacl -R -m u:用户名:权限 目录名

[root@localhost ~]# setfacl -R -m u:tom:rw acl/
[root@localhost ~]# getfacl acl/
# file: acl/
# owner: root
# group: root
user::rwx
user:tom:rw-
group::r-x
mask::rwx
other::r-x
[root@localhost ~]# getfacl acl/a 
# file: acl/a
# owner: root
# group: root
user::rw-
user:tom:rw-
group::r--
mask::rw-
other::r--

 

7.默认acl:针对加入到目录的新文件
      setfacl -m d:u:用户名:权限  目录名

新文件没有acl权限
[root@localhost ~]# touch /root/acl/test.txt
[root@localhost ~]# getfacl /root/acl/test.txt
getfacl: Removing leading '/' from absolute path names
# file: root/acl/test.txt
# owner: root
# group: root
user::rw-
group::r--
other::r--
对目录使用默认acl权限
[root@localhost ~]# setfacl -m d:u:tom:rw /root/acl/
[root@localhost ~]# touch /root/acl/test2.txt
[root@localhost ~]# getfacl /root/acl/test2.txt
getfacl: Removing leading '/' from absolute path names
# file: root/acl/test2.txt
# owner: root
# group: root
user::rw-
user:tom:rw-
group::r-x   #effective:r--
mask::rw-
other::r--

 备份和恢复ACL
                    主要的文件操作命令cp和mv都支持ACL,只是cp命令需要加上-p 参数。但是tar等常见的备份工具是不会保留目录
                    和文件的ACL信息             

#getfacl -R /tmp/dir1 > acl.txt
#setfacl -R -b /tmp/dir1
#setfacl -R --set-file=acl.txt /tmp/dir1
#getfacl -R /tmp/dir1

 附:用户对于文件的最终权限要和mask值“相与”才是最后所得权限,“文件所有者”和“其他人”对文件权限不受mask限制。

 

 

 

原创文章,作者:M20-1--孔祥文,如若转载,请注明出处:http://www.178linux.com/28408

(0)
M20-1--孔祥文M20-1--孔祥文
上一篇 2016-08-04
下一篇 2016-08-04

相关推荐

  • 链接分析算法之:HillTop算法

       Hilltop算法是由Krishna Baharat 在2000年左右研究的,于2001年申请专利,但是有很多人以为Hilltop算法是由谷歌研究的。只不过是Krishna Baharat 后来加入了Google成为了一名核心工程师,然后授权给Google使用的。     &nbsp…

    大数据运维 2015-04-08
  • Linux文件系统总结(7.4课上作业)

    一、什么是文件系统         文件系统是对一个存储设备上的数据和元数据进行组织的机制。它使用文件和树形目录的抽象逻辑概念代替了硬盘和光盘等物理设备使用的数据块的概念,用户使用文件系统来保存数据而不必关心数据实际保存在硬盘(or光盘)的地址为多少数据块上,只需要记住这个文件的…

    Linux干货 2016-07-10
  • shell中的位置参数变量

    要了解位置变量,首先要知道什么是变量?  变量就是让某一个特定的字符串来代表不固定的内容,简单理解就是用一个简单的“字眼”来替代另一个比较复杂或者容易变动的数据。 而什么又是位置变量呢?  位置变量就是bash将传递给脚本的参数,按照位置不同,保存在不同的位置变量中,以便于脚本调用这些变量。 位置参数变量: $1, $2, ……

    Linux干货 2016-08-12
  • linux 进程解析

    博文介绍了进程信息以及部分进程管理命令

    2017-09-09
  • 8月3日作业

    课堂练习: 当用户xiaoming对/testdir 目录无执行权限时,意味着无法 做哪些操作? 不能进入目录,不能创建目录、文件 当用户xiaoqiang对/testdir 目录无读权限时,意味着无法做 哪些操作?  不能查看目录、文件 当用户wangcai 对/testdir 目录无写权限时,该目录下的只 读文件file1是否可修改和删除?&n…

    Linux干货 2016-08-07
  • vim编辑器-练习题

    1 、复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的行首的空白字符 #cp /etc/profile /tmp #vim /tmp/profile :%s/^[[:space:]]\+// 2 、复制/etc/rc.d/init.d/functions 文件至/tmp 目录,用查找替换命令为/tmp/func…

    Linux干货 2016-08-15