粘滞位(Stickybit),或粘着位,是Unix文件系统权限的一个旗标。最常见的用法在目录上设置粘滞位,如此以来,只有目录内文件的所有者或者root才可以删除或移动该文件。如果不为目录设置粘滞位,任何具有该目录写和执行权限的用户都可以删除和移动其中的文件。实际应用中,粘滞位一般用于/tmp目录,以防止普通用户删除或移动其他用户的文件。(粘滞位概念来源百度百科https://baike.baidu.com/item/粘滞位/15576416?fr=aladdin)
本博客主要给出一个案例来引入粘滞位的使用方法。
案例:
同组下的两个用户创建的问价可以读写但是不能删除(默认在不加权限的情况下是可以删除的)
分析思路:
首先创建devops组和bob、alice两个用户,将两个用户加入到组中以备实验
其次创建public文件夹并赋予777权限,给public赋予sgid权限
将public文件夹的组属性改为devops,那么现在alice和bob在该文件夹下创建的文件的默认组属性都设置为了devops
用alice用户和bob用户分别在该文件夹下创建新的文件并验证,可以发现两个用户可以对对方的文件进行读写删除的操作。
给public赋予粘滞位权限(chmod o+t /public),然后ls -ld /public查看文件夹的权限
再次分别登入alice用户和bob用户,创建新的文件
再次验证,结果得到alice用户和bob用户可以对对方的文件进行读写操作,但是不能进行删除操作
实现:
[root@localhost ~]# groupadd devops
[root@localhost ~]# useradd alice -G devops
[root@localhost ~]# useradd bob -G devops
[root@localhost ~]# cat /etc/group | grep devops
devops:x:1000:alice,bob
[root@localhost ~]# mkdir /public
[root@localhost ~]# chmod 777 /public
[root@localhost ~]# ls -ld /public
drwxrwxrwx. 2 root root 6 11月 3 11:41 /public
[root@localhost ~]# chmod g+s /public
[root@localhost ~]# ls -ld /public
drwxrwsrwx. 2 root root 6 11月 3 11:41 /public
[root@localhost ~]# chown :devops /public
[root@localhost ~]# ls -ld /public
drwxrwsrwx. 2 root devops 6 11月 3 11:41 /public
[root@localhost ~]# su - alice
[alice@localhost ~]$ cd /public
[alice@localhost public]$ echo "This is alice" > alice.txt
[alice@localhost public]$ cat alice.txt
This is alice
[alice@localhost public]$ ls -l alice.txt
-rw-rw-r--. 1 alice devops 14 11月 3 11:43 alice.txt
[root@localhost ~]# su - bob
[bob@localhost ~]$ cd /public
[bob@localhost public]$ echo "This is bob" > bob.txt
[bob@localhost public]$ cat bob.txt
This is bob
[bob@localhost public]$ ls -l bob.txt
-rw-rw-r--. 1 bob devops 12 11月 3 11:44 bob.txt
[root@localhost ~]# su - alice
上一次登录:日 11月 3 11:43:29 CST 2019pts/0 上
[alice@localhost ~]$ cd /public
[alice@localhost public]$ ls -l
总用量 8
-rw-rw-r--. 1 alice devops 14 11月 3 11:43 alice.txt
-rw-rw-r--. 1 bob devops 12 11月 3 11:44 bob.txt
[alice@localhost public]$ cat bob.txt
This is bob
[alice@localhost public]$ echo "This is Alice Write" > bob.txt
[alice@localhost public]$ cat bob.txt
This is Alice Write
[alice@localhost public]$ rm -rf bob.txt
[root@localhost ~]# su - bob
上一次登录:日 11月 3 11:44:30 CST 2019pts/0 上
[bob@localhost ~]$ cd /public
[bob@localhost public]$ ls -l
总用量 4
-rw-rw-r--. 1 alice devops 14 11月 3 11:43 alice.txt
[bob@localhost public]$ cat alice.txt
This is alice
[bob@localhost public]$ echo "This is bob write" > alice.txt
[bob@localhost public]$ cat alice.txt
This is bob write
[bob@localhost public]$ rm -rf alice.txt
[root@localhost ~]# chmod o+t /public
[root@localhost ~]# ls -ld /public
drwxrwsrwt. 2 root devops 6 11月 3 11:47 /public
[root@localhost ~]# su - alice
上一次登录:日 11月 3 11:45:24 CST 2019pts/0 上
[alice@localhost ~]$ cd /public
[alice@localhost public]$ echo "This is alice" > alice.txt
[alice@localhost public]$ cat alice.txt
This is alice
[alice@localhost public]$ ls -l alice.txt
-rw-rw-r--. 1 alice devops 14 11月 3 11:43 alice.txt
[root@localhost ~]# su - bob
上一次登录:日 11月 3 11:46:26 CST 2019pts/0 上
[bob@localhost ~]$ cd /public
[bob@localhost public]$ echo "This is Bob" > bob.txt
[bob@localhost public]$ cat bob.txt
This is Bob
[bob@localhost public]$ ls -l bob.txt
-rw-rw-r--. 1 bob devops 12 11月 3 11:49 bob.txt
[root@localhost ~]# su - alice
上一次登录:日 11月 3 11:48:05 CST 2019pts/0 上
[alice@localhost ~]$ cd /public
[alice@localhost public]$ ls -l
总用量 8
-rw-rw-r--. 1 alice devops 14 11月 3 11:48 alice.txt
-rw-rw-r--. 1 bob devops 12 11月 3 11:49 bob.txt
[alice@localhost public]$ cat bob.txt
This is Bob
[alice@localhost public]$ echo "This is Alice Write" >> bob.txt
[alice@localhost public]$ cat bob.txt
This is Bob
This is Alice Write
[alice@localhost public]$ rm -rf bob.txt
rm: 无法删除"bob.txt": 不允许的操作
[root@localhost ~]# su - bob
上一次登录:日 11月 3 11:48:58 CST 2019pts/0 上
[bob@localhost ~]$ cd /public/
[bob@localhost public]$ ls -l
总用量 8
-rw-rw-r--. 1 alice devops 14 11月 3 11:48 alice.txt
-rw-rw-r--. 1 bob devops 32 11月 3 11:50 bob.txt
[bob@localhost public]$ cat alice.txt
This is alice
[bob@localhost public]$ echo "This is Bob Write" >> alice.txt
[bob@localhost public]$ cat alice.txt
This is alice
This is Bob Write
[bob@localhost public]$ rm -rf alice.txt
rm: 无法删除"alice.txt": 不允许的操作