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

相关推荐

  • N28-第二周

    文件管理命令: cp命令:copy    源文件;目标文件; 单源复制:cp [OPTION]… [-T] SOURCE DEST多源复制:cp [OPTION]… SOURCE… DIRECTORYcp [OPTION]… -t DIRECTORY SOURCE… 单源复制:cp [OPTION]…

    Linux干货 2017-12-17
  • 基于nginx实现7层http的负载均衡

    一、实验环境实验环境为三台服务器:1. nginx负载均衡器1. 内网ip:192.168.11.1002. 外网ip:172.16.251.892. 提供网页服务的RS-1服务器:192.168.11.2013. 提供网页服务的RS-2服务器:192.168.11.2024. 拓扑如下:二、实验配置后台服务器配置:1. 后台提供网页服务的两台服务器配置:y…

    Linux干货 2017-06-29
  • 学习积累01#计算机组成#Linux版本#基本命令

    第一周的积累 问题1:描述计算机的组成及其功能 CPU:中央处理器,主要功能是进行运算和逻辑运算,内部大致可分为控制单元、逻辑算术单元、存储单元; 主板:核心部件,是电脑的“脉络”,CPU\内存\控制核心电路均安装在主板上,各种外部设备也通过主板上的插槽相互连接; 硬盘:常见的外存储器,容量大,保存时间长、安全性高。接口主要分为IDE、SATA、SCSI。 …

    Linux干货 2016-10-25
  • 网络及TCP三次握手四次挥手

    批处理应用程序     FTP 、TFTP 、库存更新     无需直接人工交互     带宽很重要,但并非关键性因素 交互式应用程序     库存查询、数据库更新。     人机交互。     因为用户需等待响应,所以响应时间很重要,但并非关键性因素,除非要等待很长时间。 实时应用程序     VoIP 、视频     人与人的交互     端到端的延时至…

    2017-05-06
  • five

    1;显示当前系统上root, fedora或user1用户的默认shell。 #   grep "^\(root\|fedora\|user1\)" /etc/passwd #   grep -E "^(root|fedora|u…

    Linux干货 2017-01-16
  • do some test

    1、简述TCP三次握手四次挥手过程及各过程中客户端和服务器端的状态。     三次握手:         a(syn-send) -> send syn -> b(listen)         a(syn-send) <- receive …

    Linux干货 2016-06-12