Linux 基礎權限(rwx、擁有者/群組/其他)無法滿足精細授權需求(如:單一用戶讀寫、其他群組唯讀)。ACL(Access Control List)提供檔案層級的擴充存取控制。本文從核心支援檢查、掛載啟用、setfacl/getfacl 實操到實戰案例,完整建立 ACL 運作認知。
grep -i acl /boot/config-$(uname -r)
# 需見 CONFIG_EXT3_FS_ACL=y (或 ext4/xfs/btrfs 相應選項)
# 無支援需重編核心或安裝 acl 套件 (kernel 2.6+ 多內建)
# 臨時
mount -t ext3 -o acl /dev/sda1 /fs1
# 永久:/etc/fstab
/dev/sda1 /fs1 ext3 defaults,acl 1 2
# 重新掛載生效
mount -o remount,acl /fs1
| 指令 | 用途 | 範例 |
|---|---|---|
setfacl -m u:user:perm file | 設定用戶 ACL | setfacl -m u:test2:rw test1.txt |
setfacl -m g:group:perm file | 設定群組 ACL | setfacl -m g:developers:rwx project/ |
setfacl -m m::perm file | 設定有效權限遮罩 | setfacl -m m::rx file |
setfacl -x u:user file | 移除特定用戶 ACL | setfacl -x u:test2 test1.txt |
setfacl -b file | 移除所有 ACL(保留基本權限) | |
getfacl file | 檢視完整 ACL | getfacl test1.txt |
ls -l | 基本權限尾部出現 + 表示有 ACL | -rw-rw-r--+ |
# 建立用戶
adduser test1; adduser test2; adduser test3
# 掛載 ACL 分區
mount -t ext3 -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-------
# test2 嘗試讀取 → Permission denied
# test1 給予 test2 讀寫權限
setfacl -m u:test2:rw test1.txt
# ls -l 顯示 -rw-rw-r--+ (尾部 + 表有 ACL)
getfacl test1.txt
# 輸出:
# user::rw-
# user:test2:rw- ← ACL 生效
# group::rw-
# mask::rw-
# other::r--
# test2 現在可讀寫
su - test2
cat test1.txt # 成功
echo "Modify by test2" >> test1.txt # 成功
# test3 仍無權限
su - test3
cat test1.txt # Permission denied
setfacl -d -m u:test2:rw /fs1/project
# 新建檔案自動獲得 test2:rw
# getfacl /fs1/project 顯示 default:user:test2:rw-
getfacl -R /fs1 > acl.backup
setfacl --restore=acl.backup
mask 限制除擁有者外所有 ACL 實際生效權限。chmod g-w file 會同步修改 mask。cp -a 保留 ACL,tar/rsync -a 需支援 ACL 選項(tar --acls、rsync -A)。CONFIG_NFS_V3_ACL/CONFIG_NFS_V4_ACL 並掛載 acl 選項。mask 限制(除非 CAP_DAC_OVERRIDE)。ACL 突破傳統 UGO 限制,實現用戶/群組級精細授權。核心支援 → 掛載啟用 → setfacl/getfacl 操作 → mask 理解 → 備份還原,五步建立完整 ACL 管理能力。小團隊協作、多租戶隔離、臨時授權場景,ACL 都是輕量靈活的最佳選擇。