Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

Linux 入門:ACL 文件訪問控制列表從概念到實戰

為什麼需要 ACL

Linux 傳統的九位基本權限(rwx)加上三種特殊權限(SUID、SGID、Sticky),在場景簡單時隨手調整就夠用了。可一旦遇到「多個組、多個成員,各自需要不同權限」的局面,傳統模型立刻捉襟見肘:你沒辦法只給某幾個人開小門,而不動搖其他人。為了解決這類困境,內核引入了 ACL(Access Control List,訪問控制列表)模塊,把權限管理變成分層、可叠加的條目式配置。

文件系統支持情況

RedHat 7、CentOS 7 默認建立的 xfs 與 ext4 文件系統自帶 ACL 能力,开箱即用。而 RedHat 5、6 這類較老版本上新格式化的文件系統,可能需要手動開啟:

tune2fs -o acl /dev/to/path    # 直接對設備啟用內部 ACL 支持
mount -o acl /dev/to/path /mnt  # 或者在掛載時附加該功能

ACL 的判定優先級

優先級從高到低依次為:所有者 → 自定義用戶條目 → 自定義組條目 → 其他人。這個順序解釋了很多「看起來不合理」的現象,後面會有實例。

查看 ACL:getfacl

getfacl <FILE|DIR>

先看一個未設置 ACL 的文件:

[root@mzf facldir]# ls -l file1
-rw-r--r--. 1 root root 0 Aug 4 08:57 file1

權限位末尾那個 .,表示此文件沒有任何 ACL 條目。

[root@mzf facldir]# getfacl file1
# file: file1
# owner: root
# group: root
user::rw-
group::r--
other::r--

再看一個設置過 ACL 的文件:

[root@mzf facldir]# ls -l file2
-rw-rw-r--+ 1 root root 0 Aug 4 09:02 file2

末尾的 + 就是標誌——這個文件已經被追加了 ACL 條目。

[root@mzf facldir]# getfacl file2
# file: file2       # 文件名
# owner: root       # 文件所有者
# group: root       # 文件所屬組
user::rw-           # 屬主權限
user:tom:rw-        # 額外添加的指定用戶 tom
group::r--          # 屬組權限
mask::rw-           # 除 owner、other 外,其他用戶權限不得超過此上限
other::r--          # 其他人權限

除了屬主 root,這裡多出了一個 tom 用戶,同樣擁有讀寫權限——這正是 ACL 的功勞。順帶留意 mask 這一項,它像一道天花板,所有具名用戶與具名組的有效權限都要與它取交集。

設置 ACL:setfacl

setfacl [option] [action] [file|dir]

常用 option:

-m 設置特定條目;-x 刪除特定條目;-b 清空指定文件的全部 ACL;-R 遞歸處理目錄下的所有文件;-k 清除默認(default)ACL;-M FILE 從文本文件批量讀入條目;-X FILE 從文本文件讀入並匹配刪除。

action 的寫法:

u:uname:### 為指定用戶設置;g:gname:### 為指定組設置;m::### 設置 mask 上限;d:[action] 為目錄設置默認 ACL,使其下新建文件自動繼承。

實戰示例

為單一用戶添加條目:

[root@mzf facldir]# setfacl -m u:tom:rw file1
[root@mzf facldir]# getfacl file1
user::rw-
user:tom:rw-
group::r--
mask::rw-
other::r--

一次添加多個用戶:

[root@mzf facldir]# setfacl -m u:tom:rw,u:wangcai:w file1

用戶與組混搭,權限各不相同:

[root@mzf facldir]# setfacl -m g:it:rw,u:mzf:rw,u:tom:w file2
[root@mzf facldir]# getfacl file2
user::rw-
user:mzf:rw-
user:tom:-w-
group::r--
group:it:rw-
mask::rw-
other::r--

同時刪除用戶與組條目:

[root@mzf facldir]# setfacl -x u:mzf:,g:it file2

刪除後 getfacl 可見,mzf 與 it 的條目消失,tom 的條目被保留。

親眼驗證優先級

[root@mzf facldir]# su tom
[tom@mzf facldir]$ cat file2
cat: file2: Permission denied

other 明明有 r 權限,tom 卻讀不到文件。原因就在優先級:tom 一旦成為具名 ACL 條目,就不再走 other 分支,而他手裡只有 w。這也說明,在開啟 ACL 的文件系統上,ACL 條目的優先級高於傳統的 other 判定。

遞歸設置與繼承

[root@mzf facldir]# setfacl -R -m u:mzf:rwx /testdir/facldir/

遞歸之後,目錄及其下已有的文件、子目錄都會寫入相同條目;但此後新建的文件不會自動繼承。要實現「新建即繼承」,得靠默認 ACL:

[root@mzf facldir]# setfacl -m d:u:tom:rwx /testdir/facldir/
[root@mzf facldir]# touch file4 file5
[root@mzf facldir]# ls -l file4 file5
-rw-rw-r--+ 1 root root 0 Aug 4 11:04 file4
-rw-rw-r--+ 1 root root 0 Aug 4 11:04 file5

新建文件權限位末尾自動出現 +,說明繼承生效。要注意的是,普通文件不會繼承 x 權限,這是一道安全防線。

常用格式速查

setfacl -m u:uname:rwx /path/to/file   # 設置單一用戶條目
setfacl -x u:uname /path/to/file       # 刪除單一用戶條目
setfacl -m g:gname:rwx /path/to/file   # 設置單一用戶組條目
setfacl -x g:gname: /path/to/file      # 刪除單一用戶組條目
setfacl -m u:uname:rwx,g:gname:rx /path/to/file   # 一次設置多條目
setfacl -x g:gname:,u:uname /path/to/file         # 一次刪除多條目
setfacl -k /path/to/file               # 清除所有默認 ACL
setfacl -b /path/to/file               # 刪除全部 ACL
setfacl -Rm g:gname:rwX /path/to/dir   # 遞歸設置,普通文件不會帶 x
setfacl -M file.acl /path/to/file      # 從文件批量導入條目
setfacl -X file.acl /path/to/file      # 從文件匹配刪除條目
getfacl /path/to/file1 | setfacl --set-file=- /path/to/file2  # 複製權限
setfacl -m mask::rw /path/to/file      # 設置 mask
setfacl --set u:uame:rwx /path/to/file # 重置全部並加入一條

案例:備份目錄的 ACL 條目

getfacl -R /tmp/dir1 > acl.txt
setfacl -R -b /tmp/dir1
setfacl -R --set-file=acl.txt /tmp/dir1
getfacl -R /tmp/dir1
cp -a -r /tmp/dir1 /tmp/dir1.bak

案例:協作目錄的權限規劃

需求:/data/testdir 下新建的文件自動歸屬 g1 組;g2 成員(如 alice)可讀寫;g3 成員(如 tom)只可讀;其他人完全不能訪問。

mkdir -p /data/testdir
groupadd g1; groupadd g2; groupadd g3
chown :g1 /data/testdir
chmod g+s /data/testdir
gpasswd -a alice g2
gpasswd -a tom g3
setfacl -m g:g2:rw,g:g3:r /data/testdir
chmod o= /data/testdir

這套組合拳的思路很清晰:SGID 負責讓新建文件自動繼承組身份,ACL 負責為不同組切分粗細不同的權限,最後收緊 other 兜底。三者各司其職,比單純堆 chmod 要優雅得多。

Copyright © Linux教程網 All Rights Reserved