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

Linux 權限完全指南:從 rwx 到 ACL 與 setUID

先把權限這件事想清楚

Linux 的權限體系看起來簡單,真正上手卻常讓人栽跟頭。最典型的場景莫過於用 sudo 建立檔案之後,自己反而動不了它——因為 sudo 代表以 root 身分執行,新建的檔案與目錄擁有者自然是 root,而原本的你只是「其他使用者」。

基本權限只有三組、三種:

  • r(read):讀取,數值 4
  • w(write):寫入、修改、刪除,數值 2
  • x(execute):執行,數值 1

三組分別對應 擁有者(owner)、所屬群組(group)、其他人(other)。所以 750 的意思就是:擁有者 7(rwx)、群組 5(r-x)、其他人 0(---)。若不希望其他人讀取,chmod 750 就能解決。

但真實需求往往更細:我希望某個特定使用者或某個特定群組能存取,其他人一律不行。傳統九碼權限做不到這件事,這時就輪到 ACL(Access Control List) 登場。

一、把權限交給特定的人

以下示範如何針對使用者 st 開放 project/ 目錄:

//建立目錄
mkdir project
chmod 770 project/

//建立兩個使用者並加入群組
useradd bimm
useradd cangls
groupadd tgroup
gpasswd -a bimm tgroup
gpasswd -a cangls tgroup
chown root:tgroup project/

//授予使用者 st 權限
useradd st
setfacl -m u:st:rx project/

設定完成後,ll 的權限欄尾端會多出一個 + 號,代表這個檔案帶有 ACL:

[root@bogon temp]# ll -d project/
drwxrwx---+ 2 root tgroup 16 5月 14 21:14 project/

[root@bogon temp]# getfacl project/
# file: project/
# owner: root
# group: tgroup
user::rwx
user:st:r-x
group::rwx
mask::rwx
other::---

若要授權給一整個群組(例如 tgroup2),把 u: 換成 g: 即可:

[root@bogon temp]# setfacl -m g:tgroup2:rwx project/
[root@bogon temp]# getfacl project/
# file: project/
# owner: root
# group: tgroup
user::rwx
user:st:r-x
group::rwx
group:tgroup2:rwx
mask::rwx
other::---

二、mask:決定「實際生效」的上限

用 setfacl 授權時,寫下的權限並不一定等於真正生效的權限,它會先被 mask 攔一道。舉例來說,若執行 setfacl -m u:st:rw project,而該目錄的 mask 是 r-x,那麼 st 實際拿到的只有 r--。

反過來,mask 本身也是可以調整的:

[root@bogon temp]# setfacl -m u:st:rw project/
[root@bogon temp]# getfacl project/
user::rwx
user:st:rw-
group::rwx
group:tgroup2:rwx
mask::rwx
other::---

[root@bogon temp]# setfacl -m m:r-x project/
[root@bogon temp]# getfacl project/
user::rwx
user:st:rw-      #effective:r--
group::rwx       #effective:r-x
group:tgroup2:rwx #effective:r-x
mask::r-x
other::---

一旦 mask 收緊,getfacl 就會用 #effective: 標註每個條目真正剩下的能力。實務上這是排查「明明設了權限卻沒作用」的第一步。

三、刪除 ACL

-x u:st 檔案   刪除指定 ACL 條目(--remove)
-b 檔案        移除所有延伸 ACL 條目(--remove-all)
[root@bogon temp]# setfacl -x u:st project/
[root@bogon temp]# setfacl -x g:tgroup2 project/
[root@bogon temp]# getfacl project/
# file: project/
# owner: root
# group: tgroup
user::rwx
group::rwx
mask::rwx
other::---

四、-R 與 default ACL:處理既有檔案與未來檔案

直接對目錄下 ACL,只影響這個目錄本身,裡面的子檔案不受影響。要連既有內容一起套用,加上 -R:

[root@bogon temp]# touch project/abc
[root@bogon temp]# setfacl -m u:st:rx project/
[root@bogon temp]# getfacl project/abc
user::rw-
group::r--
other::r--

[root@bogon temp]# setfacl -m u:st:rx -R project/
[root@bogon temp]# getfacl project/abc
user::rw-
user:st:r-x
mask::r-x
group::r--
other::r--

問題來了:-R 只管已經存在的檔案,之後新建的檔案一樣吃不到權限。

[root@bogon temp]# touch project/newabc
[root@bogon temp]# getfacl project/newabc
user::rw-
group::r--
other::r--

這時候需要 default ACL,用 d: 前綴設定,讓目錄「記得」要把哪些權限灌給新成員:

[root@bogon temp]# setfacl -m d:u:st:rx project/
[root@bogon temp]# touch project/newabc2
[root@bogon temp]# getfacl project/newabc2
user::rw-
user:st:r-x      #effective:r--
group::rwx       #effective:rw-
mask::rw-
other::---

一句話記法:-R 管現在,d: 管未來。

五、setUID:暫時借來擁有者的身分

[root@bogon temp]# ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 6月 10 2014 /usr/bin/passwd

那個出現在擁有者 x 位置上的 s,就是 setUID。它讓使用者執行該程式時,暫時取得檔案擁有者(root)的權限。原因很實際:passwd 需要改寫 /etc/shadow,而 shadow 只有 root 能動,因此必須在一般使用者執行 passwd 的期間借到 root 的寫入能力。

前提條件也很明確:該檔案必須是可執行的,否則 s 沒有意義。

Linux 權限完全指南:從 rwx 到 ACL 與 setUID

Copyright © Linux教程網 All Rights Reserved