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

Linux ACL 權限管理實戰:讓檔案存取控制更精細

編輯:Windows XP 系統介紹

Linux 傳統的 rwx 權限僅能針對「擁有者、群組、其他人」三類主體設定,無法針對單一使用者給予特殊權限。ACL(Access Control List)解決了這個痛點,支援對個別使用者或群組設定獨立權限,甚至能限制 root。以下為啟用與操作完整流程。

1. 確認核心與檔案系統支援

# grep -i acl /boot/config-$(uname -r)
CONFIG_EXT4_FS_ACL=y   # 霵為 y 表示已編譯支援

若無支援,需重編核心或安裝 acl 套件(apt install acl / yum install acl)。

2. 掛載時啟用 ACL

臨時掛載:

mount -t ext4 -o acl /dev/sda1 /fs1

永久生效:編輯 /etc/fstab,在對應分區選項加入 acl:

UUID=xxxx  /fs1  ext4  defaults,acl  0  2

3. 實操範例:針對特定使用者授權

# 建立測試使用者
useradd test1; useradd test2; useradd test3
echo "pass" | passwd --stdin test1
echo "pass" | passwd --stdin test2
echo "pass" | passwd --stdin test3

# test1 建立檔案並移除群組/他人權限
su - test1 -c 'cd /fs1 && echo "Create by test1" > test1.txt && chmod 600 test1.txt'

# 僅授予 test2 讀寫權限
su - test1 -c 'setfacl -m u:test2:rw /fs1/test1.txt'

# 驗證
getfacl /fs1/test1.txt
# 輸出會顯示 user:test2:rw- 且檔案尾部出現 + 號,表示已套用 ACL

# test2 可讀寫,test3 被拒絕
su - test2 -c 'cat /fs1/test1.txt && echo "Modify by test2" >> /fs1/test1.txt'
su - test3 -c 'cat /fs1/test1.txt'   # Permission denied

常用指令速查

  • setfacl -m u:user:perm file:設定使用者 ACL
  • setfacl -m g:group:perm file:設定群組 ACL
  • setfacl -x u:user file:移除指定使用者 ACL
  • setfacl -b file:移除所有 ACL
  • getfacl file:檢視 ACL

善用 ACL,即可在不更改傳統權限位的前提下,實現精細到「單一使用者/群組」的存取控制,滿足多使用者共存環境的安全需求。

Copyright © Windows教程網 All Rights Reserved