linux下文件特殊权限设置位S和沾附位T(转载)

今天在创建文件的时候,发现了文件权限末尾有个T,之前没留意过,后来又用c创建(open)了两个文件来查看,在我没有指定权限(省略open的第三个参数)的时候,有时还会出现S,虽然还没弄懂什么时候会出现S和T,但是先了解S和T的含义,以此记录。这里的S和T都是针对执行权限x的。少数内容和原文不同,整理了别的资料。

一. 设置位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

转自:

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

原文地址:https://www.cnblogs.com/Cccarl/p/6985143.html