Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> 關於Linux

Linux 進階權限 ACL:getfacl 與 setfacl 實作

傳統的 owner/group/other 三組 rwx 權限在多人共用主機時常不夠用,例如想讓某個特定使用者單獨具備讀取權,卻不想動到 group。這時 ACL(Access Control List)就是答案:它能針對單一檔案或目錄,為指定使用者或群組設定額外的讀、寫、執行權限。

兩個工具

  • getfacl:查詢檔案或目錄目前的 ACL 設定。
  • setfacl:設定或修改 ACL 項目。

實作示範

## 建立測試檔案
[jimistephen@localhost acl]$ touch acl_test

## 檢視基本權限
[jimistephen@localhost acl]$ ll acl_test
-rw-rw-r--. 1 jimistephen jimistephen 0 Mar 10 08:21 acl_test

## 針對使用者 jimi 加上 r-x
[jimistephen@localhost acl]$ setfacl -m u:jimi:rx acl_test
[jimistephen@localhost acl]$ ll acl_test
-rw-rwxr--+ 1 jimistephen jimistephen 0 Mar 10 08:21 acl_test

注意權限欄位尾端多了一個 +,這就是「此檔設有 ACL」的標記。

## 不指定使用者名單時,改的是擁有者本身
[jimistephen@localhost acl]$ setfacl -m u::rwx acl_test
[jimistephen@localhost acl]$ ll acl_test
-rwxrwxr--+ 1 jimistephen jimistephen 0 Mar 10 08:21 acl_test

讀取 ACL 內容

[jimistephen@localhost acl]$ getfacl acl_test
# file: acl_test
# owner: jimistephen
# group: jimistephen
user::rwx        # 擁有者權限
user:jimi:r-x    # 特定使用者 jimi 的權限
group::rw-       # 群組權限
mask::rwx        # ACL 的有效遮罩,實際生效的權限不可超過它
other::r--       # 其他人權限

mask 是最容易被忽略的一欄:即使 ACL 給了 rwx,只要 mask 較嚴格,實際生效的權限仍以 mask 為上限。

讓權限被子目錄繼承

# 只改變該目錄本身
setfacl -m u:myuser1:rx /srv/projecta

# 加上 d: 前綴,讓此目錄下新建的檔案與子目錄都繼承這條 ACL
setfacl -m d:u:myuser1:rx /srv/projecta

專案共用目錄很適合用預設 ACL 處理,新進的檔案不必再逐一補權限。要移除 ACL 則用 setfacl -x u:jimi acl_test,清空全部則是 -b。

Copyright © Linux教程網 All Rights Reserved