特殊权限语法2

Linux特殊权限介绍
一、s权限:

当s权限位于可执行命令所有者的位置时,其他用户执行时将具有所有者的权限
# ll $(which passwd)
-rwsr-xr-x. 1 root root 27832 1月 30 2014 /usr/bin/passwd
# ll /etc/shadow
----------. 1 root root 1447 5月 17 20:58 /etc/shadow
可以看到,用户没有任何对/etc/shadow文件的权限,但是其他用户可以修改密码,为什么呢?就是因为passwd命令所有者的位置上有s权限

看一个例子你就明白了:

# touch /opt/test.txt
# ll /opt/test.txt
-rw-r--r--. 1 root root 0 5月 27 12:26 /opt/test.txt
用root用户在/opt下创建一个test.txt文件,此时除了root用户其他用户没有写权限。
接下来我们给vim命令的所有者位置增加一个s权限:

# chmod u+s $(which vim)
# ll $(which vim)
-rwsr-xr-x. 1 root root 2289600 1月 30 2014 /usr/bin/vim
此时用其他用户可以使用vim往/opt/test.txt写内容。

了解了这个例子,也就能理解s位于可执行命令所有者位置时的特殊权限了。

s位于目录的所属组位置时,不管哪个用户在该目录下创建文件或目录,都会继承该目录的所属组:
# mkdir /opt/test
# chgrp tom /opt/test
# chmod g+s /opt/test
# ls -ld /opt/test
drwxr-srwx. 2 root tom 6 5月 27 12:37 /opt/test
然后分别用root和bob用户在该目录下创建文件:

# touch /opt/test/test_root
# ll /opt/test/test_root
-rw-r--r--. 1 root tom 0 5月 27 12:40 /opt/test/test_root
$ touch /opt/test/test_bob
$ ll /opt/test/test_bob
-rw-rw-r--. 1 bob tom 0 5月 27 12:42 /opt/test/test_bob
# mkdir /opt/test/root1
# ls -ld /opt/test/root1
drwxr-sr-x. 2 root tom 6 5月 27 12:42 /opt/test/root1
看懂了这个例子,就能理解s位于所属组位置的权限问题了

t权限
t位于目录的other的位置时,这时除了所有者和root之外,其他的用户即使有权限,也无法删除该目录下的文件:
[root@www ~]# mkdir /opt/test2
[root@www ~]# chmod 777 /opt/test2
[root@www ~]# touch /opt/test2/test
[root@www ~]# chmod 777 /opt/test2/test
[tom@www ~]$ rm -rf /opt/test2/test
[root@www ~]# chmod o+t /opt/test2
[root@www ~]# touch /opt/test2/test
[root@www ~]# chmod 777 /opt/test2/test
[tom@www ~]$ rm -rf /opt/test2/test
rm: 无法删除"/opt/test2/test": 不允许的操作
通过这个例子,就能理解t位于目录other位置的权限了。

chmod 7777 file
后面三个权限是原本的权限,前面的7代表的是:

u+s --------> 4
g+s --------> 2
o+t --------> 1
三、特殊权限
使用lsattr命令查看特殊权限.

特殊权限a,意味着只能增加不能减小,对目录和文件都生效:
[root@www ~]# touch /tmp/test
[root@www ~]# lsattr /tmp/test
---------------- /tmp/test
[root@www ~]# echo xxxxxxxx > /tmp/test
[root@www ~]# chattr +a /tmp/test
[root@www ~]# lsattr /tmp/test
-----a---------- /tmp/test
[root@www ~]# echo yyyyyyyyyyy > /tmp/test
-bash: /tmp/test: 不允许的操作
[root@www ~]# echo yyyyyyyyyyy >> /tmp/test
注:>表示先清空,再写入

特殊权限i,文件和目录的内容不能发生改变:
[root@www ~]# chattr -a /tmp/test
[root@www ~]# chattr +i /tmp/test
[root@www ~]# cat /tmp/test
xxxxxxxx
yyyyyyyyyyy
[root@www ~]# lsattr /tmp/test
----i----------- /tmp/test
[root@www ~]# echo hahahhahah >> /tmp/test
-bash: /tmp/test: 权限不够

原文地址:https://www.cnblogs.com/duanlinxiao/p/10778771.html