文件特殊权限(SUID,SGID,Sticky)

三种特殊权限:SUID,SGID,Sticky

SUID

安全上下文,何为安全上下文

安全上下是一个访问控制属性,是selinux中的重要组成部分,在进行移动时,不会改变文件的属性和权限,而复制的过程会改变文件的属性,而安全上下文是一个访问的凭证,特定的文件被特定的程序访问,安全上下文会关闭系统认为不安全的功能,当安全上下文相匹配时才允许访问。

前提:进程有属主和属组;文件有属主和属组

  1. 任何一个可执行程序文件能不能启动为进程,取决发起者对程序文件是否拥有执行权限
  2. 启动为进程之后,其进程的属主为发起者,进程的属组为发起者所属的组
  3. 进程访问文件时的权限,取决于进程的发起者
  • 进程的发起者,同文件的属主:则应用文件属主权限
  • 进程的发起者,属于文件属组;则应用文件属组权限
  • 应用文件“其它”权限

二进制的可执行文件上SUID权限功能:

任何一个可执行程序文件能不能启动为进程:取决发起者对程序文件是否拥有执行权限

启动为进程之后,其进程的属主为原程序文件的属主

SUID只对二进制可执行程序有效

SUID设置在目录上无意义

SUID权限设定:

chmod u+s FILE... 
chmod 6xxx FILE 
chmod u-s FILE...

范例:

[root@centos7 ~]# ll `which passwd`
-rwsr-xr-x. 1 root root 27856 Apr  1  2020 /usr/bin/passwd
[root@centos7 ~]# ll `which cat`
-rwxr-xr-x. 1 root root 54080 Aug 20  2019 /usr/bin/cat

实验:

#查看vim原权限
[root@centos7 ~]# ll `which vim`
-rwxr-xr-x. 1 root root 2337208 Dec 16  2020 /usr/bin/vim
#赋予suid权限 
[root@centos7 ~]# chmod u+s `which vim`
[root@centos7 ~]# ll `which vim`
-rwsr-xr-x. 1 root root 2337208 Dec 16  2020 /usr/bin/vim

#切换普通用户
[root@centos7 ~]# su - song
Last login: Tue Sep  6 10:56:17 CST 2022 on pts/0
[song@centos7 ~]$ vim /etc/passwd
......
song:x:1000:1000:song is superboy:/home/song:/bin/bash   #修改内容song is superboy
......
#修改后,然后wq!强制保存,可以实现

SGID

二进制的可执行文件上SGID权限功能:

任何一个可执行程序文件能不能启动为进程:取决发起者对程序文件是否拥有执行权限

启动为进程之后,其进程的属组为原程序文件的属组

目录上的SGID权限功能

默认情况下,用户创建文件时,其属组为此用户所属的主组,一旦某目录被设定了SGID,则对此目录有

写权限的用户在此目录中创建的文件所属的组为此目录的属组,通常用于创建一个协作目录

SGID权限设定:

chmod g+s FILE... 
chmod 2xxx FILE 
chmod g-s FILE...

实验:

#root下创建一个目录/test ,赋予权限2777,属组设置为song1
[root@centos7 ~]# mkdir /test
[root@centos7 ~]# ll -d /test/
drwxr-xr-x 2 root root 6 Sep  6 11:20 /test/
[root@centos7 ~]# chmod 2777 /test/
[root@centos7 ~]# chown root:song1 /test/
[root@centos7 ~]# ll -d /test/
drwxrwsrwx 4 root song1 86 Sep  6 11:28 /test/
[root@centos7 ~]# su - song
Last login: Tue Sep  6 11:16:52 CST 2022 on pts/0
[song@centos7 ~]$ touch /test/aa1
[song@centos7 ~]$ mkdir /test/aa2
[song@centos7 ~]$ ll /test/
total 0
-rw-rw-r-- 1 song song1 0 Sep  6 11:29 aa1
drwxrwsr-x 2 song song1 6 Sep  6 11:29 aa2

[song@centos7 ~]$ 

结果显示,song在/test目录下创建的文件和目录都自动继承了song1的属组
而且新目录的权限也继承了SGID.
所以设定了SGID的目录中的所有新建文件和目录都会自动属于song1组.

STICKY

具有写权限的目录通常用户可以删除该目录中的任何文件,无论该文件的权限或拥有权

在目录设置Sticky 位,只有文件的所有者或root可以删除该文件

通俗来讲就是

  • 对于一个多人可写的目录,如果设置了sticky,则每个用户仅能删除和改名自己的文件或目录;

  • 只能作用在目录上.普通文件设置无意义,且会被linux内核忽略

  • 用户在设置Sticky权限的目录下新建的目录不会自动继承Sticky权限

Sticky权限设定:

chmod o+t DIR... 
chmod 1xxx DIR 
chmod o-t DIR...

范例:

# ll -d /tmp/
drwxrwxrwt. 9 root root 161 Sep  6 10:39 /tmp/

实验:

#创建一个目录
[root@centos7 ~]# mkdir -p  /test/mkfile
[root@centos7 ~]# ll -d /test/mkfile/
drwxr-xr-x 2 root root 6 Sep  6 14:45 /test/mkfile/
[root@centos7 ~]# chmod 777 /test/mkfile
[root@centos7 ~]# ll -d /test/mkfile/
drwxrwxrwx 2 root root 6 Sep  6 14:45 /test/mkfile/
#分别切换用户song和song1在里边新建文件
[root@centos7 ~]# su - song
Last login: Tue Sep  6 11:29:32 CST 2022 on pts/0
[song@centos7 ~]$ touch /test/mkfile/aa1
[song@centos7 ~]$ mkdir /test/mkfile/mkfile1
[song@centos7 ~]$ ll /test/mkfile/
total 0
-rw-rw-r-- 1 song song 0 Sep  6 14:48 aa1
drwxrwxr-x 2 song song 6 Sep  6 14:48 mkfile1

[song@centos7 ~]$ su - song1
Password: 
Last login: Tue Sep  6 11:17:14 CST 2022 on pts/0
[song1@centos7 ~]$ touch /test/mkfile/aa2
[song1@centos7 ~]$ mkdir /test/mkfile/mkfile2
[song1@centos7 ~]$ ll /test/mkfile/
total 0
-rw-rw-r-- 1 song  song  0 Sep  6 14:48 aa1
-rw-rw-r-- 1 song1 song1 0 Sep  6 14:49 aa2
drwxrwxr-x 2 song  song  6 Sep  6 14:48 mkfile1
drwxrwxr-x 2 song1 song1 6 Sep  6 14:49 mkfile2

#这时song和song1可以删除自己建立和别人建立的文件

#这时给/test/mkfile目录设置一个sticky位
[root@centos7 ~]# chmod +t /test/mkfile/
[root@centos7 ~]# ll -d /test/mkfile/
drwxrwxrwt 4 root root 58 Sep  6 14:51 /test/mkfile/

#这时切换到song用户中,只能对自己建立的文件操作,其他则无法操作
[song@centos7 ~]$ ll /test/mkfile/
total 0
-rw-rw-r-- 1 song  song  0 Sep  6 14:48 aa1
-rw-rw-r-- 1 song1 song1 0 Sep  6 14:49 aa2
drwxrwxr-x 2 song  song  6 Sep  6 14:48 mkfile1
drwxrwxr-x 2 song1 song1 6 Sep  6 14:49 mkfile2
[song@centos7 ~]$ mv /test/mkfile/aa2 /tmp/
mv: cannot move ‘/test/mkfile/aa2’ to ‘/tmp/aa2’: Operation not permitted
[song@centos7 ~]$ rm -rf /test/mkfile/aa2 
rm: cannot remove ‘/test/mkfile/aa2’: Operation not permitted
[song@centos7 ~]$ rm -rf /test/mkfile/aa1 
[song@centos7 ~]$ rm -rf /test/mkfile/mkfile2/
rm: cannot remove ‘/test/mkfile/mkfile2/’: Operation not permitted

#sticky生效了

权限位映射

SUID: user,占据属主的执行权限位

s:属主拥有x权限

S:属主没有x权限

SGID: group,占据属组的执行权限位

s: group拥有x权限

S:group没有x权限

Sticky: other,占据other的执行权限位

t:other拥有x权限

T:other没有x权限

总结:

二进制文件目录
SUID此用户将继承此程序的所有者权限无意义
SGID此用户将继承此程序的所属组权限此目录下所有用户新建文件都自动继承此目录的用户组
Sticky无意义目录中每个用户仅能删除、移动或改名自己的文件或目录
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值