Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學

Linux ACL 權限控制實作教學:突破傳統 rwx 限制,精細管理檔案存取

編輯:XP 系統應用技巧

Linux 傳統權限模型(User/Group/Other)僅能針對單一擁有者、單一群組設定讀寫執行,無法針對「特定使用者」給予獨立權限。存取控制清單(ACL)解決此限制,允許對檔案/目錄設定多組使用者/群組權限,甚至限制 root。本文從核心支援檢查、掛載參數、setfacl/getfacl 實戰操作完整說明。

前置檢查:核心與檔案系統支援

# 檢查核心編譯選項(以 ext4 為例)
grep -i acl /boot/config-$(uname -r)
# 輸出應包含 CONFIG_EXT4_FS_ACL=y

若無支援,需重編核心或安裝支援 ACL 的核心模組。

掛載啟用 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

常用 ACL 指令速查

指令說明
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 內容

注意事項

  • ACL 權限受 mask 限制,實際生效權限 = ACL 項目 & mask。
  • 備份工具(tar、rsync)需加參數保留 ACL(tar --acls, rsync -A)。
  • NFS/Samba 共享需額外設定支援 ACL 傳遞。

ACL 讓 Linux 權限管理達到企業級細緻度,配合群組策略可實現複雜的多部門協作權限模型,是系統管理員必備技能。

Copyright © Windows教程網 All Rights Reserved