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

Linux 使用 setfacl/getfacl 為單一檔案設定多組群組權限

編輯:Linux 系統常見問題解答

前言

Linux 傳統權限模型(User/Group/Others)僅支援單一群組。當需要針對同一檔案給予多個不同群組不同權限時,需使用 ACL (Access Control List) 機制。本文透過實戰示範 setfacl 設定與 getfacl 查看多群組權限。

環境資訊

  • 測試平台:Ubuntu 3.2.0-58-generic-pae
  • Shell:GNU bash 4.2.39
  • 測試檔案:testacl(擁有者 v0id6:v0id6)

操作實錄

1. 查看初始 ACL 狀態

$ ls -la testacl
-rw-rw-r--+ 1 v0id6 v0id6 9  Mar  2 14:21 testacl   # 尾端 '+' 表示有 ACL 擴充屬性

$ getfacl testacl
# file: testacl
# owner: v0id6
# group: v0id6
user::rw-
group::r--
other::r--

2. 新增兩個群組的讀寫權限

$ setfacl -m g:v0id6:rw,g:yygydjtest:rw testacl
$ getfacl testacl
# file: testacl
# owner: v0id6
# group: v0id6
user::rw-
group::r--
group:v0id6:rw-
group:yygydjtest:rw-
mask::rw-
other::r--

關鍵點:

  • -m:修改 ACL 規則
  • g:群組名:權限:指定群組權限格式
  • mask:自動生成,限制群組與命名使用者的最大有效權限

3. 移除特定群組權限

$ setfacl -x g:yygydjtest testacl
$ getfacl testacl
# file: testacl
# owner: v0id6
# group: v0id6
user::rw-
group::r--
group:v0id6:rw-
mask::rw-
other::r--

-x 參數可精確刪除指定的 ACL 條目。

進階補充

查詢系統現有群組

cat /etc/group

新增群組

groupadd newgroup

遞迴套用至目錄(含預設 ACL)

# 設定目錄及其未來新建檔案繼承 ACL
setfacl -R -m g:team:rw /project/shared
setfacl -R -d -m g:team:rw /project/shared

備份與還原 ACL

# 備份
getfacl -R /project > acl_backup.txt

# 還原
setfacl --restore=acl_backup.txt

常見問題排查

  • 掛載選項:檔案系統必須支援 ACL(ext4/xfs/btrfs 預設開啟)。若無效,檢查 /etc/fstab 掛載選項是否包含 acl。
  • 權限不生效:確認 mask 值未限制過低(mask::r-- 會導致 rw- 實際只能讀取)。
  • cp/mv 行為:cp -p 保留 ACL,mv 同檔案系統內移動保留 ACL,跨檔案系統視為複製+刪除。
Copyright © Windows教程網 All Rights Reserved