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 14:39
下一篇 2016-08-04 14:39

相关推荐

  • 控制字体颜色的输出echo[转载]

    http://onlyzq.blog.51cto.com/1228/546459 echo显示带颜色,需要使用参数-e格式如下:echo -e "\033[字背景颜色;文字颜色m字符串\033[0m"例如: echo -e "\033[41;37m TonyZhang \033[0m"其中41的位置…

    Linux干货 2017-02-01
  • 构建NP和NMP

    实验一、构建NP (一)实验布置:两台虚拟机充当代理服务器和后端服务器,一台虚拟机充当客户端。 (二)实验目的:实现NP的搭建。 (三)实验图解: (四)实验步骤: 1、  在后端服务器安装php-fpm文件,修改PHP-FPM的配置文件,vim/etc/php-fpm.d/www.conf文件,如下: listen = 127.0.0.1:900…

    2017-05-07
  • bash编程基础(二)补

       bash脚本编程         脚本文件格式:         第一行,顶格:#!/bin/bash         注释信息:#         代码注释:  …

    Linux干货 2016-12-23
  • bash编程之数组和字符串处理

    数组: 程序=指令+数据          指令:     数据:变量、文件 数组:存储过个元素的连续的内存空间; 变量:存储单个元素的内存空间; 数组名:整个数组只有一个名字; 数组索引:编号从0开始;   &nbsp…

    Linux干货 2016-08-24
  • 【原创】RHEL7-PPTP-VPN-Server排错

    第一次写博客,明显不知道如何下笔。     昨天6月21日,突然发现往日运行一切正常的pptpvpn服务器怎么也连不上了,错误代码是619。这个错误代码以前并没有见过,于是上google查了一下资料,据说有几种可能: 1,路由器或防火墙干掉了tcp1723; 2,电脑协议栈问题; 3,拨号连接的认证选项有问题; &nb…

    Linux干货 2016-06-23
  • SED基本用法和在文本中的使用

    sed命令行格式为: sed [-nefri] ‘command’ 输入文本/文件 常用选项: -n∶取消默认的输出,使用安静(silent)模式。在一般 sed 的用法中,所有来自 STDIN的资料一般都会被列出到屏幕上。但如果加上 -n 参数后,则只有经过sed 特殊处理的那一行(或者动作)才会被列出来 -e∶进行多项编辑,…

    Linux干货 2017-05-05