antony@notes:~/suse$ cat "Identity-and-Security.md"
Identity and Security
Identity and Security
:::warning
:::spoiler 目錄
[TOC]
:::
Use ACLs
要使用 ACLs 必須熟悉
- ACLs 如何運作
- ACL 術語
- ACL 種類
- ACLs 和 權限 如何相互對應
- 使用 ACL 命令工具
- 透過 Access ACL 設定目錄
- 透過 Default ACL 設定目錄
- 使用更多的 setfacl 選項
- ACL Check Algorithm
- How Applications Handle ACLs
ACLs 如何運作
ACL 提供了三組傳統權限的擴展:
權限 :
Read (r)Write (w)Execute (x)
Applied to :
- Owner
- Group
- Others
ACLs 在 Linux 中是 Access Control Lists 的縮寫。ACLs 是附加在 Linux 檔案系統中的檔案或目錄上的一組權限,用於決定誰可以存取和執行操作。
ACL 允許您將權限分配給 “一位用戶” 或 “一個群組”,即使他們不是所有者或所屬群組的成員
傳統上,Linux 系統上的每個文件對像都定義了三組權限。 這些集合包括三種類型用戶中每一種的read (r), write (w), and execute (x) 權限:
- User (file owner)
- Group
- Other authenticated users
這個概念適用於大多數實際情況。 然而,在過去,對於更複雜的場景或高級應用,系統管理員不得不使用一些技巧來規避傳統權限概念的局限性。 訪問控制列表提供了傳統文件權限概念的擴展。 它們允許您將權限分配給單個用戶或組,即使這些用戶或組不對應於原始所有者或擁有群組。
ACL 是 Linux 內核的一項功能,受 Ext2/3/4、XFS 和 BtrFS 文件系統(and others, but those listed are most relevant for SLE12)支持。 使用 ACL,您可以創建複雜的場景,而無需在應用程序級別實施複雜的權限模型。
The advantages of ACLs are clearly evident in situations like replacing a Windows server with a Linux server providing file and print services with Samba. Since Samba supports ACLs, user permissions can be configured both on the Linux server and in Windows.
Samba 是 Linux 中的一個服務,它允許 Linux 和 Windows 系統之間進行文件共享。通過 Samba,Linux 可以充當文件共享服務器,允許 Windows 使用者在網絡上訪問和共享 Linux 上的文件和印表機。
Samba 支持許多安全功能,例如身份驗證和加密,以保護共享文件的安全。在 Linux 中,通常使用 Samba 來實現與 Windows 系統之間的互通性。
ACL Example
# 新增一個名為 file 的空檔案
> touch file
# 檢查
> ls -l
total 0
-rw-r--r-- 1 rancher users 0 Mar 16 19:47 file
# 將可讀寫的權限套在 jsmith 使用者身上
> sudo setfacl -m u:jsmith:rw file
# 顯示檔案或目錄的存取控制列表(ACL)
> getfacl file
# file: file
# owner: rancher
# group: users
user::rw-
user:jsmith:rw-
group::r--
mask::rw-
other::r--
# ls 的輸出會多一個 +
> ls -l
total 0
-rw-rw-r--+ 1 rancher users 0 Mar 16 19:47 fileACL Types
Two basic classes of ACLs:
- Minimal ACLs (for practical purposes: same as POSIX permissions)
- Extended ACLs
ACLs extend the classic Linux file permissions:
- Named user –> 指定使用者
- Named group –> 指定群組
- Mask –> 權限
What is Minimal ACLs
ACLs equivalent with the file mode permission bits are called minimal ACLs.
They have three ACL entries for the types:
- owning user
- owning group
- other.
These correspond to the conventional permission bits for files and directories.
What is Extended ACLs
ACLs with more than the three entries are called extended ACLs.
Extended ACLs also contain a mask entry and may contain any number of named user and named group entries.
ACLs extend the classic Linux file permission model by the following:
- Named user: Lets you assign permissions to individual users.
- Named group: Lets you assign permissions to individual groups.
- Mask: Lets you limit the permissions granted to named users or groups.
These named group and named user entries are assigned to the group class, which already contains the owning group entry.
ACL Terminology(術語)
Important ACL terms include:
- User classes
- Owner class
- Group class
- Other class
- Access ACL
- Default ACL
- ACL entry
What is User classes
The conventional POSIX permission concept uses three classes of users for assigning permissions in the file system:
- the owning user
- the owning group
- other users.
Three permission bits can be set for each user class, giving permission to read (r), write (w), and execute (x).
What is Access ACL
Determine access permissions for users and groups for all kinds of file system objects (files and directories).
我的理解 :
- 確認使用者或群組對於檔案或目錄的存取權限
What is Default ACL
Can be applied only to directories. They determine the permissions a file system object inherits from its parent directory when it is created.
我的理解 :
- 為目錄設置 ACL 繼承的權限,這樣可以讓該目錄中任何使用者創建的任何子目錄或檔案都將繼承父目錄的權限。
- 在指定的目錄下,為新建立的檔案或目錄指定預設的存取權限
What is ACL entry
Each ACL consists of a set of ACL entries. An ACL entry contains a type, a qualifier for the user or group the entry refers to, and a set of permissions. For some entry types, the qualifier for the group or users is undefined.
- 類型 (type):代表該 ACL 記錄的類型,可以是 “user”、“group” 或 “mask”。
- 限定詞 (qualifier):用於指定該 ACL 記錄參考的使用者或群組。當類型為 “user” 時,限定詞指定了使用者;當類型為 “group” 時,限定詞指定了群組。
- 權限 (permissions):一組用於定義特定使用者或群組對該檔案或目錄的存取權限。
例如,如果想要讓使用者 “john” 對 /home/myfolder 目錄具有讀、寫和執行權限,您可以使用以下命令設定 ACL 記錄:
setfacl -m user:john:rwx /home/myfolder在這個例子中,user 代表 ACL 記錄的類型是使用者,john 代表限定詞是使用者 “john”,rwx 代表這個使用者對 /home/myfolder 目錄具有讀、寫和執行權限。
ACLs 和 權限 如何相互對應
What is mask in ACL
限制使用者和群組可以得到的最高權限
例如 : 如果一位使用者被授予 rwx 的權限,但是 mask 這時候設定的是只允許 r 的權限,那麼這時候這位使用者實際上只會有讀取的權限。
下面的例子中,透過 getfacl 來查看 jsmith 這位使用者對於 test/haha/ 這個目錄有 rw 的權限,但由於 mask 設定只有 r 的權限,所以 jsmith 這位使用者,實際上只會有讀取的權限。
> getfacl test/haha/
# file: test/haha/
# owner: rancher
# group: users
user::rwx
user:jsmith:rw- #effective:r--
group::r-x #effective:r--
mask::r--
other::r-xmask 如何得到權限 ?
預設情況下, “mask 的權限” = “user(使用者) 的權限” + “group(群組) 的權限”
範例 :
# 建立 haha 目錄
> mkdir haha
# 更改目錄權限為 r--,r--,---
> chmod 440 haha
# 將使用者 jsmith 對於 haha 目錄的權限設定為 rw-
> setfacl -m u:jsmith:rw- haha
# 檢查,可以發現使用者(r--,rw-) + 群組(r--) 總和 rw- 會等於 mask 的權限
> getfacl haha
# file: haha
# owner: rancher
# group: users
user::r--
user:jsmith:rw-
group::r--
mask::rw-
other::---Mask during Default ACL
- Default ACL 應用在目錄上,因此在目錄中創建的新目錄和檔案將自動獲得應用於它的 ACL。
- 如果建立的是目錄的話,它會繼承父目錄的 Default ACL 和 Access ACL。
- Mask 也將與父目錄的 Default ACL 中的 Mask 相同
- 如果建立的是檔案的話,父目錄的 Default ACL 將變成檔案的 Access ACL。但是如果 Default ACL 對使用者具有執行權限。它不會被繼承到新創建的檔案中。 mask 也是如此
建立的目錄的範例 :
# 建立 black-box 目錄
> mkdir black-box
# 更改 black-box 目錄權限為 r--,r--,---
> chmod 440 black-box/
# 設定使用者 jsmith 對於 black-box/ 目錄具有 rw- 的權限
> setfacl -m u:jsmith:rw- black-box/
# 設定 wheel 群組對於 black-box/ 目錄具有 r-x 的權限
# 並且設為 Default ACL
> setfacl -m d:g:wheel:r-x black-box/
# 檢視 black-box/ 目錄的 ACL
> getfacl black-box/
# file: black-box/
# owner: rancher
# group: users
user::r--
user:jsmith:rw-
group::r--
## user(r--,rw-) + group(r--) = mask(rw-)
mask::rw-
other::---
default:user::r--
default:group::r--
default:group:wheel:r-x
## user(r--) + group(r--,r-x) = mask(r-x)
default:mask::r-x
default:other::---
# 建立子目錄 teams
> sudo mkdir black-box/teams
# 檢視 teams 的 ACL
> sudo getfacl black-box/teams
# file: black-box/teams
# owner: root
# group: root
user::r--
group::r--
group:wheel:r-x
mask::r-x
other::---
default:user::r--
default:group::r--
default:group:wheel:r-x
default:mask::r-x
default:other::---建立的檔案的範例(延續上一個範例) :
# 在 black-box/ 目錄下建立名為 notepad 的空檔案
> sudo touch black-box/notepad
# 檢查 notepad 的 ACL
> sudo getfacl black-box/notepad
# file: black-box/notepad
# owner: root
# group: root
user::r--
group::r--
group:wheel:r-x #effective:r--
## Default ACL(包括 mask)有被繼承。
## 但是 mask 的執行權限不會從 Default ACL 中繼承
mask::r--
other::---由上下兩個範例可以看到,Default ACL 包括父目錄的 mask 會被轉移到新建的檔案中。但是 mask 和 使用者的執行權限不會轉移(被繼承)。
# 建立 test 目錄
> mkdir test
# 更改 test 目錄權限為 r--,r--,---
> chmod 440 test/
# 設定使用者 jsmith 對於 test 目錄具有 rwx 的權限
# 並且設為 Default ACL
> setfacl -d -m u:jsmith:rwx test/
# 檢視 test/ 目錄的 ACL
> getfacl test/
# file: test/
# owner: rancher
# group: users
user::r--
user:jsmith:rw-
group::r--
mask::rw-
other::---
default:user::r--
default:user:jsmith:rwx
default:group::r--
default:mask::rwx
default:other::---
# 在 test 目錄下建立名為 haha 的空檔案
> sudo touch test/haha
# 檢視 haha 空檔案的 ACL
> sudo getfacl test/haha
# file: test/haha
# owner: root
# group: root
user::r--
user:jsmith:rwx #effective:rw-
group::r--
mask::rw-
other::---Change ACL mask
- 使用
chmod並且設定群組的權限 - 使用
setfacl
Using chmod
當更改檔案或目錄群組的權限時,會直接更改 mask
> mkdir haha
> setfacl -m u:jsmith:rwx haha
> getfacl haha/
# file: haha/
# owner: rancher
# group: users
user::rwx
user:jsmith:rwx
group::r--
mask::rwx
other::---
> chmod 740 haha
> getfacl haha/
# file: haha/
# owner: rancher
# group: users
user::rwx
user:jsmith:rwx #effective:r--
group::r--
mask::r--
other::---Using setfacl
語法 : setfacl -m m::<權限> <目錄/檔案>
> setfacl -m m::r-x haha/
> getfacl haha/
# file: haha/
# owner: rancher
# group: users
user::rwx
user:jsmith:rwx #effective:r-x
group::r--
mask::r-x
other::---
