RH134-6 使用訪問控制列表限制文件訪問
第六章、使用訪問控制列表限制文件訪問
6.1、 ACL訪問控制列表概述
實現更多的權限控制,可以同時針對多個不同的用戶、組進行權限設定
ext2,ext3,ext4文件系統掛載的時候需要使用acl選項,xfs默認就支持.
設定acl只能是root
命令: getfacl , setfacl
# ll /tmp/passwd
-rw-r--r--. 1 root root 2005 Jul 19 11:38/tmp/passwd
# getfacl /tmp/passwd 在沒有對文件設定acl的時候,看到結果和ls -l 看到的傳統權限設定是一樣
getfacl: Removing leading ‘/‘ from absolutepath names
# file: tmp/passwd
# owner: root
# group: root
user::rw- 文件owner權限
group::r-- 文件擁有組權限
other::r-- 其他人權限
文件設定acl後:
# ll /tmp/passwd
-rwxrwx---+ 1 root root 2058 May 11 21:08/tmp/passwd
# getfacl /tmp/passwd
getfacl: Removing leading ‘/‘ from absolutepath names
# file: tmp/passwd
# owner: root
# group: root
user::rwx
user:mary:rw- #effective:r--
user:jack:r--
group::rwx #effective:r-x
group:tom:r--
mask::r-x
other::---
目錄設定默認acl後:
# getfacl /tmp/test
getfacl: Removing leading ‘/‘ from absolutepath names
# file: tmp/test
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:tom:r-x
default:group::r-x
default:mask::r-x
default:other::r-x
6.2、制定ACL訪問控制列表
例子1:
要求在desktop虛擬機裏把文件的屬性修改tom:admin , 權限默認為644
要求: tom對該文件有所有的權限,mary可以讀寫該文件,admin組可以讀寫執行該文件,jack只讀該文件,其他人一律不能訪問該文件
tom組僅僅對該文件只讀
實驗前,建立幾個普通用戶
# useradd tom
# useradd bean
# useradd mary
# useradd jack
# groupadd admin
# gpasswd -a mary admin
# gpasswd -a bean admin
# id tom
uid=1001(tom) gid=1001(tom)groups=1001(tom)
# id mary
uid=1002(mary) gid=1002(mary)groups=1002(mary),1004(admin)
# id bean
uid=1003(bean) gid=1003(bean)groups=1003(bean),1004(admin)
# id jack
uid=1004(jack) gid=1005(jack)groups=1005(jack)
# chown tom:admin /tmp/passwd
# chmod 644 /tmp/passwd
# setfacl -muser::rwx,user:mary:rw,group::rwx,user:jack:r,group:tom:r,other::--- /tmp/passwd
# getfacl /tmp/passwd
getfacl: Removing leading ‘/‘ from absolutepath names
# file: tmp/passwd
# owner: tom
# group: admin
user::rwx
user:mary:rw-
user:jack:r--
group::rwx
group:tom:r--
mask::rwx <---除了 user:: 和 other:: ,會於中間的所有控制列表進行 交集運算, 算出對應規則有效權限
other::---
acl的控制規則是從上往下匹配的
tom由於他是文件的擁有者,所以直接安裝user:: 指定的權限去操作 --》 rwx
mary不是文件的擁有著,從上往下找規則,發現user:mary:rw-能夠精確匹配mary用戶 --》 rw-
盡管mary是屬於admin組,admin組有rwx的權限,但是由於mary的規則在前面,所有優先生效
bean由於找不到精確匹配bean用戶的規則,而bean是屬於admin組,根據文件的定義,該文件是屬於admin組的,所以bean的權限是按照group::rwx的權限去操作。 --》 rwx
jack 不是文件的主人,並且找到user:jack:r-- ,所以只讀
student用戶,不是文件主人,也找不到精確匹配的user定義規則,也找不到相關組的定義規則,最後屬於other --》 ---
理解mask的作用
=========================================
刪除關於jack的規則
# setfacl -x user:jack /tmp/passwd
去掉所有規則
# setfacl -b /tmp/passwd
===========================================
例子2:要求在server0虛擬機上完成
執行 lab acl setup開始實驗
controller組成員:student
sodor組成員有:thomas,james 密碼均為redhat
要求: sodor組的成員可以對/shares/steamies有所有的權限,但sodor組成員james對該目錄以及目錄裏的現有文件和以後建立的文件什麽權限都沒有.
目錄裏的display_engines.sh用於測試執行權限.
# setfacl -Rm g:sodor:rwX /shares/steamies
# setfacl -Rm u:james:- /shares/steamies
# setfacl -m d:g:sodor:rwx /shares/steamies
# setfacl -m d:u:james:- /shares.steamies
# exit
$ su - thomas
$ cd /shares/steamies/
$ cat roster.txt
$ ./display_engines.sh
$ mkdir tidmouth
$ echo "toot toot" >tidmouth/whistle.txt
$ exit
$ su - james
$ cd /shares/steamies
$ ls !$
$ cat !$/roster.txt
student$ newgrp controller
==================================================
刪除目錄的所有默認acl
# setfacl -k /tmp/dir
===================================================
實驗:
共享目錄: /shares/cases,目錄下存在兩個文件分別為adventures.txt和moriarty.txt
bakerstreet組成員:holmes,watson
scotlandyard組成員: lestrade,gregson,jones
所有用戶密碼均為redhat
所有更改應用在cases目錄及其下的文件,但不會應用到/shares目錄
所有bakerstreet組成員可以對cases目錄裏的文件有讀和寫權限
scotlandyard組成員jones只能對目錄裏的文件有只讀權限,其余的scotlandyard組成員均可以對該目錄的文件有讀寫權限
目錄裏的所有內容都屬於bakerstreet組,讓目錄裏的文件可以讓文件的擁有者和擁有組可以讀寫,其他用戶沒有任何權限,要求目錄裏以後新建的文件都自動屬於bakerstreet組
# lab acl setup
# chgrp -R bakerstreet /shares/cases/
# chmod g+s /shares/cases/
# setfacl -R -muser::rwX,group::rwX,group:scotlandyard:rwX,user:jones:rX,other::-/shares/cases/
# setfacl -md:user::rwX,d:group::rwx,d:group:scotlandyard:rwx,d:user:jones:rx,other::-/shares/cases/
# lab acl grade
個人筆記:
[[email protected] ~]# chown tom:admin/tmp/passwd
[[email protected] ~]# chmod 644 /tmp/passwd
[[email protected] ~]# ll /tmp/passwd
-rw-r--r--. 1 tom admin 2005 Jun 28 21:41/tmp/passwd
[[email protected] ~]# setfacl -mu::rwx,u:mary:rw,g:admin:rwx,u:jack:r,o::-,g:tom:r /tmp/passwd
[[email protected] ~]#
[[email protected] ~]# getfacl /tmp/passwd
getfacl: Removing leading ‘/‘ from absolutepath names
# file: tmp/passwd
# owner: tom
# group: admin
user::rwx
user:jack:r--
user:mary:rw-
group::r--
group:tom:r--
group:admin:rwx
mask::rwx
other::---
改MASK
[[email protected] ~]# setfacl -m mask::rx/tmp/passwd
[[email protected] ~]# setfacl /tmp/passwd
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ...} file ...
Try `setfacl --help‘ for more information.
[[email protected] ~]# getfacl /tmp/passwd
getfacl: Removing leading ‘/‘ from absolutepath names
# file: tmp/passwd
# owner: tom
# group: admin
user::rwx
user:jack:r—
user:mary:rw- #effective:r--
group::r--
group:tom:r--
group:admin:rwx #effective:r-x
mask::r-x
other::---
對user和other是不受影響的,中間的用戶才受MASK的影響。
刪除設定的權限
[email protected] ~]# setfacl -x user:jack/tmp/passwd
[[email protected] ~]# setfacl /tmp/passwd
Usage: setfacl [-bkndRLP] { -m|-M|-x|-X ...} file ...
Try `setfacl --help‘ for more information.
[[email protected] ~]# getfacl /tmp/passwd
getfacl: Removing leading ‘/‘ from absolutepath names
# file: tmp/passwd
# owner: tom
# group: admin
user::rwx
user:mary:rw-
group::r--
group:tom:r--
group:admin:rwx
mask::rwx
other::---
[[email protected] ~]# setfacl -x /tmp/passwd
setfacl: Option -x: Invalid argument nearcharacter 1
[[email protected] ~]# setfacl -b /tmp/passwd
[[email protected] ~]# getfacl /tmp/passwd
getfacl: Removing leading ‘/‘ from absolutepath names
# file: tmp/passwd
# owner: tom
# group: admin
user::rwx
group::r--
other::---
[[email protected] ~]#
例2.
本文出自 “HCIE_38xx” 博客,謝絕轉載!
RH134-6 使用訪問控制列表限制文件訪問