ACL即Access Control List 主要的目的是提供傳統的owner,group,others的read,write,execute權限之外的具體權限設置,ACL可以針對單一用戶、單一文件或目錄來進行r,w,x的權限控制,對於需要特殊權限的使用狀況有一定幫助。如,某一個文件,不讓單一的某個用戶訪問。
ACL使用兩個命令來對其進行控制
getfacl:取得某個文件/目錄的ACL設置項目
setfacl:設置某個文件/目錄的ACL設置項目
setfacl 參數 -m:設置后續acl參數 -x:刪除后續acl參數 -b:刪除全部的acl參數 -k:刪除默認的acl參數 -R:遞歸設置acl,包括子目錄 -d:設置默認acl
例:創建一文件test,將其權限修改為777,並查看其默認ACL權限配置
[root@Self-study ~]# touch /test [root@Self-study ~]# chmod 777 /test [root@Self-study ~]# getfacl /test //獲得文件的ACL權限 getfacl: Removing leading '/' from absolute path names # file: test //文件名 # owner: root //文件所屬者 # group: root //文件所屬組 user::rwx //文件所屬者權限 group::rwx //同組用戶權限 other::rwx //其它者權限 [root@Self-study ~]#
可以看到其它者的權限也是可讀可寫可執行,可以自行測試,現在我們修改其ACL策略,使用用戶code只有讀取的權限
[root@Self-study ~]# setfacl -m u:code:r /test [root@Self-study ~]# ll /test -rwxrwxrwx+ 1 root root 1 Apr 11 07:25 /test //可以看到權限的最后多了一個”+”號 [root@Self-study ~]#
現在再次查看一下此文件的ACL屬性
[root@Self-study ~]# getfacl /test
getfacl: Removing leading '/' from absolute path names # file: test # owner: root # group: root user::rwx user:code:r-- //可以看到code單獨的權限為r-- group::rwx mask::rwx other::rwx
[root@Self-study ~]#
注:code的權限並不是只根據ACL配置來決定的,它是由code用戶基本權限與配置的ACL權限的“與”運算決定的,即other:rwx 與 code:r-- = code:r--
現在使用code用戶,測試是否可寫
在寫文件時,會出現-- INSERT -- W10: Warning: Changing a readonly file提示。
除了對單個用戶進行設置外,還可以對用戶組、有效權限(mask)進行設置如對用戶組設置: g:[用戶組]:[rwx]
注:有效權限(mask) 即用戶或組所設置的權限必須要存在於mask的權限設置范圍內才會生效
如上面的/test文件,已經有了可讀權限,如果我們把它的有效權限修改為只有寫權限,則設置的acl權限不在有效權限之內,則用戶code就不可能再查看/test文件中的內容了
[root@Self-study ~]# setfacl -m m:w /test //設置有效權限為只寫
可以查看/test acl屬性
[root@Self-study ~]# getfacl /test getfacl: Removing leading '/' from absolute path names # file: test # owner: root # group: root user::rwx user:code:r-- #effective:--- group::rwx #effective:-w- mask::-w- //可以看到有效權限已經修改成功 other::rwx [root@Self-study ~]#
使用code用戶查看文件內容,首先使用root用戶寫入一些內容,會使測試更加直觀
[root@Self-study ~]# echo "this is a test getfacl " >/test [code@Self-study ~]$ vim /test "/test" [Permission Denied] //可以在最下面看到不允許訪問的提示,並且看不到任何內容
取消acl權限
[root@Self-study ~]# setfacl -x u:code /test //取消/test對用戶code的權限 [root@Self-study ~]# setfacl -x m /test //恢復有效權限 [root@Self-study ~]# getfacl /test getfacl: Removing leading '/' from absolute path names # file: test # owner: root # group: root user::rwx group::rwx other::rwx [root@Self-study ~]# ll /test -rwxrwxrwx 1 root root 24 Apr 11 08:01 /test //已經可以正常使用 [root@Self-study ~]#
至於另外的一些參數,自己嘗試使用!!