每個檔案擁有類型與權限兩大屬性。檔案類型分五種:普通檔、目錄檔、連結檔、裝置檔、管道檔。權限包含讀、寫、執行。Linux 為多使用者系統,將檔案/目錄與使用者或群組關聯,透過 ACL (Access Control List) 實現比傳統 rwx 更精細的存取控制,甚至能限制 root 權限。
執行 cat /boot/config-$(uname -r) | grep -i ext3,確認輸出包含 CONFIG_EXT3_FS_ACL=y 等項目,表示核心已編譯 ACL 支援。若無,需重編核心或安裝 ACL 套件 (參考 acl.bestbits.at)。
臨時掛載:mount -t ext3 -o acl /dev/sda1 /fs1
永久生效:編輯 /etc/fstab,在對應分區掛載選項加入 acl,例如:/dev/sda1 /fs1 ext3 defaults,acl 0 0。
# 建立三個測試帳號
adduser test1; adduser test2; adduser test3
passwd test1; passwd test2; passwd test3
# 掛載測試分區
mount -t ext3 -o acl /dev/sda1 /fs1
chmod -R 777 /fs1 # 先給予完整權限方便測試
# test1 建立檔案並移除其他人權限
su - test1
cd /fs1
echo "Create by test1" > test1.txt
chmod go-r test1.txt # 只有 test1 可讀寫
# test1 透過 ACL 授予 test2 讀寫權限
setfacl -m u:test2:rw test1.txt
# 查看 ACL 屬性 (注意權限字串尾端的 + 號)
getfacl test1.txt
# 輸出範例:
# user::rw-
# user:test2:rw-
# group::rw-
# mask::rw-
# other::r--
# test2 現在可讀寫該檔案
su - test2
cat /fs1/test1.txt
echo "Modify by test2" >> /fs1/test1.txt
# test3 仍無權限
su - test3
cat /fs1/test1.txt # Permission denied
| 指令 | 用途 |
|---|---|
setfacl -m u:使用者:權限 檔案 | 設定使用者 ACL 權限 |
setfacl -m g:群組:權限 檔案 | 設定群組 ACL 權限 |
getfacl 檔案 | 檢視檔案 ACL 設定 |
setfacl -x u:使用者 檔案 | 移除指定使用者 ACL |
setfacl -b 檔案 | 移除所有 ACL 設定 |
ACL 讓 Linux 檔案權限管理達到企業級精細度,遠超 Windows 預設的簡單權限模型。掌握 ACL 是 Linux 系統管理進階的關鍵技能。