前面学习了Linux文档的权限(见 http://www.linuxidc.com/Linux/2011-11/47894.htm ),鸟哥说,文件rwx的权限是读取写入与执行的权限,但与删除文件并没有关系。而目录rwx则代表读取目录列表,添加删除目录中的文件,切换进入目录!对于此,我做了一项权限问题的试验。
首先,以sunjiebin用户测试下test文件的rwx权限问题:
当只有r权限时:
[sunjiebin@rhel6 test]$ ll
总用量 4
-r--------. 1 sunjiebin sunjiebin 6 10月 1 17:34 test
[sunjiebin@rhel6 test]$ echo hello2 >>test
-bash: test: 权限不够
[root@rhel6 test]# ./test
-bash: ./test: 权限不够
[sunjiebin@rhel6 test]$ cat test
hello
可以看出是不可以写入与执行的。但可以查看。
当只有w权限时:
[sunjiebin@rhel6 test]$ chmod u=w test
[sunjiebin@rhel6 test]$ ll
总用量 4
--w-------. 1 sunjiebin sunjiebin 6 10月 1 17:34 test
[sunjiebin@rhel6 test]$ cat test
cat: test: 权限不够
[sunjiebin@rhel6 test]$ echo test1 >>test
[sunjiebin@rhel6 test]$ ./test
-bash: ./test: 权限不够
我们用root来看看写入是否成功
[root@rhel6 test]# cat test
hello
test1
可以看出可以写入成功了,但sunjibin用户自己却不能查看写入的文件
由上可知,当只有w权限时,可以写入文件,但不可读,不可执行。
注意:这里不能用vi来编辑test。如果用vi,我在vi里面输入test3,结果如下:
[sunjiebin@rhel6 test]$ vi test
test3
-- 插入 -- W10: 警告: 正在修改一个只读文件
:wq! [root@rhel6 test]# cat test
test3
可以看出,在用vi编辑的时候,我们并不能看到test文件中的内容,而且编辑后的文件,也已经没有了一开始的内容。为什么没有原来的内容了呢?其实这本质上vi并没有编辑真正的修改test文件,而是在编写一个同名的test文件。在保存的时候,再将原来的test覆盖掉。覆盖其实就是执行了删除和新建的动作。所以,原来的test文件就没有了!下面在讲目录权限的时候,我会验证这句话的正确性!
当只有x权限时:
[sunjiebin@rhel6 test]$ chmod u=x test
[sunjiebin@rhel6 test]$ ll
总用量 4
---x------. 1 sunjiebin sunjiebin 6 10月 1 17:59 test
[sunjiebin@rhel6 test]$ cat test
cat: test: 权限不够
[sunjiebin@rhel6 test]$ echo test >>test
-bash: test: 权限不够
[sunjiebin@rhel6 test]$ ./test
bash: ./test: 权限不够
咦,怎么执行权限也没有了呢?读写权限都没有了可以理解,那么执行权限怎么会没有呢?因为我们执行一个文件的时候,肯定是先要读取文件啊,读都不能读了,怎么进行下面的执行呢?当我们改为rx权限时,执行结果如下:
[sunjiebin@rhel6 test]$ chmod u=rx test
[sunjiebin@rhel6 test]$ ./test
./test: line 1: test3: command not found
可以看到,没有说权限不够了吧~ 所以啊,要文件能执行,必需还要有r权限哦。