Linux入门之facl文件访问控制列表

简介:

linux默认的3中基本权限(rwx)以及3中特殊权限(suid,sgid,sticky)在平常情况下做适当调整即可,但是如果出现多个组多个成员情况下对某些文件或目录做权限配置就会发现不够分配,所以为了解决此类情况linux内核出现了acl(访问控制列表)模块来进行分层管理

 

版本对功能支持度:

readhat7centos7默认创建的xfsext4文件系统会有acl功能,如果redhat56等版本

新格式化的文件系统可能需要命令去添加:

tune2fs  -o  acl  /dev/to/path      #直接对设备做内部支持调整

mount  -o acl  /dev/to/path  /mnt  #或者挂载时加入此功能

 

ACL 生效的优先级:

所有者,自定义用户,自定义组,其他人

 

使用命令对文件或目录查看其facl    getfacl 命令

getfacl  <FILE|DIR>

[root@mzf facldir]# ls -l  file1            
-rw-r--r--. 1 root root 0 Aug  4 08:57 file1

#查看权限发现其它人的rwx后面有个 . 表示未设置

[root@mzf facldir]# getfacl  file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
other::r--

#查看发现只有默认的权限,下面查看一个设置过facl的文件

[root@mzf facldir]# ls -l file2
-rw-rw-r--+ 1 root root 0 Aug  4 09:02 file2

#此时发现这个文件权限位最后是一个 + ,这就表示该文件被设置过facl权限

[root@mzf facldir]# getfacl file2
# file: file2                   #文件名
# owner: root                   #文件所有者
# group: root                   #文件所属组
user::rw-                 #文件属主权限
user:tom:rw-               #添加的指定用户(tom)
group::r--                #文件属组权限
mask::rw-                #除了onwer、other,其他用户设置的权限都将不能超过mask的权限
other::r--#其他用户的权限

#发现除了文件属主root还加入了一个tom用户也有读写权限,而tom就是额外facl添加的

对文件或目录设置facl权限  setfacl命令

setfalc  [option]  [action]  [file|dir]

option:

-m :设置特定条目权限

-x  :删除特定条目权限

-b  :清除指定文件的所有facl权限

-R  :只能对指定目录文件,则同时设置其目录下的所有文件

-k  :清除默认(default)acl权限

-M FILE:读取指定格式文本文件中的acl条目进行设置

-X FILE :读取特定格式文本文件中acl条目对进行匹配删除acl权限

action

    u:uname:###  /path/to/file  对指定文件设置某一用户的facl

g:gname:###  /path/to/file  对指定文件设置某一用户组的facl

m::###   /path/to/file      对指定文件设置mask限制权限

d:[action]  /path/to/dirfile   对指定目录设置,其目录下新建文件及目录将继承facl

 

设置例子:

#对指定文件添加或设置单一指定用户facl权限

[root@mzf facldir]# setfacl -m u:tom:rw file1
[root@mzf facldir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:tom:rw-
group::r--
mask::rw-
other::r--


解释:在user加入一条新user条目tom,说明tomfile1有读写权限

#对指定文件添加或设置多个用户或组facl权限

[root@mzf facldir]# setfacl -m u:tom:rw,u:wangcai:w file1
[root@mzf facldir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
user:wangcai:-w-
user:tom:rw-
group::r--
mask::rw-
other::r--

#同时添加指定用户和指定组facl

[root@mzf facldir]# setfacl -m g:it:rw,u:mzf:rw,u:tom:w file2
[root@mzf facldir]# getfacl file2
# file: file2
# owner: root
# group: root
user::rw-
user:mzf:rw-            #添加了mzf用户对文件有读写权限
user:tom:-w-            #添加了tom用户对文件有读权限,而tom作为onther却不能读
group::r--
group:it:rw-
mask::rw-
other::r--


解析:同时添加了3个条目用户mzftomit组,并且权限设置不同

#同时删除指定用户或指定组facl

[root@mzf facldir]# setfacl -x u:mzf:,g:it file2
[root@mzf facldir]# getfacl file2
# file: file2
# owner: root
# group: root
user::rw-
user:tom:-w-            #保留了tom用户facl,刚才的it组和mzf用户条目被删除
group::r--
mask::rw-
other::r--


#验证facl的优先级

[root@mzf facldir]# su tom#切换到tom用户
[tom@mzf facldir]$ cat file2#查看刚才的file2文件
cat: file2: Permission denied         #没有读权限

解析:onther默认是有r权限的,但是tom用户做为facl条目中只有写权限,却只能修改文件而不能查看文件,说明在开启了acl 的文件系统上facl的优先级更高

#对文件夹做递归或者权限继承
#递归设置目录及目录下所有文件
[root@mzf facldir]# setfacl -R -m u:mzf:rwx /testdir/facldir/
[root@mzf facldir]# getfacl . file1 file2
# file: .
# owner: root
# group: root
user::rwx
user:mzf:rwx
group::r-x
mask::rwx
other::r-x
 
# file: file1
# owner: root
# group: root
user::rw-
user:mzf:rwx
user:wangcai:-w-
user:tom:rw-
group::r--
mask::rwx
other::r--
 
# file: file2
# owner: root
# group: root
user::rw-
user:mzf:rwx
user:tom:-w-
group::r--
mask::rwx
other::r--


解析:对facldir目录下的进行了递归设置,其下面的文件及子目录都会被添加目录的条目,但是再在目录下新建的目录或文件不会自动继承

#目录下新建子目录或文件就继承facl权限

#使用d自动表示对目录文件实现继承facl

[root@mzf facldir]# setfacl -m d:u:tom:rwx /testdir/facldir/   
[root@mzf facldir]# touch file4 file5              #创建新新文件检查
[root@mzf facldir]# ls -l file4 file5           #查看新建的文件权限后都自动加了+
-rw-rw-r--+ 1 root root 0 Aug  4 11:04 file4
-rw-rw-r--+ 1 root root 0 Aug  4 11:04 file5


解析:+ 表示此文件有facl设置,默认一般文件创建不会添加facl条目,facl权限位显示的是一个 . ,但是对目录加上d字段代表继承,但是文件不会继承x权限

 

 

总结以下格式:

setfacl  -m  u:uname:rwx  /path/to/file       #设置单一指定用户条目
setfacl  -x   u:uname  /path/to/file      #删除单一指定用户条目
setfacl  -m  g:gname:rwx  /path/to/file       #设置单一指定用户组条目
setfacl  -x   g:gname:     /path/to/file   #删除单一指定用户组条目
setfacl  -m  u:uname:rwx,g:gname:rx   /path/to/file   #同时设置多条目
setfacl  -x   g:gname:,u:uname   /path/to/file     #同时删除指定多条目
setfacl  -k   /path/to/file               #清除所有默认(default)facl条目
setfacl  -b   /path/to/file               #删除所有facl存在设置
setfacl  -Rm  g:gname:rwX  /path/to/dir_file #对目录有效递归设置,普通文件不会有x权限
seffacl  -M  file.acl  /path/to/file     #将指定格式文本文件条目设置到对应文件
file.acl文件格式: u:uname:rw  等
setfacl  -X   file.acl  /path/to/file    将删除格式文件中匹配的条目
getfacl  /path/to/file1 | setfacl  --set-file=-  /path/to/file2 
#将file1权限复制给file2
setfacl  -m  mask::rw   /path/to/file   #设置mask权限

setfacl  --set  u:uame:rwx  /path/to/file   #重置所有并添加一个条目


 

 

案例:

备份dir1文件夹、facl条目

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


 

/data/testdir里创建的新文件自动属于g1组,组g2的成员如:alice能对这些新文件有读写权限

,组g3的成员如:tom只能对新文件有读权限,其它用户(不属于g1,g2,g3)不能访问这个文件夹。

mkdir -p /data/testdir
groupadd g1
groupadd g2
groupadd g3
chown :g1  /data/testdir
chmod g+s /data/testdir
gpasswd -a alice  g2
gpasswd -a tom    g3
setfacl -m  g:g2:rw,g:g3:r /data/testdir
chmod o=  /data/testdir