Login

Navigation

This articles is published 724 days ago and last updated 724 days ago, some information may be out of date.

SUID/SGID/SBIT 的功能與應用

Cover.png

SUID (Set User ID) 的功能
系統設定一個 SUID 的權限旗標到 passwd 執行擋上,
當使用者執行 passwd 指令時,就能夠藉由 SUID 來切換執行權限。

查看passwd權限

❯ ll /usr/bin/passwd
-rwsr-xr-x 1 root root 67K Jul 15  2021 /usr/bin/passwd

可以看到root權限的 x 變成了 s,當用戶執行 passwd 時,就會自動透過 SUID 轉換身份成為 owner ,亦即變成了 root 的身份。

SGID (Set Group ID) 的功能
SUID 類似,SGID 為將特殊的權限旗標設定在群組的 x 上。

因為每個用戶在該目錄下產生的新檔案所屬群組並非共享群組。當目錄添加上 SGID 的權限旗標後,共享目錄底下新建的內容都屬於共享群組。用於多用戶在共享目錄,如SAMBA等等。

查看apps目錄權限

ubuntu@proxy:~/apps/$ ll
total 20
drwxrws--- 3 ubuntu users 4096 Apr 22 08:58 ./
drwxrws--- 3 ubuntu users 4096 Oct  4  2021 ../
-rwxrwx--- 1 root   users 3530 Apr 26 08:04 .env*
-rwxrws--- 1 ubuntu users  264 Oct  4  2021 docker-compose.yml*
drwxrws--- 9 root   users 4096 Apr  1 21:15 logs/

當使用者root建立的文件(.env)還是ubuntu建立文件(docker-compose.yml),都可以相互編輯,因為rootubuntu 都屬於users群組時,而群組users具有讀寫執(r,w,x)的權限。

SBIT (Sticky bit) 的功能
當使用者在該目錄下建立檔案或目錄時,僅有自己與 root 才有權力刪除該檔案。
SUID/SGID/SBIT 權限的設定

查看tmp目錄內容權限

-rw-r--r-- 1 king king  104 Mar  8 10:18 p10k-instant-prompt-output-king-619
-rw-r--r-- 1 king king   60 Jan 22 14:35 p10k-instant-prompt-output-king-8893
drwx------ 2 king king 4.0K Apr 11 09:12 tmux-1000
  • 4 為 SUID

    • 文件「p10k-instant-prompt-output-king-8893」的傳統權限為「-rw-r--r--」添加SUID標籤(權限) chmod 4644 p10k-instant-prompt-output-king-8893
  • 2 為 SGID

    • 目錄「tmux-1000」的傳統權限為「drwx------」添加SGID標籤(權限) chmod 2700 tmux-1000
  • 1 為 SBIT

    • 文件「p10k-instant-prompt-output-king-619」的傳統權限為「-rw-r--r--」添加SBIT標籤(權限) chmod 1777 p10k-instant-prompt-output-king-619

也可以使用符號法,添加 SUID/SGID/SBIT權限:

  • SUID: chmod u+s filename
  • SGID: chmod g+s filename
  • SBIT: chmod o+t filename

修改後tmp目錄內容權限

-rwxrwxrwt 1 king king  104 Mar  8 10:18 p10k-instant-prompt-output-tkk-619
-rwSr--r-- 1 king king   60 Jan 22 14:35 p10k-instant-prompt-output-tkk-8893
drwx--S--- 2 king king 4.0K Apr 11 09:12 tmux-1000
參考資料:

鳥哥私房菜 - 第 5 堂課:權限應用、程序之觀察與基本管理