Technology Sharing

  • 首页
  • 资料分享
  • 在线工具
    • 随机密码生成器
  • 介绍
  • RSS
  • privacy
  • 云产品推广
    • 腾讯云
    • 阿里云
Share IT knowledge
  1. 首页
  2. linux
  3. 正文

了解 Linux 中的 SUID、SGID 和 Sticky bit

2022年7月24日 231点热度 0人点赞 0条评论

Linux对目录和文件权限的管理是安全管理的重要部分,除了普通的rwx权限之外,每一个文件和目录还存在SUID、SGID、Sticky Bite三个特殊权限。

SUID

不知大家是否想过这个问题:/etc/passwd文件保存着所用用户的信息,而且其属主为root用户,为什么其他用户也可以通过passwd命令修改自己的密码?

$ ls -l /etc/passwd
-rw-r--r-- 1 root root 1718 Mar 16  2017 /etc/passwd

通过对passwd工具的探究发现了一些奇怪的内容——属主权限的'x'位上显示的是's'权限,这就是SUID权限。

$ which passwd
/usr/bin/passwd
$ ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 30768 Feb 17  2012 /usr/bin/passwd

Set-User Identification,简称SUID权限,其作用是:在工具的执行过程中,将属主的权限赋予执行者。如上例中用户修改密码时,实际上拥有的是root权限,所以才能够操作/etc/passwd文件;另一个很好的例子是su命令。

$ which su
/bin/su
$ ls -l /bin/su 
-rwsr-xr-x. 1 root root 34904 Oct 17  2013 /bin/su

如何设置SUID权限

$ chmod u+s file
$ chmod 4xxx file

以上两种方式均可以设置文件的SUID权限

  • 第一种方式采用的是符号模式,给文件的属主user添加s权限(SUID);
  • 第二种方式采用了八进制模式,特殊权限SUID、SGID、Stick
    Bit依次各占一个二进制位,构成一个八进制位,其中4(100)表示的SUID位置位,同时SGID和粘着位清零,xxx分别表示属主、属组、其他用户对该文件的权限。

SUID权限的存在允许了在不修改文件属主或属组的前提下释放文件的执行权限,仅对二进制文件有效,且仅在执行过程中有效,但同时也容易造成安全漏洞,不应轻易设置。

SGID

当's'权限存在于目录中时,就称为Set-Group Identification权限。SGID权限的作用在于:若一个目录设置了GID权限,则在用户在此目录下新建的文件具有与目录相同的群组。

SGID权限常用于文件共享

$ mkdir testdir
$ ls -l
total 0
drwxr-xr-x 2 root root  6 Oct 25 20:58 testdir
$ chgrp shared testdir/
$ ls -l
total 0
drwxr-xr-x 2 root shared 6 Oct 25 20:58 testdir
$ chmod g+s testdir/
$ umask 002
$ cd testdir/
$ touch testfile
$ ll
total 0
-rw-rw-r-- 1 root shared 0 Oct 25 21:01 testfile

将共享文件夹添加SGID权限,通过umask 002使得该用户新建的文件对属组用户可写,进而该用户在共享文件夹下生成文件的属组不再是用户的默认属组,而是共享文件组,从而实现文件共享。

如何设置SGID权限

$ chmod g+s dir
$ chmod 2xxx dir

Sticky Bite

对Linux文件系统中/tmp文件夹的观察可以发现,其他用户权限中的'x'权限被't'权限取代了,这就是Stick Bite,又称为粘滞位。

$ ls -ld /tmp
drwxrwxrwt. 9 root root 195 Oct 25 21:01 /tmp

粘滞位的作用是:对目录设置粘滞位后,只有文件的属主和root权限才有权删除该文件。

这就回到了tmp目录的用法,作为临时文件目录,对所有用户都开放了rwx权限,写权限带来的负面是所有用户均可以删除目录中的文件,包括其他用户的文件,如何实现允许用户添加文件但同时不能删除文件?可以通过对目录设置粘滞位,实现只有文件的属主和root用户可以删除该文件。

如何设置粘滞位

$ chmod o+t dir
$ chmod 1xxx dir

注意事项

$ mkdir test
$ touch testfile
$ chmod -x test testfile
$ ls -l
drw-r--r-- 2 root root 4096 Oct 25 21:24 test
-rw-r--r-- 1 root root    0 Oct 25 21:26 testfile
$ chmod +s test testfile
$ ls -l
drwSr-Sr-- 2 root root 4096 Oct 25 21:24 test
-rwSr-Sr-- 1 root root    0 Oct 25 21:26 testfile
$ chmod +t test
$ ls -l
drwSr-Sr-T 2 root root 4096 Oct 25 21:24 test
-rwSr-Sr-- 1 root root    0 Oct 25 21:26 testfile
$ chmod +x test testfile
$ ls -l
drwsr-sr-t 2 root root 4096 Oct 25 21:25 test
-rwsr-sr-x 1 root root    0 Oct 25 21:26 testfile

  • 文件或目录具'x'权限,则为's' 't'权限;不具有'x'权限时,则分别为'S' 'T'权限
  • 粘滞位只存在于目录的特殊权限中
打赏微海报分享
标签: 暂无
最后更新:2022年7月24日

nico

Linux运维工程师 软考网络工程师 && 软考信息安全工程师

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

nico

Linux运维工程师
软考网络工程师 && 软考信息安全工程师

最新 热点 随机
最新 热点 随机
linux部署JDK环境 [Solved] MariaDB import issue: Error at line 1: Unknown command '\-'. Could not retrieve mirrorlist http://mirrorlist.centos.org/ CentOS 7 Kubernetes如何删除卡在“Terminating”状态的命名空间 解决GitLab Runner签名无效 如何禁用 Ubuntu "Daemons using outdated libraries" 弹出窗口
powershell 自动补全 curl 获取状态码和跳转链接 GitLab 项目未通过最后一次存储库检查 解锁WordPress:上传文件大小限制调整指南 nginx正向代理 如何在不使用 FTP 的情况下直接更新 WordPress
最近评论
woodcockkienzlelsj8o9+73s48g9rr3m0@gmail.com 发布于 11 个月前(07月17日) necessitatibus corporis et odit nam quo harum et c...
RonaldG 发布于 11 个月前(07月07日) Very interesting topic, thank you for putting up.&...
小黑 发布于 1 年前(12月28日) 不错
nico 发布于 2 年前(02月09日) 嘻嘻嘻!!!
Bruse 发布于 2 年前(02月09日) 我来啦!!!
nico 发布于 3 年前(12月10日) 方便查询
Justin 发布于 3 年前(12月10日) 写的很好,谢谢分享!我 Mark 一下~
nico 发布于 3 年前(11月18日) 主题的原因吧
Justin 发布于 3 年前(09月22日) 这篇文章的质量很高呀!写得很详细~ 话说你的文章是隐藏了发布时间吗?
Justin 发布于 3 年前(08月27日) 好家伙,这标题乍一看还以为你打算进军英文技术写作领域了🤓

COPYRIGHT © 2023 Technology Sharing. ALL RIGHTS RESERVED.

备案图标 皖公网安备34132402000202 皖ICP备2023004851号-1