這三種特殊許可權(suid、sgid、sticky)
阿新 • • 發佈:2019-01-01
先看看這兩個檔案的許可權:
[[email protected] ~]# ls -ld /usr/bin/passwd /tmp
drwxrwxrwt 4 root root 4096 Jun 2 17:33 /tmp
-rwsr-xr-x 1 root root 22984 Jan 7 2007 /usr/bin/passwd
這裡的s和t是針對執行許可權來講的。
這個s許可權,是為了讓一般使用者臨時具有該檔案所屬主/組的執行許可權。就比如/usr/bin/passwd在執行它的時候需要去修改/etc/passwd和/etc/shadow等檔案,這些檔案除了root外,其他使用者都沒有寫許可權,但是又為了能讓普通使用者修改自己的密碼,只能時臨時讓他們具有root的許可權。所以這個s許可權就是用來完成這個特殊任務的。s許可權只能應用在二進位制的可執行檔案上。
如果你不想讓普通使用者修改自己的密碼,只需要
[ [email protected] ~]# chmod u-s /usr/bin/passwd 或者
[[email protected] ~]# chmod 0755 /usr/bin/passwd
0755最前面的0表示不使用任何特殊許可權,該位上的數字可以是0,1(--t),2(-s-),3(-st),4(s--),5(s-t),6(ss-),7(sst)
那個t許可權只針對目錄生效,它表示只能讓所屬主以及root可以刪除(重新命名/移動)該目錄下的檔案。比如/tmp目錄本來就是任何使用者都可以讀寫,如果別人可以任意刪除(重新命名/移動)自己的檔案,那豈不是很危險。所以這個t許可權就是為了解決這個麻煩的。下面舉一個例子,說明一下這個許可權的用法:
drwxrwxrwt 2 root root 4096 Jun 2 18:10 test
total 4
-rw-r--r-- 1 test1 test 0 Jun 2 18:12 1.txt
[ [email protected] tmp]$ ls -l test/1.txt
-rwxrwxrwx 1 test1 test 6 Jun 2 18:12 test/1.txt
rm: cannot remove `test/1.txt': Operation not permitted
提示不能刪除1.txt
去掉t許可權。
drwxrwxrwx 2 root root 4096 Jun 2 18:13 test
[[email protected] tmp]$ rm -f test/1.txt
再次刪除,則刪除成功。
ls: test/1.txt: No such file or directory
-----------------------------------------------------------------------------------------------
2,特殊許可權使用
前面我們學習過linux的基本許可權,但如果只有基本許可權,可能無法滿足各式各樣的要求
例如:建立一個公共目錄任何人都可以在目錄裡建立自己的檔案,但只能刪除自己的檔案,此時基本許可權就無能為力了.
如果你想要完成這種需求就必須要藉助linux的特殊許可權;特殊許可權可以更精密的定義檔案的許可權;
之前我們看到的umask是0022,其中第一個0就是描述的特殊許可權.
這類特殊許可權共有三種;
suid sgid sticky
那現在來看下第一個特殊許可權 suid
限定:只能設定在二進位制可執行程式上,對目錄無效和文字無效
功能:不管誰來執行程式,linux都以程式的擁有者身份進入許可權獲取流程中從而決定存取許可權,相當於許可權下發
特徵:在uesr位的x顯示為S或s,s代表包含了x許可權,S代表未包含x許可權
試驗演示:
試驗一:使用者修改密碼藉助root身份
# ll /usr/bin/passwd
-rwsr-xr-x 1 root root /usr/bin/passwd
# chmod u-s /usr/bin/passwd
# ll /usr/bin/passwd
-rwxr-xr-x 1 root root /usr/bin/passwd
# su - aming
$ passwd
Changing password for user seker.
Changing password for seker
(current) UNIX password:
passwd: Authentication token manipulation error
$
試驗二:使用者無法讀取/etc/shadow,借用root身份使用cat命令則可
#
ll /etc/shadow
---------- 1 root root /etc/shadow
# su - aming
$ cat /etc/shadow
cat: /etc/shadow:許可權不夠
$ exit
logout
# chmod u+s /bin/cat
# su - aming
$ cat /etc/shadow
root:$1$EV/a2BnK$pRN0qjwqLf8zvpK8w1MFT.:14360:0:99999:7:::
瞭解了suid,我們再來看看sgid
限定:sgid既可以作用於二進位制檔案又可以作用於目錄,但兩者的意義卻截然不同
功能:
先說在二進位制檔案上,與前面講的SUID類似:不管是誰來執行,都以檔案的所屬組身份來決定許可權
大家自己測試,跟suid一樣
再說作用於目錄上:預設情況下使用者建立檔案時,檔案的所屬組是使用者的主組,如果在設定了SGID目錄下建立檔案,則檔案的所屬組是繼承目錄的屬組,並且新建立的目錄也繼承g+s許可權
特徵:在group位的x顯示為S或s,s代表包含了x許可權,S代表未包含x權
試驗一:
# mkdir /public
# chmod 777 /public
# chmod g+s /public
# su - lishiming
$ cd /public
$ mkdir lishiming
$ ls -l lishiming -d
-rwxr-xr-x 1 lishiming root lishiming
$ touch sgid_yes
$ ll sgid_yes
-rw-rw-r-- 1 lishiming root sgid_yes
sticky 冒險位(黏貼位) 限定:只作用於目錄 功能:任何人都可以在一個目錄下建立檔案,卻只有root和建立者本人才可以刪除檔案 特徵:在other位的x顯示為T或t,t代表包含了x許可權,T代表未包含x許可權 # chmod o+t /public drwxrwxrwt 5 root root /public # su - lishiming $ cd /public -rw-r--r-- 1 leiqin leiqin test-file $ rm -rf test-file rm:無法刪除“test-file”:不允許的操作 (充分說明lishiming這個賬戶在/public目錄下無法刪除leiqin這個使用者的檔案,雖然/public的目錄許可權是777)
sticky 冒險位(黏貼位) 限定:只作用於目錄 功能:任何人都可以在一個目錄下建立檔案,卻只有root和建立者本人才可以刪除檔案 特徵:在other位的x顯示為T或t,t代表包含了x許可權,T代表未包含x許可權 # chmod o+t /public drwxrwxrwt 5 root root /public # su - lishiming $ cd /public -rw-r--r-- 1 leiqin leiqin test-file $ rm -rf test-file rm:無法刪除“test-file”:不允許的操作 (充分說明lishiming這個賬戶在/public目錄下無法刪除leiqin這個使用者的檔案,雖然/public的目錄許可權是777)