环境:Fedora 19,kernel 3.10.7-200.fc19.x86_64
问题:chmod无法用数字表示法去掉文件夹的suid或sgid
理论上,我们既然能用"7XXX"为文件加上suid、sgid和sticky属性,就应该能用0XXX为其去掉suid、sgid和sticky属性。就像下面的演示,结果和我们的预期是一样的:
[[email protected] ~]$ touch testfile;ll testfile -rw-rw-r--. 1 Dotcra Dotcra 0 Aug 19 11:22 testfile [[email protected] ~]$ chmod 7775 testfile ;ll testfile -rwsrwsr-t. 1 Dotcra Dotcra 0 Aug 19 11:22 testfile [[email protected] ~]$ chmod 0775 testfile ;ll testfile -rwxrwxr-x. 1 Dotcra Dotcra 0 Aug 19 11:22 testfile
那么文件夹呢?我想应该是一样的,为什么不呢? 但实际情况却让人摸不着头脑——我以为0775会去掉suid、sgid和sticky,但实际上只去掉了sticky,剩下的suid、sgid非要用chmod -s才能去掉。
[[email protected] ~]$ mkdir testdir;ll -d testdir drwxrwxr-x. 2 Dotcra Dotcra 4096 Aug 19 11:25 testdir [[email protected] ~]$ chmod 7775 testdir/;ll -d testdir/ drwsrwsr-t. 2 Dotcra Dotcra 4096 Aug 19 11:25 testdir/ [[email protected] ~]$ chmod 0775 testdir/;ll -d testdir/ drwsrwsr-x. 2 Dotcra Dotcra 4096 Aug 19 11:25 testdir/ [[email protected] ~]$ chmod -s testdir/;ll -d testdir/ drwxrwxr-x. 2 Dotcra Dotcra 4096 Aug 19 11:25 testdir/
加入-v选项看起来更直观:
[[email protected] ~]$ ll -d testdir/ drwsrwsr-t. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/ [[email protected] ~]$ chmod -v 0775 testdir/;ll -d testdir/ mode of ‘testdir/’ changed from 7775 (rwsrwsr-t) to 6775 (rwsrwsr-x) drwsrwsr-x. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/ [[email protected] ~]$ chmod -v 1775 testdir/;ll -d testdir/ mode of ‘testdir/’ changed from 6775 (rwsrwsr-x) to 7775 (rwsrwsr-t) drwsrwsr-t. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/ [[email protected] ~]$ chmod -v 2775 testdir/;ll -d testdir/ mode of ‘testdir/’ changed from 7775 (rwsrwsr-t) to 6775 (rwsrwsr-x) drwsrwsr-x. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/ [[email protected] ~]$ chmod -v 3775 testdir/;ll -d testdir/ mode of ‘testdir/’ changed from 6775 (rwsrwsr-x) to 7775 (rwsrwsr-t) drwsrwsr-t. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/ [[email protected] ~]$ chmod -v 4775 testdir/;ll -d testdir/ mode of ‘testdir/’ changed from 7775 (rwsrwsr-t) to 6775 (rwsrwsr-x) drwsrwsr-x. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/ [[email protected] ~]$ chmod -v 5775 testdir/;ll -d testdir/ mode of ‘testdir/’ changed from 6775 (rwsrwsr-x) to 7775 (rwsrwsr-t) drwsrwsr-t. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/ [[email protected] ~]$ chmod -v 6775 testdir/;ll -d testdir/ mode of ‘testdir/’ changed from 7775 (rwsrwsr-t) to 6775 (rwsrwsr-x) drwsrwsr-x. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/ [[email protected] ~]$ chmod -v 7775 testdir/;ll -d testdir/ mode of ‘testdir/’ changed from 6775 (rwsrwsr-x) to 7775 (rwsrwsr-t) drwsrwsr-t. 2 Dotcra Dotcra 4096 Aug 22 15:31 testdir/
很明显,当你试图给予的权限不带suid或sgid时,shell会自动加上,也就是说特殊权限位你无法减去4或2,只要你给予了1(sticky权限),结果全都是4+2+1,反之则是4+2+0;所以就像在反馈结果里看到的,1775、3775和5775都变成7775,而0775、2775、4775和6775都变成了6775。 切换root一样的结果,看来和用户权限无关。 Fedora 17上测试也是这个状况。 而在办公室的RHEL 5.4上测试却正常,另外有网友回复说RHEL 4.6也正常。 看来这是Fedora上才有的现象。 搞不懂,先留个疑问在这儿。
Add new comment