linux中的setuid、setgid以及sticky bit

马谦马谦马谦 Linux评论1,260字数 2487阅读8分17秒阅读模式

setuid的作用是以该命令拥有者的权限去执行,比如修改密码的passwd命令,执行passwd时会拥有root权限,不然就修改不了/etc/passwd文件了。

而setgid的意思是以命令所有组的权限去执行,它们的标志位是s,出现在x的地方,例如-rwsr-xr-x。手动添加这一位的方式:

对于以下程序:

编译后加上UID权限,分别使用不同的身份运行:

可以看到,不管是以当前用户还是root用户运行,实际用户id都是一样的,都是其所有者。

二、黏着位(sticky bit)

关于黏着位找了很多资料也没有找到明确的描述,网上众说纷纭也没有清晰的描述出它的作用,最后还是在《UNIX环境高级编程》中找到一些更明确的解释:

The S_ISVTX bit has an interesting history. Onversions of the UNIX System that predated demand paging, this bit was known as the sticky bit.If it was set for an executable program file, then the first time the program was executed, a copy of the program’s text was saved in the swap area when the process terminated. (The text portion of a program is the machine instructions.) The program would then load into memory morequickly the next time it was executed, because the swap area was handled as a contiguous file, as compared to the possibly random location of data blocks in a normal UNIX file system. The sticky bit was often set for common application programs, such as the text editor and the passes of the C compiler. Naturally,therewas a limit to the number of sticky files that could be contained in the swap area beforerunning out of swap space, but it was a useful technique. The name sticky came about because the text portion of the file stuck around in the swap area until the system was rebooted. Later versions of the UNIX System referred to this as the saved-text bit; hence the constant S_ISVTX.With today’s newer UNIX systems, most of which have a virtual memory system and a faster file system, the need for this technique has disappeared.

在早期的unix系统中,如果一个程序被设置了黏着位,那么当它第一次执行结束之后,程序的正文段会被写入到交换空间中,以此加快后续使用的加载速度。因为交换空间是顺序存放,而磁盘上是随机的。它通常被设置成经常被使用的公用程序例如文本编辑器、编译器等,会一直持续到系统重启。

在后续的unix系统都设计了新的更快速的文件系统,所以这种用法逐渐消失。而对于这一黏着位“现在的用途”的描述是:

On contemporary systems, the use of the sticky bit has been extended. The Single UNIX Specification allows the sticky bit to be set for a directory. If the bit is set for a directory, a file in the directory can be removed or renamed only if the user has write permission for the directory and meets one of the following criteria:

  1. Owns the file
  2. Owns the directory
  3. Is the superuser

The directories /tmp and /var/tmp are typical candidates for the sticky bit—they are directories in which any user can typically create files. The permissions for these two directories are often read, write, and execute for everyone (user, group, and other). But users should not be able to delete or rename files owned by others.

现在的系统里面,黏着位已经被扩展了,它被用于目录权限上,如果一个目录设置了这一位,这个目录下的文件就只能被满足以下条件的用户重命名或者删除:

  1. 所有者
  2. 当前目录所有者
  3. 超级用户

目录/tmp和/var/tmp是设置粘住位的候选者—这两个目录是任何用户都可在其中创建文件的目录。这两个目录对任一用户 (用户、组和其他)的许可权通常都是读、写和执行。但是用户不应能删除或更名属于其他人的文件,为此在这两个目录的文件方式中都设置了粘住位

手动给目录添加sticky位:

加上这一个权限之后目录的颜色也有变化:

linux中的setuid、setgid以及sticky bit

 
马谦马谦马谦
  • 本文由 马谦马谦马谦 发表于 2018年11月3日14:34:33
  • 转载请务必保留本文链接:https://www.dyxmq.cn/linux/linux-setuid-setgid-sticky-bit.html
CentOS5 YUM源 CentOS

CentOS5 YUM源

centos5已停止了维护,国内大部分的源也停止维护,找了很久都没有找到可用的源。最后才发现,centos早期的源都已经vault.centos.org网站下,国内的镜像源也都改成了centos-va...
openssl命令的用法 Linux

openssl命令的用法

openssl是目前使用最广泛的ssl库之一,除了提供全面的ssl加密库以外,还提供了一些基础的命令行工具用于测试,目前绝大多数的软件都是使用openssl库来进行ssl交互,很多系统默认都自带了op...
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证