特殊權限
在系統中,難免有一些比較特殊的用戶或文件目錄,但是普通的權限有不能解決我們的需求,于是就有特殊權限幫我們解決這個問題
特殊權限有三個:SUID、SGID、STICKY
首先,先說說安全上下文:
進程以某用戶的身份運行,那么進程將會以此用戶身份去完成所有操作。
然后,權限的匹配過程:
1.進程的屬主(發起者),是否為被訪問的文件的屬主,如果是,則應用屬主的權限對文件進行操作,否則進入第二步
2.判斷進程的屬主是否為被訪問的文件的屬組,如果是,則應用該權限,否則應用other的權限。
最后,特殊權限將打破這一匹配過程。具體如下:
SUID:超級屬主
用戶運行某程序時,如果發現其擁有SUID權限,那么程序運行轉換為進程時,其進程的屬主(發起者)不是真實的發起者,而是擁有SUID權限程序的屬主。
當我們看都某文件或目錄的第一個3位權限最后一位為S時,那么當用戶運行命令對其執行是,進程的屬主則是其文件上的屬主。
SUID權限管理
chmod u+/- S FILE... [root@localhost testdir]# ls -l -rw-r--r-- 1 root root 0 Jul 25 18:20 aaa //原有權限 [root@localhost testdir]# chmod u+s aaa //加上SUID權限 [root@localhost testdir]# ls -l -rwSr--r-- 1 root root 0 Jul 25 18:20 aaa //第一個三位權限的最后一位為大寫S [root@localhost testdir]# chmod u-s aaa //取消對文件的SUID [root@localhost testdir]# ls -l -rw-r--r-- 1 root root 0 Jul 25 18:20 aaa //恢復默認
SUID實戰解析
1.讓lii用戶能查看magedu用戶的家目錄
[root@localhost ~]# cp /bin/ls /testdir/ls //復制/bin/ls 到另一個目錄,這樣能完成實驗的需求 [root@localhost ~]# chmod u+s /testdir/ls //然后給復制過去的ls 加上SUID權限,使得我們一會好應用此權限去訪問其他用戶的家目錄 [root@localhost ~]# ls -l /testdir/ls //查看復制過去的ls里有s權限(注意:此時系統用的是默認的/bin/ls,我把原有的ls別名刪除了) -rwsr-xr-x 1 root root 117048 Jul 25 18:51 /testdir/l [lii@localhost home]$ ls -l magedu/ ls: cannot open directory magedu/: Permission denied //用其他用戶登錄,在家目錄中查看別的用戶的家目錄是不允許的 //權限不夠,因為系統原有的/bin/ls 沒有SUID權限。 [lii@localhost home]$ /testdir/ls -a magedu/ . .. .bash_history .bash_logout .bash_profile.bashrc .gnome2 //當我們使用復制過去并添加SUID的ls命令查看別的用戶的家目錄時是可以的。因為這個ls的屬主是root,root用戶查看任何一個用戶的家目錄都是可以的,所以,這就是SUID的作用。
結論:SUID對文件進行授權SUID之后,任何一個用戶使用其授權的SUID的二進制文件訪問時,使用的是其二進制文件的屬主來對其訪問的。
注意:在添加s權限時,原文件必須要有執行權限(x),如果沒有添加SUID之后顯權限位顯示為大S則表示SUID權限沒成功,必須為小s。
SGID:超級組
當目錄屬組有寫權限時,且有SGID權限時,那么所有屬于此目錄的屬組的用戶(以此目錄的屬組為附加組的用戶),并且以屬組身份在此目錄中新建文件或目錄是,新建的文件或目錄不是用戶的本組,而是此目錄的屬組。
SGID權限管理
chmod g+/- S FILE... [root@localhost /]# ls -ld /testdir/SGID drwxr-xr-x 2 root root 4096 Jul 25 19:19 /testdir/SGID [root@localhost /]# chmod g+s /testdir/SGID //給目錄添加SGID權限 [root@localhost /]# ls -ld /testdir/SGID drwxr-sr-x 2 root root 4096 Jul 25 19:19 /testdir/SGID [root@localhost /]# chmod g-s /testdir/SGID //給目錄減去SGID權限 [root@localhost /]# ls -ld /testdir/SGID drwxr-xr-x 2 root root 4096 Jul 25 19:19 /testdir/SGID
SGID實戰解析
1.將目錄設定SGID,讓其他用戶在此目錄創建文件或目錄是屬主不變,屬組為目錄的屬組。
[root@localhost /]# ls -ld /testdir/sgid //查看原有目錄權限 drwx-wxr-x 2 root root 4096 Jul 25 19:37 /testdir/sgid [root@localhost /]# chmod g+s /testdir/sgid //對目錄增加s權限,(注意此前一定要有w權限才能創建文件,這是普通權限對目錄的含義) [root@localhost /]# ls -ld /testdir/sgid //查看目錄信息 drwx-wsr-x 2 root root 4096 Jul 25 19:37 /testdir/sgid [root@localhost /]# su - lii //切換用戶之后在目錄內創建文件 [lii@localhost ~]$ touch /testdir/sgid/lii.txt [root@localhost /]# ls -l /testdir/sgid/lii.txt -rw-r--r-- 1 lii root 0 Jul 25 19:39 /testdir/sgid/lii.txt //查看創建的文件的屬主屬組 屬主為lii,屬組則為root,因為sgid目錄具有SGID權限。
結論:1.對目錄進行賦權SGID時,在其目錄中創建文件的用戶必須是其目錄的屬組的成員。
2.當sgid作用在目錄上時,在目錄內新建的文件或目錄的所屬組自動繼承該目錄的所屬組
3.在具有SGID權限的目錄中創建文件時,目錄的屬組一定要有寫(w)權限,因為這樣才能對其目錄創建文件。、
2.對二進制可執行文件添加SGID權限,讓其他用戶能使用其有SGID權限的二進制程序對文件進行操作
[root@localhost ~]# chmod g+s /bin/cat //二進制可執行文件/bin/cat加上SGID權限 [root@localhost ~]# ls -l /bin/cat -rwxr-sr-x 1 root root 48568 May 11 16:59 /bin/cat [root@localhost ~]# ls -l /etc/shadow ----r----- 1 root magedu 1051 Jul 25 12:28 /etc/shadow //查看shadow文件的屬組為magedu,屬組權限為可讀 [root@localhost ~]# su - lii [lii@localhost ~]$ cat /etc/shadow cat: /etc/shadow: Permission denied //切換到lii用戶執行cat訪問shadow文件,提示權限不不夠,因為此時shadow文件的屬組為magedu,而cat的屬組為root,所以cat的SGID在這里并不能生效。 [root@localhost ~]# chown :root /etc/shadow //將shadow文件屬組更改為root,再用lii用戶查看shadow文件,查看成功。 [root@localhost ~]# su - lii [lii@localhost ~]$ cat /etc/shadow root:$6$npNlJrmB$T5JD58gi9xVAkEudYZlhixv8BCjd316Q6mR/xvOi7gibuS8BpMuxTgrPpZyFoyTu31WQGB9algduVjYux7b8g0:17007:0:99999:7::: bin:*:15980:0:99999:7::: daemon:*:15980:0:99999:7:::
結論:對二進制文件設定SGID時,其他用戶引用的是該二進制文件的屬組,所以在其他用戶使用其二進制文件訪問其他文件時,其文件的屬組必須和二進制文件的屬組一致才能進行訪問,
注意:在添加s權限時,原文件必須要有執行權限(x),如果沒有添加SGID之后顯權限位顯示為大S則表示SGID權限沒成功,必須為小s。
STICKY:其他用戶權限
對于屬組可寫或全局可寫的目錄,所在組內的用戶或系統上的所有用戶在此目錄創建或刪除文件,如果為此類目錄設置sticky權限,則每個用戶能創建新文件,但只能刪除屬主為自己的文件
STICKY權限管理
chmod o+/- t FILE…
[root@localhost /]# ls -ld /testdir/STICKY drwxr-xr-x 2 root root 4096 Jul 25 19:19 /testdir/STICKY [root@localhost /]# chmod o+t /testdir/STICKY //給目錄添加STICKY權限 [root@localhost /]# ls -ld /testdir/STICKY drwxr-sr-t 2 root root 4096 Jul 25 19:19 /testdir/STICKY [root@localhost /]# chmod o-t /testdir/STICKY //給目錄減去STICKY權限 [root@localhost /]# ls -ld /testdir/STICKY drwxr-xr-x 2 root root 4096 Jul 25 19:19 /testdir/STICK
STICKY實戰解析
對/testdir/sticky目錄設定sticty權限,并讓所有用戶創建在其內創建文件是自動屬于目錄的屬組,最后讓目錄內的文件所有用戶能看,只能刪除自己的,也只能編輯有屬組有w權限的文件
[
root@localhost testdir]# chmod o=rwx,g=rwx,u= /testdir/sticky/ [root@localhost testdir]# ll -d /testdir/sticky/ d---rwxrwx 2 root root 4096 Jul 25 14:13 /testdir/sticky/ //給文件創建指定權限,組和其他用戶都要有執行權限和讀權限,因為我們要滿足題目的屬組為目錄屬組和讓目錄內的文件用戶只能刪除自己的,并只能更改其文件屬組為目錄屬組的文件(并且還要有寫權限) [root@localhost testdir]# chmod o+t /testdir/sticky/ [root@localhost testdir]# ll -d /testdir/sticky/ d---rwxrwt 2 root root 4096 Jul 25 14:13 /testdir/sticky/ [root@localhost testdir]# chmod g+s /testdir/sticky/ [root@localhost testdir]# ll -d /testdir/sticky/ d---rwsrwt 2 root root 4096 Jul 25 14:22 /testdir/sticky/ //給目錄添加SGID和STICKY權限 [root@localhost testdir]# touch /testdir/sticky/root1 [lii@localhost ~]$ touch /testdir/sticky/lii1 [magedu@localhost ~]$ touch /testdir/sticky/magedu1 [magedu@localhost ~]$ ls /testdir/sticky/ -l -rw-rw-r-- 1 lii root 0 Jul 25 14:19 lii1 -rw-rw-r-- 1 magedu root 0 Jul 25 14:19 magedu1 -rw-r--r-- 1 root root 0 Jul 25 14:18 root1 //三個用戶分別創建一個文件,屬主為用戶本身,屬組為root,因為上面我們對目錄添加了SGID,會自動把屬組更改為目錄屬組,完成一半。 [magedu@localhost ~]$ vi /testdir/sticky/root2 [magedu@localhost ~]$ vi /testdir/sticky/lii1 //可以編輯lii1文件,因為此文件的屬組有寫權限,如果沒有也不能修改。但如果有執行權限,也不不能刪除其文件,因為此文件的父目錄設定了sticky權限。 [magedu@localhost ~]$ rm -f /testdir/sticky/lii1 //不能刪除其他用戶的文件,如果其余屬組的權限有的話課以修改和查看,但不能刪除別人的。 rm: cannot remove `/testdir/sticky/lii1': Operation not permitted [magedu@localhost ~]$ rm -f /testdir/sticky/root1 //也不能刪除 rm: cannot remove `/testdir/sticky/root1': Operation not permitted //用magedu賬戶登入第一行為編輯root2文件,而root2文件屬組為root但是沒有寫權限所以不能修改,只能看。
結論:1.sticky的作用為當給目錄設定sticky權限后,此內文件所有用戶只能刪除屬主為用戶本身的文件,
2.所有用戶能對此目錄創建文件,但如果要修改或查看其他人的文件取決于其他文件對用戶本身設定的權限。
原創文章,作者:Lii,如若轉載,請注明出處:http://www.www58058.com/28359
文章整體層次清晰,有理論,有實踐,通過實踐對文件權限有了深刻的認識和理解。