Linux 傳統權限模型(User/Group/Other)僅能針對單一擁有者、單一群組設定讀寫執行,無法針對「特定使用者」給予獨立權限。存取控制清單(ACL)解決此限制,允許對檔案/目錄設定多組使用者/群組權限,甚至限制 root。本文從核心支援檢查、掛載參數、setfacl/getfacl 實戰操作完整說明。
# 檢查核心編譯選項(以 ext4 為例)
grep -i acl /boot/config-$(uname -r)
# 輸出應包含 CONFIG_EXT4_FS_ACL=y
若無支援,需重編核心或安裝支援 ACL 的核心模組。
掛載時加入 acl 參數:
# 臨時掛載
mount -t ext4 -o acl /dev/sda1 /fs1
# 永久生效:編輯 /etc/fstab
/dev/sda1 /fs1 ext4 defaults,acl 0 2
# 建立測試用戶
useradd test1; useradd test2; useradd test3
passwd test1; passwd test2; passwd test3
# 掛載測試分區
mount -t ext4 -o acl /dev/sda1 /fs1
chmod 777 /fs1
# test1 建立檔案並移除他人權限
su - test1
cd /fs1
echo "Create by test1" > test1.txt
chmod go-r test1.txt # 僅擁有者可讀寫
# -rw------- 1 test1 test1 17 Jul 14 22:11 test1.txt
# test1 針對 test2 授予讀寫權限(ACL)
setfacl -m u:test2:rw test1.txt
# 查看 ACL 屬性(注意尾端 + 號)
ls -l test1.txt
# -rw-rw-r--+ 1 test1 test1 10 Feb 16 13:52 test1.txt
getfacl test1.txt
# file: test1.txt
# owner: test1
# group: test1
# user::rw-
# user:test2:rw- ← ACL 項目
# group::rw-
# mask::rw-
# other::r--
# 驗證:test2 可讀寫,test3 拒絕
su - test2
cat /fs1/test1.txt # 成功
echo "Modify by test2" >> /fs1/test1.txt # 成功
su - test3
cat /fs1/test1.txt # Permission denied
| 指令 | 說明 |
|---|---|
setfacl -m u:user:perm file | 設定使用者權限(perm: r/w/x) |
setfacl -m g:group:perm file | 設定群組權限 |
setfacl -x u:user file | 移除使用者 ACL 項目 |
setfacl -b file | 移除所有 ACL 項目 |
setfacl -d -m u:user:perm dir | 設定預設 ACL(新建檔案繼承) |
getfacl file | 顯示 ACL 內容 |
mask 限制,實際生效權限 = ACL 項目 & mask。tar --acls, rsync -A)。ACL 讓 Linux 權限管理達到企業級細緻度,配合群組策略可實現複雜的多部門協作權限模型,是系統管理員必備技能。