前言

环境:centos 7.9

linux文件普通权限rwx

Linux中文件的普通权限一般为:rwx,对应与数字表示:421,除此之外,文件还有三种特殊权限,就这是我们本节要讲的三种特殊文件权限。

linux文件特殊权限 suid、sgid、sticky

linux文件的三种特殊权限分别是:suid权限、sgid权限、sticky权限;其中suid权限作用于文件属主,sgid权限作用于属组上,sticky权限作用于other其他上。

suid权限

作用:让普通用户临时拥有该文件的属主的执行权限,suid权限只能应用在二进制可执行文件(命令)上,而且suid权限只能设置在属主位置上。

suid权限使用s表示,增加权限u+s,移除权限u-s;
suid权限也可以使用数字形式表示,0表示去除suid权限,4表示添加suid权限,而且是在原权限的数字表达形式开头加0或4,如:0755移除suid权限,4755添加suid权限。

例如:普通用户执行执行passwd命令时需要去修改/etc/shaow等文件,但ll /etc/shaow发现该文件没有任何权限,即普通用户对/etc/shaow文件是没有写入权限的,所以普通用户是怎么实现成功修改自己的密码的呢?答案是临时拥有了root权限(root超级管理员可以对任何文件进行修改)来实现密码的修改,suid权限只能应用在二进制可执行文件上。

[root@localhost ~]# ll /etc/shadow										#这个文件没有任何权限,只有root超级管理员才能修改
---------- 1 root root 1575 Oct  5 22:48 /etc/shadow
[root@localhost ~]# ll /usr/bin/passwd 
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd				#有个suid权限,这就说明了普通用户执行passwd命令时临时提权到root权限了,所以才有root权限才写到shadow文件

去除suid权限

suid权限的去除可以使用数字或字母的形式添加,如果使用数字,0表示去除权限,4表示添加权限,而且是在原权限的数字表达形式开头加0或4,如下:

[root@localhost ~]# ll /bin/passwd 								#查看passwd命令的权限,有suid权限
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /bin/passwd			#查看passwd命令的权限,有suid权限
[root@localhost ~]# chmod 0755 /usr/bin/passwd					#在原数字表达权限前加0就表示去除suid权限(命令等价于chmod u-s /usr/bin/passwd)
[root@localhost ~]# ll /usr/bin/passwd							#查看passwd命令的权限,没有了suid权限,但这时任何普通用户都修改不了密码,如前面介绍的那样,因为任何普通用户都对shadow文件没有写入权限	
-rwxr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd
[root@localhost ~]# 

增加suid权限

[root@localhost ~]# chmod 4755 /usr/bin/passwd					#在原数字表达权限前加4就表示添加suid权限(命令等价于chmod u+s /usr/bin/passwd)
[root@localhost ~]# ll  /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 Jun 10  2014 /usr/bin/passwd		#suid权限已经添加成功,普通用户可以正常修改他们的密码
[root@localhost ~]# 

sgid权限

作用:sgid权限一般应用在目录上,当一个目录拥有sgid权限时,任何用户在该目录下创建的文件的属组都会继承该目录的属组。

sgid权限也使用s表示,增加权限g+s,移除权限g-s;
sgid权限也可以使用数字形式表示,0表示去除sgid权限,2表示添加sgid权限,而且是在原权限的数字表达形式开头加0或2,如:0755移除sgid权限,2755添加sgid权限。

增加sgid权限

sgid权限也使用s表示,增加权限g+s,移除权限g-s;
sgid权限也可以使用数字形式表示,0表示去除sgid权限,2表示添加sgid权限,而且是在原权限的数字表达形式开头加0或2,如:0755移除sgid权限,2755添加sgid权限。

演示示例:

[zhangsan@localhost ~]$ ll -d Test/
drwxrwxr-x 2 zhangsan nginx 6 Oct 13 20:44 Test/					#查看目录的权限,都是普通权限,属主是zhangsan,属组是nginx
[zhangsan@localhost ~]$ touch Test/file1							#创建一个file1文件
[zhangsan@localhost ~]$ ll Test/file1 
-rw-rw-r-- 1 zhangsan zhangsan 0 Oct 13 20:47 Test/file1			#file1的的属主属组都是zhangsan 
[zhangsan@localhost ~]$ rm -rf Test/file1 							#先删除file1
[zhangsan@localhost ~]$ chmod 2755 Test/							#增加sgid权限
[zhangsan@localhost ~]$ ll -d Test/
drwxr-sr-x 2 zhangsan nginx 19 Oct 13 20:47 Test/					#sgid权限已经增加了,属组位置上有了一个s
[zhangsan@localhost ~]$ touch Test/file2							#重新创建一个file2文件
[zhangsan@localhost ~]$ mkdir Test/test								#也创建一个目录
[zhangsan@localhost ~]$ ll Test/									#查看创建的文件和目录
total 0
-rw-rw-r-- 1 zhangsan nginx    0 Oct 13 20:59 file2					#属组都与Test目录的属组一样,这是因为Test目录具有sgid权限
drwxrwsr-x 2 zhangsan nginx    6 Oct 13 20:59 test
[zhangsan@localhost ~]$ 

移除sgid权限

sgid权限也使用s表示,增加权限g+s,移除权限g-s;
sgid权限也可以使用数字形式表示,0表示去除sgid权限,2表示添加sgid权限,而且是在原权限的数字表达形式开头加0或2,如:0755移除sgid权限,2755添加sgid权限。

演示示例:

[zhangsan@localhost ~]$  chmod g-s Test/							#使用字母形式移除sgid权限
[zhangsan@localhost ~]$ ll -d Test/
drwxr-xr-x 3 zhangsan nginx 44 Oct 13 20:59 Test/					#sgid权限已被移除
[zhangsan@localhost ~]$ 

sticky权限

作用:sticky权限一般针对目录来设置,作用是只允该目录下的文件的创建者删除自己的创建的文件,不允许其他人删除文件。(root用户除外,因为root用户是超级管理员),而且sticky权限只能设置在other位置上。

sticky权限使用t表示,增加权限o+t,移除权限o-t;
sticky权限也可以使用数字形式表示,0表示去除权限,1表示添加权限,而且是在原权限的数字表达形式开头加0或1,如下:如:0755移除sticky权限,1755添加sticky权限。

演示示例:

[root@localhost /]# mkdir /test_tmp								#创建目录
[root@localhost /]# chmod 777 test_tmp/							#设置权限
[zhangsan@localhost /]$ ll test_tmp/ -d							#查看test_tmp的权限
drwxrwxrwx 2 root root 6 Oct  6 17:28 test_tmp/
[zhangsan@localhost test_tmp]$ echo "fsdsdsd" >> zhangsan.txt	#张三用户登录并创建一个zhangsan.txt文件
[lisi@localhost test_tmp]$ echo "fsdsdsd" >> lisi.txt			#李四用户登录并创建一个lisi.txt文件
[zhangsan@localhost test_tmp]$ rm -rf lisi.txt					#张三用户删除李四的lisi.txt文件,正常删除
[lisi@localhost test_tmp]$ rm -rf  zhangsan.txt 				#李四用户删除张三的zhangsan.txt文件,正常删除

以上的删除就是不合理的,因为test_tmp的权限是777权限,所以我们需要使用sticky权限来限制用户自己删除自己创建的文件,不能删除别人的文件。
[root@localhost /]$ ll tmp/ -d									#查看系统默认的tmp目录权限,发现tmp目录就有sticky权限
drwxrwxrwt. 12 root root 4096 Oct  6 17:37 tmp/
[zhangsan@localhost /]$ echo "fsdsdsd" >> zhangsan.txt			#张三用户登录并在tmp目录创建一个zhangsan.txt文件
[lisi@localhost /]$ echo "fsdsdfdfdsd" >> lisi.txt				#张三用户登录并在tmp目录创建一个lisi.txt文件
[zhangsan@localhost tmp]$ ll  zhangsan.txt lisi.txt 			#查看我们创建的文件
-rw-rw-r-- 1 lisi     lisi     7 Oct  6 17:23 lisi.txt
-rw-rw-r-- 1 zhangsan zhangsan 9 Oct  6 17:25 zhangsan.txt
[zhangsan@localhost tmp]$ rm -rf lisi.txt						#张三删除李四的lisi.txt,权限报错,删除使用,这就是因为tmp目录的沾滞位权限t起的作用
rm: cannot remove ‘lisi.txt’: Operation not permitted 
[lisi@localhost tmp]$ rm -rf zhangsan.txt						#李四删除张三的zhangsan.txt,权限报错,删除使用,这就是因为tmp目录的sticky权限起的作用
rm: cannot remove ‘zhangsan.txt	’: Operation not permitted 

移除sticky权限

sticky权限使用t表示,增加权限o+t,移除权限o-t;
sticky权限也可以使用数字形式表示,0表示去除权限,1表示添加权限,而且是在原权限的数字表达形式开头加0或1,如下:如:0755移除sticky权限,1755添加sticky权限。演示示例如下:

[root@localhost /]# chmod -R o-t /test_tmp/						#移除sticky权限,加-R表示对目录递归
[root@localhost /]# chmod -R 0777 /test_tmp/					#移除sticky权限,加-R表示对目录递归

增加sticky权限

[root@localhost /]# chmod -R 1777 /test_tmp/					#等价于chmod -R o+t /test_tmp/,加-R表示对目录递归
[root@localhost /]# ll -d test_tmp/
drwxrwxrwt 2 root root 6 Oct  6 17:28 test_tmp/
[root@localhost /]#
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐