玩转并理解linux中的文件/目录的rwx权限

 

        linux是一个相对安全的系统, 其中的权限更是无处不在。 在本文中, 我们来谈谈linux中的文件/目录的rwx权限。 为了简便起见, 我们仅仅以文件owner的rwx为例。

 

        一. 文件的rwx权限分别是什么意思?

         1. r权限:可读权限, 验证如下:

 

 [taoge@localhost learn_c]$ ls -l
total 0
[taoge@localhost learn_c]$ echo hello > a.txt
[taoge@localhost learn_c]$ ls -l
total 4
-rw-rw-r-- 1 taoge taoge 6 May  6 03:51 a.txt
[taoge@localhost learn_c]$ chmod 000 a.txt 
[taoge@localhost learn_c]$ ls -l
total 4
---------- 1 taoge taoge 6 May  6 03:51 a.txt
[taoge@localhost learn_c]$ cat a.txt 
cat: a.txt: Permission denied
[taoge@localhost learn_c]$ chmod u+r a.txt 
[taoge@localhost learn_c]$ ls -l
total 4
-r-------- 1 taoge taoge 6 May  6 03:51 a.txt
[taoge@localhost learn_c]$ cat a.txt
hello
[taoge@localhost learn_c]$ 

 

 

 

         2. w权限: 可写权限, 验证如下:

 

 [taoge@localhost learn_c]$ ls -l
total 0
[taoge@localhost learn_c]$ touch a.txt
[taoge@localhost learn_c]$ ls -l
total 0
-rw-rw-r-- 1 taoge taoge 0 May  6 03:56 a.txt
[taoge@localhost learn_c]$ chmod 000 a.txt
[taoge@localhost learn_c]$ ls -l
total 0
---------- 1 taoge taoge 0 May  6 03:56 a.txt
[taoge@localhost learn_c]$ chmod u+w a.txt
[taoge@localhost learn_c]$ ls -l
total 0
--w------- 1 taoge taoge 0 May  6 03:56 a.txt
[taoge@localhost learn_c]$ echo hello > a.txt
[taoge@localhost learn_c]$ cat a.txt
cat: a.txt: Permission denied
[taoge@localhost learn_c]$ chmod u+r a.txt
[taoge@localhost learn_c]$ cat a.txt
hello
[taoge@localhost learn_c]$ 

 

 

 

     3. x权限:可执行权限, 验证如下:

 

 [taoge@localhost learn_c]$ ls -l
total 4
-rw-rw-r-- 1 taoge taoge 65 May  6 04:02 test.c
[taoge@localhost learn_c]$ cat test.c 
#include <stdio.h>

int main()
{
printf("good\n");
return 0;
}
[taoge@localhost learn_c]$ gcc test.c 
[taoge@localhost learn_c]$ ls -l
total 12
-rwxrwxr-x 1 taoge taoge 4638 May  6 04:04 a.out
-rw-rw-r-- 1 taoge taoge   65 May  6 04:02 test.c
[taoge@localhost learn_c]$ ./a.out 
good
[taoge@localhost learn_c]$ chmod 000 a.out 
[taoge@localhost learn_c]$ ./a.out
bash: ./a.out: Permission denied
[taoge@localhost learn_c]$ chmod u+x a.out 
[taoge@localhost learn_c]$ ./a.out 
good
[taoge@localhost learn_c]$ 
 
 
        

       二. 目录的rwx权限分别是什么意思?

        1. r权限:可读权限(可列举查看目录下的内容), 验证如下:

 

[taoge@localhost learn_c]$ ls -l
total 0 
[taoge@localhost learn_c]$ mkdir test
[taoge@localhost learn_c]$ ls -l
total 4
drwxrwxr-x 2 taoge taoge 4096 May  6 04:07 test
[taoge@localhost learn_c]$ touch ./test/a.txt
[taoge@localhost learn_c]$ ls ./test/
a.txt
[taoge@localhost learn_c]$ chmod u-r test/
[taoge@localhost learn_c]$ ls ./test/
ls: cannot open directory ./test/: Permission denied
[taoge@localhost learn_c]$ 
 

 

      2. w权限:可写权限(可以往目录中写东东, 比如文件), 验证如下:

 

 [taoge@localhost learn_c]$ ls -l
total 0
[taoge@localhost learn_c]$ mkdir test
[taoge@localhost learn_c]$ ls -l
total 4
drwxrwxr-x 2 taoge taoge 4096 May  6 04:13 test
[taoge@localhost learn_c]$ touch ./test/a.txt
[taoge@localhost learn_c]$ chmod u-w test
[taoge@localhost learn_c]$ touch ./test/b.txt
touch: cannot touch `./test/b.txt': Permission denied
[taoge@localhost learn_c]$ 
 

 

     3. x权限: 可执行权限(可以cd进去), 验证如下:

 

 

 [taoge@localhost learn_c]$ ls -l
total 0
[taoge@localhost learn_c]$ mkdir test
[taoge@localhost learn_c]$ ls -l
total 4
drwxrwxr-x 2 taoge taoge 4096 May  6 04:17 test
[taoge@localhost learn_c]$ cd test/
[taoge@localhost test]$ cd -
/home/taoge/Desktop/learn_c
[taoge@localhost learn_c]$ chmod u-x test/
[taoge@localhost learn_c]$ cd test/
bash: cd: test/: Permission denied
[taoge@localhost learn_c]$ 
 
      

       好,最后我们再来看一个问题:在某目录test中创建一个文件或者删除一个文件, 需要test目录具备什么权限呢? 答曰:需要目录test具备wx权限, 验证如下:

 

 

 [taoge@localhost learn_c]$ ls -l
total 0
[taoge@localhost learn_c]$ mkdir test
[taoge@localhost learn_c]$ touch ./test/a.txt ./test/b.txt ./test/c.txt ./test/d.txt
[taoge@localhost learn_c]$ ls -l
total 4
drwxrwxr-x 2 taoge taoge 4096 May  6 04:33 test
[taoge@localhost learn_c]$ chmod u-r test/
[taoge@localhost learn_c]$ touch ./test/e.txt
[taoge@localhost learn_c]$ chmod u-w test/
[taoge@localhost learn_c]$ touch ./test/f.txt
touch: cannot touch `./test/f.txt': Permission denied
[taoge@localhost learn_c]$ rm ./test/a.txt
rm: cannot remove `./test/a.txt': Permission denied
[taoge@localhost learn_c]$ chmod u+w test/
[taoge@localhost learn_c]$ chmod u-x test/
[taoge@localhost learn_c]$ touch ./test/f.txt
touch: cannot touch `./test/f.txt': Permission denied
[taoge@localhost learn_c]$ rm ./test/a.txt
rm: cannot remove `./test/a.txt': Permission denied
[taoge@localhost learn_c]$ chmod u+x test/
[taoge@localhost learn_c]$ 

 

 

       因此, 如果某一目录test删除不掉, 很可能是因为其中有不可删除的文件, 从本质上来讲, 就是test自己没有wx权限了。

 

 

       好, 本文先闲谈到这里。

 

 

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值