一、什么是ACL
ACL(Access Control List)可靈活地,更細粒度地定義訪問文件或目錄的權限。
二、為什么使用ACL
Linux上文件系統的文件系統權限管理的對象分為三類:owner,group,other。這種分類非常簡單,如果我希望有一個用戶擁有不同于這三類對象的權限,或者再定義一個用戶組的權限,傳統的權限管理就不能實現,而ACL可以很好的解決這個問題。
三、ACL條目
一系列ACL條目構成了ACL。條目應包含標簽類型(tag type),可選擇的標簽修飾定語,以及權限設定。
其中標簽類型分為:
1.acl_user_obj: 文件的屬主
2.acl_user: 自定義用戶
3.acl_group_obj: 文件的所屬組
4.acl_group: 自定義用戶組
5.acl_mask: 規定了acl_user,acl_group_obj,acl_group可被授予的最大權限
6.acl_other: 不在acl里定義的其他用戶
[root@centos7 data]# getfacl f1 # file: f1 # owner: root # group: mysql user::rwx #acl_user_obj user:cutemsyu:rw- #acl_user group::r-x #acl_group_obj #effective:r-- group:cutemsyu:rw- #acl_group group:fly:r-- #acl_group mask::rw- #acl_mask other::--- #acl_other
tag type 可簡寫,如u表示user,g表示group。
四、ACL設置和查看方法
setfacl :設置 getfacl :查看
格式:setfacl [-bkndRLPvh] [{-m|-x} acl_spec] [{-M|-X} acl_file] file …
getfacl [-aceEsRLPtpndvh] file …
1、直接設置模式:
-m :修改權限
-x :移除權限
–set:設置權限,取代原來的acl權限設置,必需包含u,g,o的tag type
例如,修改權限并查看
[root@centos7 testdir]# setfacl -m u:cutemsyu:r,g:cutemsyu:r example [root@centos7 testdir]# getfacl example # file: example # owner: root # group: root user::rw- user:cutemsyu:r-- group::r-- group:cutemsyu:r-- mask::r-- other::r--
移除權限并查看
[root@centos7 testdir]# setfacl -x g:cutemsyu example [root@centos7 testdir]# getfacl example # file: example # owner: root # group: root user::rw- user:cutemsyu:r-- group::r-- mask::r-- other::r--
2、通過文件設置
ACL支持讀取文件來設置權限
-M:修改權限
-X:移除權限
這里我給出一個文本acl_file,
[root@centos7 testdir]# cat acl_file u:cutemsyu:rw #一行只能寫一個條目 u:fly:rw g:cutemsyu:r
修改權限并查看
[root@centos7 testdir]# touch example2 [root@centos7 testdir]# setfacl -M acl_file example2 [root@centos7 testdir]# getfacl example2 # file: example2 # owner: root # group: root user::rw- user:cutemsyu:rw- user:fly:rw- group::r-- group:cutemsyu:r-- mask::rw- other::r--
再創建一個文本用來移除權限
[root@centos7 testdir]# cat acl_file_remove u:cutemsyu u:fly g:cutemsyu
移除權限并查看
[root@centos7 testdir]# setfacl -X acl_file_remove example2 [root@centos7 testdir]# getfacl example2 # file: example2 # owner: root # group: root user::rw- group::r-- mask::r-- other::r--
3、為目錄設置ACL
為目錄設置ACL,設置方法相同,可用-R進行遞歸
另外目錄的ACL可加默認權限,不作用于目錄本身,表示再此目錄下新建的文件或目錄繼承默認權限
例如:
[root@centos7 testdir]# setfacl -m o::-,u:fly:rwx,d:u:fly:rwx test/ #d:表示默認權限 [root@centos7 testdir]# getfacl test/ # file: test/ # owner: root # group: root user::rwx user:fly:rwx group::r-x mask::rwx other::--- default:user::rwx #未定義的默認tag type會跟隨目錄相應權限 default:user:fly:rwx default:group::r-x default:mask::rwx default:other::---
在此目錄下新建目錄和文件,查看ACL
[root@centos7 test]# mkdir dir1;touch file1; [root@centos7 test]# getfacl dir1 file1 # file: dir1 # owner: root # group: root user::rwx user:fly:rwx group::r-x mask::rwx other::--- default:user::rwx default:user:fly:rwx default:group::r-x default:mask::rwx default:other::--- # file: file1 # owner: root # group: root user::rw- user:fly:rwx #effective:rw- group::r-x #effective:r-- mask::rw- #文件mask不繼承x權限 other::---
4、復制一個文件的ACL
setfacl 支持管道輸入 –set-file=-
getfacl file1|setfacl --set-file=- example #復制file1權限到example
5、備份和恢復ACL
mv和cp備份文件時都會保留ACL信息,cp指令需加-p選項
但是tar等工具工具備份文件時不會保留ACL信息,這時需要我們備份ACL信息
getfacl -R /testdir/data >acl.bak #遞歸備份ACL信息 setfacl -R --set-file=acl.bak /testdir/data #遞歸恢復ACL信息
6、清除ACL
setfacl -k dir #清除默認acl設置 setfacl -b file #清除所有acl設置
五、ACL和文件權限位的關系
設置ACL的文件或目錄,ls -l 查看權限位最后的 "."會變成"+"
drwxrwx---+ 2 root root 6 Aug 6 10:32 dir1
owner位與ACL中acl_user_obj 對應
group位與ACL中acl_mask對應
other位與ACL中acl_other對應
六、ACL權限允許訪問邏輯
當一個線程訪問一個被ACL保護的文件時,其獲權邏輯如下:
1.當線程的有效用戶ID與文件owner相匹配時,如果owner擁有相應權限,則允許訪問,否則拒絕。
2.當線程的有效用戶ID與acl_user相匹配時,如果mask和acl_user都具有相應權限則允許訪問,否則拒絕。
3.當線程的有效組ID或者補充組ID與文件所屬組或任意acl_group相匹配時,
(1)當ACL含有mask條目時
如果mask和任何匹配的組擁有相應權限,則允許訪問,否則拒絕
(2)當ACL不含有mask條目時
如果文件所屬組擁有相應權限,則允許訪問,否則拒絕
4.如果acl_other擁有相應權限,則允許訪問
5.其他則拒絕
原創文章,作者:cutemsyu,如若轉載,請注明出處:http://www.www58058.com/29774