Linux setUid (SUID) 的 使用 及 原理

为什么普通用户可以更改自己的密码?

linux更改密码的原理是就是修改存放密码的文件,具体文件看下面,总之普通用户是没有这个权限去修改。

LOOK——
-rw-r--r-- 1 root root 1787 Oct 27 2009 /etc/passwd
-r-------- 1 root root 1187 Oct 27 2009 /etc/shadow

关键就在于passwd 这个命令。

-rwsr-xr-x 1 root root 22960 Jul 17 2006 /usr/bin/passwd

这里有一个s标志位。

setUID的定义:当一个可执行程序具有setUID权限,用户执行这个程序时,将以这个程序所有者的身份执行。

所以,这里普通用户在执行passwd时,其实是在用root的身份执行(所有命令的所有者都是root)。


使用chmod u+s 即可授予s位

setUID=4

或者 chmod 4755


删除s位: chmod u-s

示例:

先把touch命令设置s位。再用普通用户创建文件,看下文件所有者

[gaotong@master bin]$ su root
Password: 
[root@master bin]# chmod u+s /bin/touch 
[root@master bin]# su gaotong
[gaotong@master bin]$ cu ~
bash: cu: command not found
[gaotong@master bin]$ cd ~
[gaotong@master ~]$ touch 1.txt
[gaotong@master ~]$ ls -l 1.txt 
-rw-rw-r-- 1 root gaotong 0 Sep 16 14:55 1.txt
[gaotong@master ~]$ 


你可能感兴趣的:(Linux setUid (SUID) 的 使用 及 原理)