chmod命令高级篇( SetUID与SetGID标识位)

在Android中很多人都知道通过su来获得root权限,但是不知道大家有没有研究过su文件的权限

/bin/su -rws-r-xr-x 1 root root

   注意看到没有那个 s 权限是什么呢? s 就是SetUID的标识,该 设置使文件在执行阶段时其有效用户id (可以通过g etuid()取得 ) 为该文件拥有者的id。 而内核检查一个进程是否具有访问某权限时,是使用进程的有效用户ID来进行检查的,因而 使其在执行阶段具有文件所有者的权限.。典型的就是su文件。 还有一点就是当其它用户使用带用Setuid标识的命令时,自己对此命令也必需有X(执行)的权限,否则一切都免谈..这点应该不难理解的..^_^
在linux中,文件的权限除了了常见的rwxrwxrwx权限,还有以下标识用于表示权限:
setuid:  设置使文件在执行阶段时其有效用户id (可以通过g etuid()取得 ) 为该文件拥有者的id,。 而内核检查一个进程是否具有访问某权限时,是使用进程的有效用户 ID 来进行检查的,因而 使其在执行阶段具有文件所有者的权限。典型的就是su文件。
setgid: 该权限只对目录有效. 目录被设置该位后, 任何用户在此目录下创建的文件都具有和该目录所属的组相同的组.
sticky bit: 该位可以理解为防删除位. 一个文件是否可以被某用户删除, 主要取决于该文件所属的组是否对该用户具有写权限. 如果没有写权限, 则这个目录下的所有文件都不能被删除, 同时也不能添加新的文件. 如果希望用户能够添加文件但同时不能删除文件, 则可以对文件使用sticky bit位. 设置该位后, 就算用户对目录具有写权限, 也不能删除该文件.
下面说一下如何操作这些标志:
操作这些标志与操作文件权限的命令是一样的, 都是 chmod. 有两种方法来操作,
1) chmod u+s temp -- 为temp文件加上setuid标志. (setuid 只对文件有效)
chmod g+s tempdir -- 为tempdir目录加上setgid标志 (setgid 只对目录有效)
chmod o+t temp -- 为temp文件加上sticky标志 (sticky只对文件有效)

实例1
@ubuntu:~/share/temp$ touch test.exe
robin@ubuntu:~/share/temp$ ls -l
-rw-r--r-- 1 robin robin         0 2013-09-02 15:26 test.exe
robin@ubuntu:~/share/temp$ chmod u+s test.exe
robin@ubuntu:~/share/temp$ ls -l
-rwSr--r-- 1 robin robin         0 2013-09-02 15:26 test.exe
robin@ubuntu:~/share/temp$ chmod o+s test.exe
robin@ubuntu:~/share/temp$ ls -l
-rwSr--r-- 1 robin robin         0 2013-09-02 15:26 test.exe
实例2
robin@ubuntu:~/share/temp$ rm test.exe
robin@ubuntu:~/share/temp$ touch test.exe
robin@ubuntu:~/share/temp$ ls -l
-rw-r--r-- 1 robin robin         0 2013-09-02 15:28 test.exe
robin@ubuntu:~/share/temp$ chmod a+t test.exe
robin@ubuntu:~/share/temp$ ls -l
-rw-r--r-T 1 robin robin         0 2013-09-02 15:28 test.exe
实例3
robin@ubuntu:~/share/temp$ rm test.exe
robin@ubuntu:~/share/temp$ mkdir testdir
robin@ubuntu:~/share/temp$ ls -l
drwxr-xr-x 2 robin robin      4096 2013-09-02 15:29 testdir
robin@ubuntu:~/share/temp$ su
密码: 
root@ubuntu:/home/robin/share/temp# ls -l
drwxr-xr-x 2 robin robin      4096 2013-09-02 15:29 testdir
root@ubuntu:/home/robin/share/temp# cd testdir
root@ubuntu:/home/robin/share/temp/testdir# ls
root@ubuntu:/home/robin/share/temp/testdir# touch 1.txt
root@ubuntu:/home/robin/share/temp/testdir# ls -l
-rw-r--r-- 1 root root 0 2013-09-02 15:30 1.txt
root@ubuntu:/home/robin/share/temp/testdir# exit
exit
robin@ubuntu:~/share/temp$ chmod g+s testdir
robin@ubuntu:~/share/temp$  ls -l
drwxr-sr-x 2 robin robin      4096 2013-09-02 15:30 testdir
robin@ubuntu:~/share/temp$ su
密码: 
root@ubuntu:/home/robin/share/temp# cd testdir
root@ubuntu:/home/robin/share/temp/testdir# ls
1.txt
root@ubuntu:/home/robin/share/temp/testdir# touch 2.txt
root@ubuntu:/home/robin/share/temp/testdir# ls -l
-rw-r--r-- 1 root root  0 2013-09-02 15:30 1.txt
-rw-r--r-- 1 root robin 0 2013-09-02 15:31 2.txt
实例4
robin@ubuntu:~/share/temp/temp$ touch 1.exe
robin@ubuntu:~/share/temp/temp$ ls -l
-rw-r--r-- 1 robin robin 0 2013-09-02 15:48 1.exe
robin@ubuntu:~/share/temp/temp$ chmod 7 777 1.exe
robin@ubuntu:~/share/temp/temp$ ls -l
-rwsrwsrwt 1 robin robin 0 2013-09-02 15:48 1.exe
结束!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值