1. 程式人生 > >linux下文件特殊權限設置位S和沾附位T(轉載)

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

linux 表示 mkdir 文件 執行 pre 體會 針對 min

今天在創建文件的時候,發現了文件權限末尾有個T,之前沒留意過,後來又用c創建(open)了兩個文件來查看,在我沒有指定權限(省略open的第三個參數)的時候,有時還會出現S,雖然還沒弄懂什麽時候會出現S和T,但是先了解S和T的含義,以此記錄。這裏的S和T都是針對執行權限x的。少數內容和原文不同,整理了別的資料。

一. 設置位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權限就是為了解決這個問題。

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

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

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

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

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

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

[[email protected] tmp]$ su shuihuo379
[[email protected] tmp]$ ls -l test/aaa.txt
-rw-rw-r--. 1 zhangming zhangming 6 Oct 12 22:34 test/aaa.txt
[[email protected] 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操作

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

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

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

轉自:

http://www.cnblogs.com/zhangming-blog/articles/5956280.html

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