【Linux】一步一步学Linux——Linux特殊权限位详解(114)
00. 目錄
文章目錄
- 00. 目錄
- 01. 概述
- 02. SUID
- 03. SGID
- 04. SBIT
- 05. 附錄
01. 概述
在復雜的生產環境中,單純設置文件的rwx權限無法滿足我們對安全和靈活性的需求,因此便有了SUID、SGID與SBIT的特殊權限位。這是一種對文件權限進行設置特殊功能的方法,可以與一般權限同時使用,以彌補一般權限不能實現的功能。下面具體解釋這3個特殊權限位的功能以及用法。
Linux 系統文件除了9位基本權限,還有額外3位特殊權限,分別是SUID(setuid),SGID(setgid),SBIT(sticky bit)。
Linux 系統文件3位特殊權限位說明
標注:這3位特殊權限不建議使用(除系統默認的特殊權限可以使用以外),除非有特殊需求
| s (suid) | 小寫s (有suid執行權限) | 大寫S (無suid執行權限) | 4 |
| s (sgid) | 小寫s (有sgid執行權限) | 大寫S (無sgid執行權限) | 2 |
| t(sticky bit) | 小寫t (有sticky bit執行權限) | 大寫T (無sticky bit執行權限) | 1 |
02. SUID
SUID是一種對二進制程序進行設置的特殊權限,可以讓二進制程序的執行者臨時擁有文件所屬者的權限(僅對擁有執行權限的二進制文件有效)。例如,所有用戶都可以執行passwd命令來修改自己的用戶密碼,而用戶密碼保存在/etc/shadow文件中。仔細查看這個文件就會發現它的默認權限是000,也就是說除了root管理員以外,所有用戶都沒有查看或編輯該文件的權限。但是,在使用passwd命令時如果加上SUID特殊權限位,就可讓普通用戶臨時獲得程序所屬者的身份,把變更的密碼信息寫入到shadow文件中。這很像我們在古裝劇中見到的手持尚方寶劍的欽差大臣,他手持的尚方寶劍代表的是皇上的權威,因此可以懲戒貪官,但這并不意味著他永久成為了皇上。因此這只是一種有條件的、臨時的特殊權限授權方法。
查看passwd命令屬性時發現所有者的權限由rwx變成了rws,其中x改變成s就意味著該文件被賦予了SUID權限。另外有讀者會好奇,那么如果原本的權限是rw-呢?如果原先權限位上沒有x執行權限,那么被賦予特殊權限后將變成大寫的S。
[root@itcast share]# ls -l /etc/shadow ---------- 1 root root 1525 8月 3 17:08 /etc/shadow [root@itcast share]# [root@itcast share]# ls -l /bin/passwd -rwsr-xr-x. 1 root root 27832 6月 10 2014 /bin/passwd [root@itcast share]#用戶屬主對應的前三位權限的x位上如果有s就表示suid權限,當x位上沒有小寫x執行權限時候,suid的權限顯示就是大S
注意:suid功能是一把雙刃劍,是一個比較危險的功能,對系統安全有一定威脅系統,suid的無用的功能取消suid權限(安全優化)
設置SUID的例子
[root@itcast tmp]# pwd /tmp [root@itcast tmp]# touch file [root@itcast tmp]# ls -l file -rw-r--r-- 1 root root 0 8月 7 22:25 file#切換到deng用戶 [root@itcast tmp]# su - deng 上一次登錄:三 8月 7 22:16:01 CST 2019pts/1 上 [deng@itcast ~]$ cd /tmp/ [deng@itcast tmp]$ rm -rf file rm: 無法刪除"file": 不允許的操作 [deng@itcast tmp]$ [deng@itcast tmp]$ exit 登出[root@itcast tmp]# which rm alias rm='rm -i'/bin/rm [root@itcast tmp]# chmod u+s /bin/rm #追加特權 [root@itcast tmp]# ls -l /bin/rm -rwsr-xr-x. 1 root root 62864 4月 11 2018 /bin/rm [root@itcast tmp]# #切換到deng用戶 [root@itcast tmp]# su - deng 上一次登錄:三 8月 7 22:26:16 CST 2019pts/1 上 [deng@itcast ~]$ cd /tmp/ [deng@itcast tmp]$ rm -rf file #刪除成功 [deng@itcast tmp]$取消suid權限
[root@itcast tmp]# chmod u-s /bin/rm [root@itcast tmp]# ls -l /bin/rm -rwxr-xr-x. 1 root root 62864 4月 11 2018 /bin/rm [root@itcast tmp]#等同命令chmod 4755 /bin/rm,取消suid命令 chmod u-s /bin/rm或chmod 0755 /bin/rm
03. SGID
SGID主要實現如下兩種功能:
-
讓執行者臨時擁有所屬組的權限(對擁有執行權限的二進制程序進行設置有效);
-
在某個目錄中創建的文件自動繼承該目錄的用戶組(只可以對目錄進行設置有效)。
SGID的第一種功能是參考SUID而設計的,不同點在于執行程序的用戶獲取的不再是文件所有者的臨時權限,而是獲取到文件所屬組的權限。舉例來說,在早期的Linux系統中,/dev/kmem是一個字符設備文件,用于存儲內核程序要訪問的數據,權限為:
cr--r----- 1 root system 2, 1 Feb 11 2017 kmem除了root管理員或屬于system組成員外,所有用戶都沒有讀取該文件的權限。由于在平時我們需要查看系統的進程狀態,為了能夠獲取到進程的狀態信息,可在用于查看系統進程狀態的ps命令文件上增加SGID特殊權限位。查看ps命令文件的屬性信息:
-r-xr-sr-x 1 bin system 59346 Feb 11 2017 ps由于ps命令被增加了SGID特殊權限位,所以當用戶執行該命令時,也就臨時獲取到了system用戶組的權限,從而可以順利地讀取設備文件了。
之前提到,每個文件都有其歸屬的所有者和所屬組,當創建或傳送一個文件后,這個文件就會自動歸屬于執行這個操作的用戶(即該用戶是文件的所有者)。如果現在需要在一個部門內設置共享目錄,讓部門內的所有人員都能夠讀取目錄中的內容,那么就可以創建部門共享目錄后,在該目錄上設置SGID特殊權限位。這樣,部門內的任何人員在里面創建的任何文件都會歸屬于該目錄的所屬組,而不再是自己的基本用戶組。此時,我們用到的就是SGID的第二個功能,即在某個目錄中創建的文件自動繼承該目錄的用戶組(只可以對目錄進行設置)。
[root@itcast tmp]# mkdir /tmp/test [root@itcast tmp]# ls -ld test drwxr-xr-x 2 root root 6 8月 7 22:08 test [root@itcast tmp]# chmod 777 test [root@itcast tmp]# chmod g+s test [root@itcast tmp]# ls -ld test drwxrwsrwx 2 root root 6 8月 7 22:08 test [root@itcast tmp]#在使用上述命令設置好目錄的777權限(確保普通用戶可以向其中寫入文件),并為該目錄設置了SGID特殊權限位后,就可以切換至一個普通用戶,然后嘗試在該目錄中創建文件,并查看新創建的文件是否會繼承新創建的文件所在的目錄的所屬組名稱:
[root@itcast tmp]# su - deng 上一次登錄:三 8月 7 21:22:00 CST 2019從 172.16.0.51pts/1 上 [deng@itcast ~]$ cd /tmp/test [deng@itcast test]$ ls [deng@itcast test]$ touch file [deng@itcast test]$ ls -l file -rw-rw-r-- 1 deng root 0 8月 7 22:09 file [deng@itcast test]$我們發現新創建的文件默認所屬組是root
設置SGID的例子
[root@itcast ~]# mkdir test [root@itcast ~]# ls -ld test drwxr-xr-x 2 root root 6 8月 7 22:32 test [root@itcast ~]# chown deng:itcast test [root@itcast ~]# ls -ld test drwxr-xr-x 2 deng itcast 6 8月 7 22:32 test [root@itcast ~]# touch test/file [root@itcast ~]# ls -l test/file #發現創建文件的所屬者和所屬組都是root -rw-r--r-- 1 root root 0 8月 7 22:32 test/file [root@itcast ~]# [root@itcast ~]# [root@itcast ~]# chmod g+s test #設置SGID權限 [root@itcast ~]# touch test/file2 [root@itcast ~]# ls -l test/file2 #發現創建文件所屬組是itcast -rw-r--r-- 1 root itcast 0 8月 7 22:33 test/file2 [root@itcast ~]#04. SBIT
現實生活中,大學里的很多老師都要求學生將作業上傳到服務器的特定共享目錄中,但總是有幾個“破壞分子”喜歡刪除其他同學的作業,這時就要設置SBIT(Sticky Bit)特殊權限位了(也可以稱之為特殊權限位之粘滯位)。SBIT特殊權限位可確保用戶只能刪除自己的文件,而不能刪除其他用戶的文件。換句話說,當對某個目錄設置了SBIT粘滯位權限后,那么該目錄中的文件就只能被其所有者執行刪除操作了。
最初不知道是哪位非資深技術人員將Sticky Bit直譯成了“粘滯位”,我們建議將其稱為“保護位”,這既好記,又能立刻讓人了解它的作用。RHEL 7系統中的/tmp作為一個共享文件的目錄,默認已經設置了SBIT特殊權限位,因此除非是該目錄的所有者,否則無法刪除這里面的文件。
[deng@itcast test]$ ls -ld /tmp drwxrwxrwt. 17 root root 4096 8月 7 22:08 /tmp [deng@itcast test]$與前面所講的SUID和SGID權限顯示方法不同,當目錄被設置SBIT特殊權限位后,文件的其他人權限部分的x執行權限就會被替換成t或者T,原本有x執行權限則會寫成t,原本沒有x執行權限則會被寫成T。
[deng@itcast ~]$ su - deng 密碼: 上一次登錄:三 8月 7 22:13:04 CST 2019pts/1 上 [deng@itcast ~]$ [deng@itcast ~]$ cd /tmp [deng@itcast tmp]$ touch file1 [deng@itcast tmp]$ chmod 777 file1 [deng@itcast tmp]$ ls -l file1 -rwxrwxrwx 1 deng deng 0 8月 7 22:16 file1 [deng@itcast tmp]$其實,文件能否被刪除并不取決于自身的權限,而是看其所在目錄是否有寫入權限。為了避免現在很多用戶不放心,所以上面的命令還是賦予了這個test文件最大的777權限(rwxrwxrwx)。我們切換到另外一個普通用戶,然后嘗試刪除這個其他人創建的文件就會發現,即便讀、寫、執行權限全開,但是由于SBIT特殊權限位的緣故,依然無法刪除該文件:
[deng@itcast tmp]$ su - itcast 密碼: 上一次登錄:三 8月 7 22:15:11 CST 2019pts/1 上 [itcast@itcast ~]$ cd /tmp/ [itcast@itcast tmp]$ rm -rf file1 rm: 無法刪除"file1": 不允許的操作 [itcast@itcast tmp]$當然,要是也想對其他目錄來設置SBIT特殊權限位,用chmod命令就可以了。對應的參數o+t代表設置SBIT粘滯位權限
[deng@itcast ~]$ mkdir test [deng@itcast ~]$ chmod o+t test [deng@itcast ~]$ ls -ld test drwxrwxr-t 2 deng deng 6 8月 7 22:18 test [deng@itcast ~]$設置特權位的例子
[root@itcast /]# mkdir /ddd [root@itcast /]# touch /ddd/test.sh [root@itcast /]# chmod -R 0777 /ddd [root@itcast /]# ls -ld /ddd drwxrwxrwx 2 root root 21 8月 7 22:39 /ddd#等同命令chmod o+s /ddd [root@itcast /]# chmod 1777 /ddd [root@itcast /]# ls -ld /ddd drwxrwxrwt 2 root root 21 8月 7 22:39 /ddd [root@itcast /]# su - deng 上一次登錄:三 8月 7 22:37:28 CST 2019pts/1 上 [deng@itcast ~]$ rm -rf /ddd/test.sh rm: 無法刪除"/ddd/test.sh": 不允許的操作 [deng@itcast ~]$ exit 登出 [root@itcast /]# rm -rf /ddd/test.sh [root@itcast /]#05. 附錄
參考:【Linux】一步一步學Linux系列教程匯總
總結
以上是生活随笔為你收集整理的【Linux】一步一步学Linux——Linux特殊权限位详解(114)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Linux】一步一步学Linux——c
- 下一篇: 【Linux】一步一步学Linux——c