SUID/SGID/SBIT 權限設定

以下摘自鸟哥的Linux私房菜

  • SUID/SGID/SBIT 權限設定

前面介紹過 SUID 與 SGID 的功能,那麼如何設定檔案使成為具有 SUID 與 SGID 的權限呢? 這就需要第五章的數字更改權限的方法了! 現在你應該已經知道數字型態更改權限的方式為『三個數字』的組合, 那麼如果在這三個數字之前再加上一個數字的話,最前面的那個數字就代表這幾個權限了!

  • 4 為 SUID
  • 2 為 SGID
  • 1 為 SBIT

假設要將一個檔案權限改為『-rwsr-xr-x』時,由於 s 在使用者權限中,所以是 SUID ,因此, 在原先的 755 之前還要加上 4 ,也就是:『 chmod 4755 filename 』來設定!此外,還有大 S 與大 T 的產生喔!參考底下的範例啦!

Tips鳥哥的圖示注意:底下的範例只是練習而已,所以鳥哥使用同一個檔案來設定,你必須瞭解 SUID 不是用在目錄上,而 SBIT 不是用在檔案上的喔!
[root@study ~]# cd /tmp
[root@study tmp]# touch test                  <==建立一個測試用空檔
[root@study tmp]# chmod 4755 test; ls -l test <==加入具有 SUID 的權限
-rwsr-xr-x 1 root root 0 Jun 16 02:53 test
[root@study tmp]# chmod 6755 test; ls -l test <==加入具有 SUID/SGID 的權限
-rwsr-sr-x 1 root root 0 Jun 16 02:53 test
[root@study tmp]# chmod 1755 test; ls -l test <==加入 SBIT 的功能!
-rwxr-xr-t 1 root root 0 Jun 16 02:53 test
[root@study tmp]# chmod 7666 test; ls -l test <==具有空的 SUID/SGID 權限
-rwSrwSrwT 1 root root 0 Jun 16 02:53 test

最後一個例子就要特別小心啦!怎麼會出現大寫的 S 與 T 呢?不都是小寫的嗎? 因為 s 與 t 都是取代 x 這個權限的,但是你有沒有發現阿,我們是下達 7666 喔!也就是說, user, group 以及 others 都沒有 x 這個可執行的標誌( 因為 666 嘛 ),所以,這個 S, T 代表的就是『空的』啦!怎麼說? SUID 是表示『該檔案在執行的時候,具有檔案擁有者的權限』,但是檔案 擁有者都無法執行了,哪裡來的權限給其他人使用?當然就是空的啦! ^_^

而除了數字法之外,妳也可以透過符號法來處理喔!其中 SUID 為 u+s ,而 SGID 為 g+s ,SBIT 則是 o+t 囉!來看看如下的範例:

# 設定權限成為 -rws--x--x 的模樣:
[root@study tmp]# chmod u=rwxs,go=x test; ls -l test
-rws--x--x 1 root root 0 Jun 16 02:53 test

# 承上,加上 SGID 與 SBIT 在上述的檔案權限中!
[root@study tmp]# chmod g+s,o+t test; ls -l test
-rws--s--t 1 root root 0 Jun 16 02:53 test
原文地址:https://www.cnblogs.com/taosiyu/p/12973655.html