Linux-CentOS7 文件夹和文件、父目录和子文件、父目录和子目录的权限详解

权限对于文件和目录的意义
拥有目录的x权限表示用户可以进入该目录成为工作目录,能不能进入一个目录,只与该目录的x权限有关,如果用户对于某个目录不具有x权限,则无法切换到该目录下,也就无法执行该目录下的任何命令,即使具有该目录的r权限。且如果用户对于某目录不具有x权限,则该用户不能查询该目录下的文件的内容,注意:指的是内容,如果有r 权限是可以查看该目录下的文件名列表或子目录列表的。所以要开放目录给任何人浏览时,应该至少要给与r及x权限。

一、权限对于目录的意义
1、首先要明白的是目录主要的内容是记录文件名列表和子目录列表,而不是实际存放数据的地方。
2、r权限:拥有此权限表示可以读取目录结构列表,也就是说可以查看目录下的文件名和子目录名,注意:仅仅指的是名字。
3、w权限:拥有此权限表示具有更改该目录结构列表的权限,总之,目录的w权限与该目录下的文件名或子目录名的变动有关,注意:指的是名字。具体如下:
1)在该目录下新建新的文件或子目录。
2)删除该目录下已经存在的文件或子目录(不论该文件或子目录的权限如何),注意:这点很重要,用户能否删除一个文件或目录,看的是该用户是否具有该文件或目录所在的目录的w权限(配合x权限,因为要进入目录)。
3)将该目录下已经存在的文件或子目录进行重命名。
4)转移该目录内的文件或子目录的位置。

4、x权限:拥有目录的x权限表示用户可以进入该目录成为工作目录,能不能进入一个目录,只与该目录的x权限有关,如果用户对于某个目录不具有x权限,则无法切换到该目录下,也就无法执行该目录下的任何命令,即使具有该目录的r权限。且如果用户对于某目录不具有x权限,则该用户不能查询该目录下的文件的内容,注意:指的是内容,如果有r 权限是可以查看该目录下的文件名列表或子目录列表的。所以要开放目录给任何人浏览时,应该至少要给与r及x权限。

二、权限对于文件的意义
1、也应该明白的是文件是实际含有数据的地方,所以r、w、x权限对文件来说是与其内容有关的。
2、r权限:用于此权限表示可以读取此文件的实际内容。

3、w权限:拥有此权限表示可以编辑、添加或者是修改该文件的内容。但是不包含删除该文件,因为由上面权限对于目录的意义得知删除文件或目录的条件是什么。
4、x权限:表示该文件具有可以被系统执行的权限。文件是否能被执行就是由该权限来决定的,跟文件名没有绝对的关系。

要开放目录给任何人浏览时,应该至少也要给予r及x的权限,但w权限不可随便给!

因为:如果A用户对某文件没有任何权限,但是对该文件所在目录拥有w权限的话,那么A用户依然可以删除该文件。

实验环境搭建:
创建两个用户zhangsan、lisi,并设定密码
在/目录下创建一个test目录,目录下创建5个文本文件

[root@xuegod110 /]# ls
bin dev home lib64 mnt proc run srv tmp var
boot etc lib media opt root sbin sys usr
[root@xuegod110 /]# mkdir test
[root@xuegod110 /]# touch test/{1…5}.txt
[root@xuegod110 /]# useradd zhangsan
[root@xuegod110 /]# useradd lisi
[root@xuegod110 /]# passwd zhangsan
更改用户 zhangsan 的密码 。
新的 密码:
无效的密码: 密码少于 8 个字符
重新输入新的 密码:
passwd:所有的身份验证令牌已经成功更新。
[root@xuegod110 /]# echo 123456 | passwd --stdin lisi
更改用户 lisi 的密码 。
passwd:所有的身份验证令牌已经成功更新。

针对other用户的权限测试:

1、修改子文件1.txt的other权限为0,测试other用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwxr-xr-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# ll 1.txt
-rw-r–r-- 1 root root 0 3月 27 11:41 1.txt
[root@xuegod110 test]# chmod 640 1.txt
[root@xuegod110 test]# ll 1.txt
-rw-r----- 1 root root 0 3月 27 11:41 1.txt
[root@xuegod110 test]# su - zhangsan
[zhangsan@xuegod110 ~]$ cat /test/1.txt
cat: /test/1.txt: 权限不够
[zhangsan@xuegod110 ~]$ echo aaa >> /test/1.txt
-bash: /test/1.txt: 权限不够

总结:当父目录other拥有读取和执行权限,子文件other没有权限时,other用户无法读取和修改文件。

2、修改子文件1.txt的other权限为4,测试other用户是否可以读取文件

[root@xuegod110 test]# ll 1.txt
-rw-r----- 1 root root 0 3月 27 11:41 1.txt
[root@xuegod110 test]# chmod 644 1.txt
[root@xuegod110 test]# ll 1.txt
-rw-r–r-- 1 root root 0 3月 27 11:41 1.txt
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 11:46:14 CST 2019pts/1 上
[zhangsan@xuegod110 ~]$ cat /test/1.txt
[zhangsan@xuegod110 ~]$ echo aaa >> /test/1.txt
-bash: /test/1.txt: 权限不够

总结:当父目录other拥有读取和执行权限,子文件other拥有读取权限,other用户只能读取文件内容,不能修改。

3、修改子文件1.txt的other权限为6,测试other用户是否可以修改文件

[root@xuegod110 test]# ll 1.txt
-rw-r–r-- 1 root root 0 3月 27 11:41 1.txt
[root@xuegod110 test]# chmod 646 1.txt
[root@xuegod110 test]# ll 1.txt
-rw-r–rw- 1 root root 0 3月 27 11:41 1.txt
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 11:50:29 CST 2019pts/1 上
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
[zhangsan@xuegod110 ~]$ cat /test/1.txt
aaa

总结:当父目录other拥有读取和执行权限,子文件other拥有读取和修改权限,other用户可以读取和修改文件。

4、修改父目录other的权限为4,测试other用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwxr-xr-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# ll 1.txt
-rw-r–rw- 1 root root 4 3月 27 11:53 1.txt
[root@xuegod110 test]# chmod 754 /test/
[root@xuegod110 test]# ll -d /test/
drwxr-xr-- 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 11:53:17 CST 2019pts/1 上
[zhangsan@xuegod110 ~]$ cd /test/
-bash: cd: /test/: 权限不够
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
-bash: /test/1.txt: 权限不够
[zhangsan@xuegod110 ~]$ cat /test/1.txt
cat: /test/1.txt: 权限不够

总结:因为父目录没有执行权限,X对于目录来说相当于进入cd,无法进入目录,所以无法修改用户。
父目录other用户有读取权限,子文件other用户有读取和修改权限,other用户无法读取和修改文件

5、修改父目录other权限为2,测试other用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwxr-xr-- 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# chmod 752 /test/
[root@xuegod110 test]# ll -d /test/
drwxr-x-w- 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 11:59:01 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
-bash: cd: /test/: 权限不够

总结:因为父目录没有X权限,无法进入目录,所以无法修改文件

6、修改父目录other权限为1,测试other用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwxr-x-w- 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# chmod 751 /test/
[root@xuegod110 test]# ll -d /test/
drwxr-x–x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:12:42 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
[zhangsan@xuegod110 test]$ ll 1.
ls: 无法访问1.: 没有那个文件或目录
[zhangsan@xuegod110 test]$ ll
ls: 无法打开目录.: 权限不够

总结:父目录没有读取权限,只有执行权限。other用户可以进度目录, 无法列出内容,更加无法修改文件

针对说所属组用户的权限测试

将zhangsan用户加入到root组里面

[root@xuegod110 test]# usermod -G root zhangsan
[root@xuegod110 test]# id zhangsan
uid=1002(zhangsan) gid=1002(zhangsan) 组=1002(zhangsan),0(root)

1、修改子文件所属组的权限为4,测试所属组用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwxr-xr-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# ll 1.txt
-rw-r–r-- 1 root root 4 3月 27 11:53 1.txt
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:17:44 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
[zhangsan@xuegod110 test]$ ll
-rw-r–r-- 1 root root 4 3月 27 11:53 1.txt
[zhangsan@xuegod110 test]$ echo aaa > 1.txt
-bash: 1.txt: 权限不够
[zhangsan@xuegod110 test]$ cat 1.txt
aaa

总结:父目录所属组拥有读取和执行权限,子文件的所属组拥有读取权限,所属组用户可以读取文件内容,无法修改文件

2、修改子文件所属组的权限为2,测试所属组用户是否可以修改文件

[root@xuegod110 test]# ll 1.txt
-rw-r–rw- 1 root root 4 3月 27 11:53 1.txt
[root@xuegod110 test]# chmod 624 1.txt
[root@xuegod110 test]# ll 1.txt
-rw–w-r-- 1 root root 4 3月 27 11:53 1.txt
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:28:12 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
[zhangsan@xuegod110 ~]$ cat /test/1.txt
cat: /test/1.txt: 权限不够

总结:父目录所属组拥有读取和执行权限,子文件的所属组拥有修改权限,所属组用户可以修改文件内容,无法读取文件。

3、修改子文件所属组的权限为1,测试所属组用户是否可以修改文件

[root@xuegod110 test]# ll 1.txt
-rw–w-r-- 1 root root 4 3月 27 12:29 1.txt
[root@xuegod110 test]# chmod 614 1.txt
[root@xuegod110 test]# ll 1.txt
-rw—xr-- 1 root root 4 3月 27 12:29 1.txt
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:29:43 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
-bash: /test/1.txt: 权限不够
[zhangsan@xuegod110 ~]$ cat /test/1.txt
cat: /test/1.txt: 权限不够

总结:父目录所属组拥有读取和执行权限,子文件的所属组拥有执行权限,所属组用户无法修改和读取文件内容。

4、修改父目录的所属组的权限为4,测试所属组用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwxr-xr-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# chmod 745 /test/
[root@xuegod110 test]# ll -d /test/
drwxr–r-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:31:59 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
-bash: cd: /test/: 权限不够
[zhangsan@xuegod110 ~]$ cat /test/1.txt
cat: /test/1.txt: 权限不够
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
-bash: /test/1.txt: 权限不够

总结:父目录所属组拥有读取权限,子文件所属组有读取和修改权限。所属组用户没有执行权限,无法进入目录,所以无法修改和读取文件内容

5、修改父目录的所属组的权限为2,测试所属组用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwxr–r-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# chmod 725 /test/
[root@xuegod110 test]# ll -d /test/
drwx-w-r-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:37:53 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
-bash: cd: /test/: 权限不够
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt #无法tab补全,因为没有r权限
-bash: /test/1.txt: 权限不够
[zhangsan@xuegod110 ~]$ cat /test/1.txt #无法tab补全,因为没有r权限,无法列出
cat: /test/1.txt: 权限不够

总结:父目录所属组拥有修改权限,子文件所属组有读取和修改权限。所属组用户没有执行权限,无法进入目录,所以无法读取和修改文件内容

6、修改父目录的所属组的权限为1,测试所属组用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwx-w-r-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# chmod 715 /test/
[root@xuegod110 test]# ll -d /test/
drwx–xr-x 2 root root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:40:42 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
[zhangsan@xuegod110 test]$ ll
ls: 无法打开目录.: 权限不够

总结:父目录所属组拥有执行权限,子文件所属组有读取和修改权限。所属组用户可以进入目录,因为没有读取权限,所以无法列出目录的结构,导致无法读取和修改文件内容

针对主组用户修改权限测试
将zhangsan从root组内踢出

[root@xuegod110 test]# id zhangsan
uid=1002(zhangsan) gid=1002(zhangsan) 组=1002(zhangsan),0(root)
[root@xuegod110 test]# usermod -G “” zhangsan
[root@xuegod110 test]# id zhangsan
uid=1002(zhangsan) gid=1002(zhangsan) 组=1002(zhangsan)

将父目录和子文件的主组修改为zhangsan用户

[root@xuegod110 test]# chown zhangsan /test/
[root@xuegod110 test]# ll -d /test/
drwx–xr-x 2 zhangsan root 71 3月 27 11:41 /test/
[root@xuegod110 test]# ll 1.txt
-rw-r–r-- 1 root root 4 3月 27 12:29 1.txt
[root@xuegod110 test]# chown zhangsan 1.txt
[root@xuegod110 test]# ll 1.txt
-rw-r–r-- 1 zhangsan root 4 3月 27 12:29 1.txt

1、修改子文件的主组权限为r,测试主组用户是否可以修改文件

[root@xuegod110 test]# ll 1.txt
-rw-r–r-- 1 zhangsan root 4 3月 27 12:29 1.txt
[zhangsan@xuegod110 ~]$ chmod 440 /test/1.txt
[root@xuegod110 test]# ll 1.txt
-r–r----- 1 zhangsan root 4 3月 27 12:29 1.txt
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:43:49 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
-bash: /test/1.txt: 权限不够
[zhangsan@xuegod110 ~]$ cat /test/1.txt
aaa

总结:父目录的主组用户拥有读取和执行权限,子文件的主组用户拥有读取权限。所以主组用户可以读取文件内容,无法修改文件内容

2、修改子文件的主组权限为w,测试主组用户是否可以修改文件

[root@xuegod110 test]# ll 1.txt
-r–r----- 1 zhangsan root 4 3月 27 12:29 1.txt
[root@xuegod110 test]# chmod 240 1.txt
[root@xuegod110 test]# ll 1.txt
–w-r----- 1 zhangsan root 4 3月 27 12:29 1.txt
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 12:51:51 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
[zhangsan@xuegod110 ~]$ cat /test/1.txt
cat: /test/1.txt: 权限不够

总结:父目录的主组用户拥有读取和执行权限,子文件的主组用户拥有修改权限。所以子文件的主组用户可以修改文件,无法查看文件内容。

3、修改子文件的主组权限为x,测试主组用户是否可以修改文件

[root@xuegod110 test]# ll 1.txt
–w-r----- 1 zhangsan root 4 3月 27 13:26 1.txt
[root@xuegod110 test]# chmod 140 1.txt
[root@xuegod110 test]# ll 1.txt
—xr----- 1 zhangsan root 4 3月 27 13:26 1.txt
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 13:26:40 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
-bash: /test/1.txt: 权限不够
[zhangsan@xuegod110 ~]$ cat /test/1.txt
cat: /test/1.txt: 权限不够

总结:父目录的主组用户拥有读取和执行权限,子文件的主组用户拥有执行权限。所以子文件的主组用户无法修改和读取文件的内容。

4、修改父目录的主组权限为r,测试主组用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
drwx–xr-x 2 zhangsan root 71 3月 27 11:41 /test/
[root@xuegod110 test]# chmod 455 /test/
[root@xuegod110 test]# ll -d /test/
dr–r-xr-x 2 zhangsan root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 13:29:47 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
-bash: cd: /test/: 权限不够
[zhangsan@xuegod110 ~]$ cat /test/1.txt
cat: /test/1.txt: 权限不够
[zhangsan@xuegod110 ~]$ echo aaa > /test/1.txt
-bash: /test/1.txt: 权限不够

总结:父目录的主组用户拥有读取权限,子文件的主组用户拥有读取和修改权限。因为父目录主组没有执行权限,无法进入目录内,所以无法修改和读取文件内容

5、修改父目录的主组权限为w,测试主组用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
dr–r-xr-x 2 zhangsan root 71 3月 27 11:41 /test/
[root@xuegod110 test]# chmod 255 /test/
[root@xuegod110 test]# ll -d /test/
d-w-r-xr-x 2 zhangsan root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 13:32:25 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
-bash: cd: /test/: 权限不够

总结:父目录的主组用户拥有修改权限,子文件的主组用户拥有读取和修改权限。因为父目录主组没有执行权限,无法进入目录内,所以无法修改和读取文件内容

6、修改父目录的主组权限为x,测试主组用户是否可以修改文件

[root@xuegod110 test]# ll -d /test/
d-w-r-xr-x 2 zhangsan root 71 3月 27 11:41 /test/
[root@xuegod110 test]# chmod 155 /test/
[root@xuegod110 test]# ll -d /test/
d–xr-xr-x 2 zhangsan root 71 3月 27 11:41 /test/
[root@xuegod110 test]# su - zhangsan
上一次登录:三 3月 27 13:34:50 CST 2019pts/0 上
[zhangsan@xuegod110 ~]$ cd /test/
[zhangsan@xuegod110 test]$ ll
ls: 无法打开目录.: 权限不够

总结:父目录的主组用户拥有执行权限,子文件的主组用户拥有读取和修改权限。因为父目录主组没有读取权限,无法列出目录内的内容,所以无法修改文件内容

通过上述实验得出结论:
1、对于目录来说
r:代表可以列出目录下的文件结构和详细信息
w:代表可以修改目录下的文件内容和详细信息
x:代表可以进入目录下,针对文件来进行操作
总结:如果要对一个目录下的文件进行操作,最少需要开通rx权限,让用户可以进入目录,列出目录的内容。w权限一般不给目录,因为如果目录给了用户w权限,用户针对子文件没有权限,可以进行修改和删除等操作。

2、对于文件来说
r:代表可以读取文件的内容
w:代表可以修改文件的内容和信息
x:代表可以对这个文件有执行权限,一般用于脚本文件赋权限。
总结:如果需要对一个文件进行操作,可以根据需要开通rwx的权限。

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值