ACL 是 Access Control List 的縮寫, 主要的目的是提供傳統的 owner 、 group 、 other 的 read 、 write 、 execute 許可權之外的具體許可權設定.
ACL 可以針對單一使用者、單一檔案或目錄來進行 r 、 w 、 x 的許可權設定, 對於需要特殊許可權的使用狀況非常有幫助.
那麼 ACL 主要可以針對那些方面來控制許可權呢? 它主要可以針對幾個專案:
* 使用者 (user): 可以針對使用者來設定許可權;
* 使用者組 (group): 針對使用者組來設定其許可權;
* 預設屬性 (mask): 還可以在該目錄下在新建新檔案/目錄是設定新資料的預設許可權.
 
<1>如何啓動 ACL
由於 ACL 是傳統 UNIX-like 操作系統許可權的額外支援專案, 因此要使用 ACL 必須要有檔案系統的支援才行. 目前絕大數的檔案系統都有支援 ACL 的功能.
那我們如何檢視你的檔案系統是否支援 ACL 呢? 我們可以這樣看:
[root@sqj2015 ~]# mount      //直接檢視掛載引數的功能
/dev/vda1 on / type ext4 (rw)
…………
##這裏我們沒有看到 ACL
 
[root@sqj2015 ~]# dumpe2fs -h /dev/vda1 | grep ‘mount options’
dumpe2fs 1.41.12 (17-May-2010)
Default mount options:    user_xattr acl
## 由 mount 單純去查詢不見得可以看到實際的選項, 由於目前新的 distribution 經常會主動加人某些預設功能.
 
若你的系統預設不會幫你加上 acl 的支援呢? 那麼你可以這樣操作:
[root@sqj2015 ~]# mount -o remount,acl /
[root@sqj2015 ~]# mount
/dev/vda1 on / type ext4 (rw,acl)
##這樣就加入了! 但是如果你想要每次開機都生效, 則需要這樣:
[root@sqj2015 ~]# vi /etc/fstab
/dev/vda1           /               ext4    defaults,acl    1 1
 
<2>ACL 設定技巧
在檔案系統啓動 ACL 支援後, 接下來該如何設定與檢視 ACL 呢? 很簡單, 利用以下 2 個命令就可以了:
getfacl: 檢視某個檔案/目錄的 ACL 設定專案;
setfacl: 設定某個目錄/檔案的 ACL 規定;
 
setfacl 語法
setfacl [引數] 目標檔名
引數:
-m : 設定後續的 acl 引數給檔案使用, 不可與-x 合用;
-x : 刪除後續的 acl 引數, 不可與-m 合用;
-b : 刪除所有 acl 設定引數;
-k : 刪除預設 acl 引數;
-R : 遞迴設定 acl, 亦即包括子目錄都會被設定起來;
-d : 設定預設 acl 引數, 只對目錄有效, 在該目錄新建的資料會引用此預設值.
 
接下來, 就來看看 setfacl 的設定方式:
**針對特定使用者的方式:
##設定規定: “u:[使用者賬號列表]:[rwx]” 如下, 設定 sqj 許可權為 rw:
[root@sqj2015 ~]# touch acl_test
[root@sqj2015 ~]# ll acl_test
-rw-r–r–. 1 root root 0 Mar 21 13:44 acl_test
[root@sqj2015 ~]# setfacl -m u:sqj:rw acl_test
[root@sqj2015 ~]# ll acl_test
-rw-rw-r–+ 1 root root 0 Mar 21 13:44 acl_test
##是否看到了不同, 在許可權部分多了個+.
 
[root@sqj2015 ~]# setfacl -m u::rwx acl_test
[root@sqj2015 ~]# ll acl_test
-rwxrw-r–+ 1 root root 0 Mar 21 13:44 acl_test
##若無使用者列表, 則代表設定該檔案所有者, 所以上面顯示 root 的許可權變成了 rwx 了.
 
但我們具體怎麼檢視設定的 ACL 呢? 如下:
[root@sqj2015 ~]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rwx
user:sqj:rw-
group::r–
mask::rw-
other::r–
 
**針對特定使用者組的方式:
##設定規定: “g:[使用者組列表]:[rwx]”, 例如, 針對 sqj 組許可權 rx:
[root@sqj2015 ~]# setfacl -m g:sqj:rw acl_test
[root@sqj2015 ~]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rwx
user:sqj:rw-
group::r–
group:sqj:rw-
mask::rw-
other::r–
 
**針對有效許可權 mask 的設定方式:
##mask 它的意思是使用者或組所設定的許可權必須存在與 mask 的許可權設定範圍內才能生效, 此即有效許可權.
##設定規定: “m:[rwx]”, 例如針對剛才的檔案規定為僅有 r 許可權:
[root@sqj2015 ~]# setfacl -m m:r acl_test
[root@sqj2015 ~]# getfacl acl_test
# file: acl_test
# owner: root
# group: root
user::rwx
user:sqj:rw-                    #effective:r–
group::r–
group:sqj:rw-                   #effective:r–
mask::r–
other::r–
##sqj 使用者、 sqj 組與 mask 的集合僅有 r 存在, 因此 sqj 僅具有 r 的許可權而已, 並不存在 w 的許可權, 這就是 mask 的功能.
 
**針對預設許可權的設定方式:
##設定規定:”d:[ug]: 使用者列表:[rwx]”
[root@sqj2015 ~]# mkdir acl_test1
[root@sqj2015 ~]# setfacl -m d:u:sqj:rw acl_test1/
[root@sqj2015 ~]# getfacl acl_test1
# file: acl_test1
# owner: root
# group: root
user::rwx
group::r-x
other::r-x
default:user::rwx
default:user:sqj:rw-
default:group::r-x
default:mask::rwx
default:other::r-x
 
[root@sqj2015 ~]# cd acl_test1
[root@sqj2015 acl_test1]# touch test
[root@sqj2015 acl_test1]# ll test
-rw-rw-r–+ 1 root root 0 Mar 21 14:16 test
[root@sqj2015 acl_test1]# getfacl test
# file: test
# owner: root
# group: root
user::rw-
user:sqj:rw-
group::r-x                      #effective:r–
mask::rw-
other::r–
##通過這個” 針對目錄來設定預設 ACL 許可權” 的選項, 我們可以讓這些屬性繼承到子目錄下面.