訪問控制列表
訪問控制列表 (ACL) 為文件系統提供了一種額外的、更加靈活的權限機制,用於完善 UNIX 的文件權限機制。ACL 允許您將任意磁碟資源授權給任意用戶組或用戶。
acl包 是 systemd 的依賴之一,應該已經安裝好了。
要啟用 ACL,文件系統在掛載時必須要加入 acl 參數。要開機時自動啟用 ACL,你可以通過編輯 fstab 文件來實現。
默認情況下 Btrfs、Ext2/3/4 已經啟用了 acl 參數,因此無需額外指定。對於 Ext* 文件系統你可以使用以下命令檢查 acl 是否真的已經啟用:
# tune2fs -l /dev/sdXY | grep "Default mount options:"
Default mount options: user_xattr acl
還需要檢查 /proc/mounts 文件中是否存在 noacl 。以防止默認掛載選項被覆蓋。
你可以通過 tune2fs -o option partition 命令設置文件系統的默認掛載選項。例如:
# tune2fs -o acl /dev/sdXY
這將導致此磁碟以後在被其他 Linux 掛載時依然會使用相同的掛載選項。
-
acl選項在創建 ext2/3/4 文件系統時將被默認啟用,該行為由/etc/mke2fs.conf控制。 -
/proc/mounts文件中不會列出默認掛載選項。
ACL 可以通過 setfacl 命令被設置。
- 你可以通過添加
--test參數來測試該命令的結果 - 要遞歸地將操作應用到所有的文件和目錄上,使用
-R/--recursive參數。
為用戶設定權限(user 是用戶名或 UID):
# setfacl -m "u:user:permissions" <file/dir>
為用戶組設定權限 (group是用戶組名或 GID):
# setfacl -m "g:group:permissions" <file/dir>
為其他用戶設定權限:
# setfacl -m "other:permissions" <file/dir>
允許所有新創建的文件或目錄從父目錄繼承條目(這不會影響將複製到目錄中的文件):
# setfacl -dm "条目" <目录>
刪除權限:
# setfacl -x "用户/用户组" <文件/目录>
刪除默認權限
# setfacl -k <文件/目录>
刪除所有ACL權限(擁有者、用戶組和其他用戶的權限將被保留)
# setfacl -b <file/dir>
--mask entry was explicitly given. The mask entry indicates the maximum permissions allowed for users (other than the owner) and for groups. Unless explicitly set, this will match the permissions of the default group. To clarify what this means, suppose the group owning a directory has r-x permissions. If you add an ACL user or group with rwx permissions, the effective permissions of this user or group will be r-x. The reason for this is so that there are no surprises when a file from a system which does not support ACLs is made available on a system which does..要顯示 ACL 權限,使用:
# getfacl <file/dir>
給予 johnny 對 abc 文件的所有訪問權:
# setfacl -m "u:johnny:rwx" abc
檢查權限:
# getfacl abc
# file: abc # owner: someone # group: someone user::rw- user:johnny:rwx group::r-- mask::rwx other::r--
改變 johnny 的權限:
# setfacl -m "u:johnny:r-x" abc
檢查權限:
# getfacl abc
# file: abc # owner: someone # group: someone user::rw- user:johnny:r-x group::r-- mask::r-x other::r--
刪除所有已添加的 ACL 權限信息:
# setfacl -b abc
檢查權限:
# getfacl abc
# file: abc # owner: someone # group: someone user::rw- group::r-- other::r--
當使用 ls -l 命令時,含有ACL權限的文件將會顯示一個 +
$ ls -l /dev/audio
crw-rw----+ 1 root audio 14, 4 nov. 9 12:49 /dev/audio
$ getfacl /dev/audio
getfacl: Removing leading '/' from absolute path names # file: dev/audio # owner: root # group: audio user::rw- user:solstice:rw- group::rw- mask::rw- other::---
下面的例子演示了如何對 WEB 伺服器這樣的進程賦予其訪問用戶目錄的權限,以便防止其越權處理文件。
假設運行該 WEB 伺服器進程的用戶為 http,授予其訪問 /home/geoffrey 的權限。
首先授予 http 執行權限:
# setfacl -m "u:http:--x" /home/geoffrey
由於 http 現在已經對 /home/geoffrey 擁有執行權,下面移除 other 對 /home/geoffrey 的權限:
# chmod o-rx /home/geoffrey
使用getfacl 查看更改:
$ getfacl /home/geoffrey
getfacl: Removing leading '/' from absolute path names # file: home/geoffrey # owner: geoffrey # group: geoffrey user::rwx user:http:--x group::r-x mask::r-x other::---
正如上面的輸出所示,other 不再具有任何權限,但是用戶 http 仍然能夠訪問文件,因此可以認為安全性有所提高。
http 授予特定目錄或文件的寫訪問權:
# setfacl -dm "u:http:rwx" /home/geoffrey/project1/cache