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

2022-09-02 16:38:38 浏览数 (2)

大家好,又见面了,我是你们的朋友全栈君。

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(“goodn”); 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权限了。

好, 本文先闲谈到这里。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/139708.html原文链接:https://javaforall.cn

0 人点赞