suid : set uid。当一个设置了SUID 位的可执行文件被执行时,该文件将以所有者的身份运行,也就是说无论谁来执行这个文件,他都有文件所有者的特权。
误区: 以前老是以为,只要有了这个权限就可以以文件所属者的身份运行了。如下文件makedir的权限为4700(-rws------)用户为root用户,组为root组。
[nick@localhost ~]$ ls -l
total 72
-rws------ 1 root root 4740 Mar 13 15:18 makedir
运行makedir,老是提示权限拒绝
[nick@localhost ~]$ ./makedir
-bash: ./makedir: Permission denied
想不明白了,不是说执行时会变成该文件的所有者么,那么变成所有者肯定有执行权限啊,为什么会提示权限不足。
仔细想一想上面说的 可执行文件被执行时,该文件将以所有者的身份运行。要执行文件必须有可执行权限,所有我的理解是 在可执行文件运行之后,执行者将变成文件的所有者,
所以要想让这个特权生效就必须改文件的可执行权限。
下面是我做的实验
[root@localhost ~]# cd /home/nick
[root@localhost nick]# vi makedir.c
输入以下内容(这个程序会在运行之后创建一个名为newdir文件夹)
#include
#include
int main(void)
{
mkdir("newdir", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
}
//编译程序
[root@localhost nick]# gcc -c makedir.c
[root@localhost nick]# gcc -o makedir makedir.o
//给makedir设置权限(普通的755权限)
[root@localhost nick]# chmod 755 makedir
[root@localhost nick]# ls -l makedir
-rwxr-xr-x 1 root root 4740 Mar 13 15:18 makedir
//切换为nick用户
[root@localhost nick]# su - nick
//执行makedir 注意看运行后的结果 newdir文件夹的 所属者为nick 所属组为nick
[nick@localhost ~]$ ./makedir
[nick@localhost ~]$ ls -l
total 12
-rwxr-xr-x 1 root root 4740 Mar 13 15:18 makedir
drwxrwxr-x 2 nick nick 4096 Mar 13 15:57 newdir
//退出nick 回到root
[nick@localhost ~]$ exit
[root@localhost nick]#
//删除 newdir文件夹
[root@localhost nick]# rmdir newdir
[root@localhost nick]# ls -l
total 8
-rwxr-xr-x 1 root root 4740 Mar 13 15:18 makedir
//更改 makedir的权限(4755带suid的特殊权限)
[root@localhost nick]# chmod 4755 makedir
[root@localhost nick]# ls -l
total 8
-rwsr-xr-x 1 root root 4740 Mar 13 15:18 makedir
//切换为nick用户
[root@localhost nick]# su - nick
//运行makedir 注意这时候newdir的所属者已经变成root了(所属组还是nick),看到区别了吧
[nick@localhost ~]$ ./makedir
[nick@localhost ~]$ ls -l
total 12
-rwsr-xr-x 1 root root 4740 Mar 13 15:18 makedir
drwxrwxr-x 2 root nick 4096 Mar 13 16:04 newdir