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

相关推荐

  • 第四周作业

    1、复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其他用户均没有任何访问权限 答:     复制目录:cp -R /etc/skel /home/tuser1     修改权限:chmod -R go=- /home/tuser1 2、编辑/etc/group文件…

    Linux干货 2016-12-07
  • CentOS6.8编译安装—- php-fpm

    安装环境: [root@station11 ~]# lsb_release -a Distributor ID: CentOS Description:     CentOS release 6.8 (Final) Release:  6.8 Codename:    …

    Linux干货 2016-12-21
  • select基本用法和软件包管理

    一、select语句     select语句在脚本中用于创建菜单,按数字顺序排列的菜单项显示在标准输出上,并用PS3提示符,等待用户输入。用户输入菜单中的某个数字,执行相应的命令,并被保存在内置变量REPLY中。 用法:     select NAME [in WORDS &#…

    Linux干货 2016-08-21
  • 文件系统结构

    -文件系统结构
    -应用程序的组成部分
    -文件类型

    2018-03-13
  • bash之运算、测试、if语句及for循环语句

      一、逻辑运算         布尔运算:真、假         与运算:                 真 && 真 = 真       …

    Linux干货 2015-05-25
  • inode总结

    inode的介绍以及影响inode号的命令

    2017-11-27