linux下文件特殊權限設置位S和沾附位T


一. 設置位S

為了讓一般使用者臨時具有該文件所屬主/組的執行權限。比如/usr/bin/passwd在執行它的時候需要去修改/etc/passwd和/etc/shadow等文件,這些文件除了root外,其他用戶都沒有寫權限,但是又為了能讓普通用戶修改自己的密碼,只能時臨時讓他們具有root的權限。所以這個s權限就是用來完成這個特殊任務的。s權限只能應用在二進制的可執行文件上。
如果你不想讓普通用戶修改自己的密碼,只需要 [root@localhost ~]# chmod u-s /usr/bin/passwd 或者 [root@localhost ~]# 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權限就是為了解決這個問題。

下面通過一個實例來體會這個t權限的用法:

(1) root用戶在/tmp目錄下創建一個test目錄,並設置test目錄的相關權限為1777(有特殊權限t)

[root@localhost tmp]# mkdir test
[root@localhost tmp]# chmod 1777 test
[root@localhost tmp]# ls -ld test
drwxrwxrwt. 2 root root 4096 Oct 12 22:31 test

(2) 切換到第一個用戶zhangming,在test目錄下創建一個新文件aaa.txt,並寫入數據

[root@localhost tmp]# su zhangming
[zhangming@localhost tmp]$ touch test/aaa.txt
[zhangming@localhost tmp]$ echo "hello" >> test/aaa.txt
[zhangming@localhost tmp]$ ls -l test
total 4
-rw-rw-r--. 1 zhangming zhangming 6 Oct 12 22:34 aaa.txt

(3) 切換到第二個用戶shuihuo379,嘗試刪除zhangming用戶創建的文件aaa.txt,此時提示無法刪除

[zhangming@localhost tmp]$ su shuihuo379
[shuihuo379@localhost tmp]$ ls -l test/aaa.txt
-rw-rw-r--. 1 zhangming zhangming 6 Oct 12 22:34 test/aaa.txt
[shuihuo379@localhost tmp]$ rm test/aaa.txt
rm: remove write-protected regular file `test/aaa.txt'? y
rm: cannot remove `test/aaa.txt': Operation not permitted

(4) 重新切換到root用戶,執行刪除權限位t操作

[shuihuo379@localhost tmp]$ su
[root@localhost tmp]# chmod -t test
[root@localhost tmp]# ls -ld test
drwxrwxrwx. 2 root root 4096 Oct 12 22:33 test

(5) 再次切換到用戶shuihuo379,嘗試刪除zhangming用戶創建的文件aaa.txt,此時刪除成功,zhangming用戶創建的文件aaa.txt已經不存在了

[root@localhost tmp]# su shuihuo379
[shuihuo379@localhost tmp]$ ls -l test
total 4
-rw-rw-r--. 1 zhangming zhangming 6 Oct 12 22:34 aaa.txt
[shuihuo379@localhost tmp]$ rm test/aaa.txt
rm: remove write-protected regular file `test/aaa.txt'? y
[shuihuo379@localhost tmp]$ ls -l test
total 0


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM