Linux基础知识之文件权限详解

中对于权限的制定虽然没有的那么精细,但是如果你了解并掌握中文件的权限知识,也可以像那样对权限做到精确配置。


中的文件权限是什么?

如何查看中的文件权限

[root@localhost test]# ll -d /test/

drwxr-xr-x. 2 root root 52 8月   7 20:18 /test/

上面的rwxr-xr-x即为文件的权限位共九位。下面分别对其进行介绍。

 

                rwx∣r-x∣r-x

                ↓    ↓   ↓

                属主 属组 其他

前三个为属主位:创建该文件者或被指定的文件所属者

中间三个为属组位:文件的所属组,在该组内的非属主用户对该文件拥有该属组权限。

最后三个Other位:other用户,既不属于属主又不在属组的用户

          r:读权限    w:写权限    x:执行权限

文件中rwx的具体含义:

      r:可以使用类似cat等命令查看文件内容

      w:可以编辑或删除此文件

      x:可以在命令提示符下当做命令提交给内核运行

目录中rwx的具体含义:

      r:可以对此目录执行ls以列出内部的所有文件

      w:可以在此目录创建文件:

      x:可以使用cd切换进此目录,也可以使用ls -l查看内部文件的详细信息

下面请看一个对应关系

          000 —  对应十进制0

          001 –x  对应十进制1

          010 -w-  对应十进制2

          011 -wx  对应十进制3

          100 r–  对应十进制4

          101 r-x  对应十进制5

          110 rw-  对应十进制6

          111 rwx  对应十进制7

上面rwx三位与三位二进制对应,因此权限也可以用数字表达

比如:

  755代表rwxr-xr-x  664代表rw-rw-r–


管理中的文件权限:

chmod chown chgrp umask

chmod  修改文件权限位命令

chmod – change file mode bits

表达格式

       chmod [OPTION]… MODE[,MODE]… FILE…

       chmod [OPTION]… OCTAL-MODE FILE…

       chmod [OPTION]… –reference=RFILE FILE…

常用选项

      -R 递归,将设置的权限应用到下面的所有文件

1、chmod [OPTION]… MODE[,MODE]… FILE…

赋权表示法:u=属主  g=属组  o=其他  a=所有

直接操作一类用户的所有权限位 rwx

    写法:u=rwx

1
2
3
4
5
6
7
8
9
10
11
12
13
[root@localhost test]# ll
总用量 16
-rw-r--r--. 1 root root 43 8月   7 16:46 cat1
-rw-r--r--. 1 root root 19 8月   7 16:46 cat2
-rw-r--r--. 1 root root 57 8月   7 19:34 head
-rw-r--r--. 1 root root 55 8月   7 20:18 siting
[root@localhost test]# chmod u=rwx,g=rwx cat1 
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1
-rw-r--r--. 1 root root 19 8月   7 16:46 cat2
-rw-r--r--. 1 root root 57 8月   7 19:34 head
-rw-r--r--. 1 root root 55 8月   7 20:18 siting

同时更改多个所属对象权限,中间用“,”隔开

授权表示法:直接操作一类用户的一个权限为r,w,x

写法:u+(r|w|x) u-(r|w|x) g+(r|w|x) g-(r|w|x) o+(r|w|x) o-(r|w|x)

a+(r|w|x) a-(r|w|x)

1
2
3
4
5
6
7
[root@localhost test]# chmod u+x,g+w cat2
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1
-rwxrw-r--. 1 root root 19 8月   7 16:46 cat2
-rw-r--r--. 1 root root 57 8月   7 19:34 head
-rw-r--r--. 1 root root 55 8月   7 20:18 siting


2、chmod [OPTION]… OCTAL-MODE FILE…

1
2
3
4
5
6
7
[root@localhost test]# chmod 755 head 
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1
-rwxrw-r--. 1 root root 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root root 57 8月   7 19:34 head
-rw-r--r--. 1 root root 55 8月   7 20:18 siting

3、chmod [OPTION]… –reference=RFILE FILE… 指定目标文件与所指文件的权限一致(不常用)

1
2
3
4
5
6
7
[root@localhost test]# chmod --reference=cat1 siting 
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 root root 43 8月   7 16:46 cat1
-rwxrw-r--. 1 root root 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root root 57 8月   7 19:34 head
-rwxrwxr--. 1 root root 55 8月   7 20:18 siting

siting与cat1文件的权限保持一致

chown 修改属主属组

chown – change file owner and group

表达格式:

       chown [OPTION]… [OWNER][:[GROUP]] FILE…

       chown [OPTION]… –reference=RFILE FILE…

常用选项:

    -R 递归修改该

1、chown [OPTION]… [OWNER][:[GROUP]] FILE…

1
2
3
4
5
6
7
[root@localhost test]# chown gentoo:fedore cat1
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 root   root   19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   root   57 8月   7 19:34 head
-rwxrwxr--. 1 root   root   55 8月   7 20:18 siting

2、chown [OPTION]… –reference=RFILE FILE…

1
2
3
4
5
6
7
[root@localhost test]# chown --reference cat1 cat2
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   root   57 8月   7 19:34 head
-rwxrwxr--. 1 root   root   55 8月   7 20:18 siting

因为chown既可以改属主又可以改属组所以下面这个chgrp命令就被打入冷宫,为了缅怀一下它,这里还是简要介绍下

chgrp – change group ownership 修改属组

表达格式:

       chgrp [OPTION]… GROUP FILE…

       chgrp [OPTION]… –reference=RFILE FILE...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[root@localhost test]# chgrp gentoo head 
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   gentoo 57 8月   7 19:34 head
-rwxrwxr--. 1 root   root   55 8月   7 20:18 siting
[root@localhost test]# chgrp --reference cat1 siting 
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   gentoo 57 8月   7 19:34 head
-rwxrwxr--. 1 root   fedore 55 8月   7 20:18 siting

umask :文件的权限反向掩码,俗称遮罩码

作用它是为了控制默认权限,不要使默认的文件和目录具有全权而设的

    文件:666-umask

    目录:777-umask

:之所以文件用666去减,表示文件默认不能拥有执行权限,如果减得的结果中有执行权限,则需+1

umask:查看当前umask

1
2
[root@localhost test]# umask
0022

umask MASK :设置umask  仅对当前shell进程有效

若要长期修改umask的值,可以把它写进/etc/profile(全局有效)或~/.profile(个人)或~/.bash_profile

1
2
3
[root@localhost test]# umask 0002
[root@localhost test]# umask
0002
1
2
3
4
5
6
7
8
[root@localhost test]# touch umask1
[root@localhost test]# ll
总用量 16
-rwxrwxr--. 1 gentoo fedore 43 8月   7 16:46 cat1
-rwxrw-r--. 1 gentoo fedore 19 8月   7 16:46 cat2
-rwxr-xr-x. 1 root   gentoo 57 8月   7 19:34 head
-rwxrwxr--. 1 root   fedore 55 8月   7 20:18 siting
-rw-rw-r--. 1 root   root    0 8月   8 20:49 umask1

使用root用户创建一个新文件umask1其权限为664,umask为0002,其新建文件的权限符合我们的设定:666-002=664。

更多内容请长按二维码关注(更有不定期发红包活动吆^0^):
致儒先生

转载自:https://www.linuxidc.com/Linux/2016-08/134047.htm

声明: 除非转自他站(如有侵权,请联系处理)外,本文采用 BY-NC-SA 协议进行授权 | 智乐兔
转载请注明:转自《Linux基础知识之文件权限详解
本文地址:https://www.zhiletu.com/archives-8020.html
关注公众号:智乐兔

赞赏

wechat pay微信赞赏alipay pay支付宝赞赏

上一篇
下一篇

相关文章

在线留言

你必须 登录后 才能留言!

在线客服
在线客服 X

售前: 点击这里给我发消息
售后: 点击这里给我发消息

智乐兔官微