Linux目录和文件级别的权限管理

字段含义简述

Linux提供目录和文件级别的权限管理。每个文件和目录都有一个owner(所有者)和一个group(用户组)。一个文件或目录可以对owner、所属group中的用户和其他所有用户开放不同的权限。

  • 对于一个文件,“r”代表读权限,“w”代表写权限。
  • 对于一个目录,“r”代表查看目录下内容的权限,“w”代表在目录中创建或删除新的文件或目录的权限,“x”代表访问该目录的子目录的权限。

当你执行ll ,通常会看到类似于下图的情况:
在这里插入图片描述
和权限有关的是“drwxr-xr-x”、“root”和“root”这三个字段。其中:

  • 第一个字段“drwxr-xr-x”包含了下面信息:
    • 第一个字符显示该行末尾的路径是文件还是目录:
      • 如果第一个字符是“d”代表该路径对应一个目录;
      • 如果第一个字符是“-”则代表该路径对应一个文件。
    • 后九个字符可分为三组三个的字符:
      • 第一组的三个字符代表该路径的owner的权限;
      • 第二组的三个字符代表该路径所属的group的权限;
      • 第三组的三个字符代表所有其他用户对该路径拥有的权限。
    • 每组三个字符中的第一个对应“r”,第二个对应“w”,第三个对应“x”,如果对应的位置显示是字
      母,则代表对应用户有字母所代表的权限,如果是“-”则代表没有权限。
  • 操作系统下文件系统的“rwx”权限可以用数字表示——比如“rwxrwxrwx” =777,“rwxr-xr-x” = 755等等。rwxrwxrwx表示的二进制为:111 111 111=7 7 7,表示owner的权限为rwx,group的权限为rwx,其他用户的权限为rwx。
  • 第二个字段“root”对应着该行末尾路径的owner。
  • 第三个四段“root”对应该行末尾路径所属的group。

权限管理命令

前置准备

创建目录:mkdir Demo

在目录Demo中创建文件hello.sh,文件内容如下:

echo "hello world!"

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0qWmz6BU-1581501190169)(C:\Users\Administrator\AppData\Roaming\Typora\typora-user-images\1581495929760.png)]

操作命令

chown 修改文件或目录的owner或组
  • 修改文件或目录的owner示例:

    • 将 Demo 的属主更改为"rancher":

      chown rancher Demo/

    [root@weltest ~]# chown rancher Demo/
    [root@weltest ~]# ll
    总用量 91424
    -rw-------. 1 root    root     1261 2月   6 17:05 anaconda-ks.cfg
    drwxr-xr-x  2 root    root      145 2月   8 09:11 cad
    drwxr-xr-x  2 rancher root       22 2月  11 11:26 Demo
    -rw-r--r--. 1 root    root      655 2月   6 10:28 modules.sh
    -rw-------  1 root    root 93609984 2月  10 16:37 tiller2.16.1.tar
    
    
    • 将Demo的owner更改为root,group更改为docker:

      chown root:docker Demo/

      [root@weltest ~]# chown root:docker  Demo/
      [root@weltest ~]# ll
      总用量 91424
      -rw-------. 1 root root       1261 2月   6 17:05 anaconda-ks.cfg
      drwxr-xr-x  2 root root        145 2月   8 09:11 cad
      drwxr-xr-x  2 root docker       22 2月  11 11:26 Demo
      -rw-r--r--. 1 root root        655 2月   6 10:28 modules.sh
      -rw-------  1 root root   93609984 2月  10 16:37 tiller2.16.1.tar
      [root@weltest ~]# 
      
      
    • 将Demo目录及其子目录下的所有文件的owner都更改为rancher

      chown -hR rancher Demo/

    [root@weltest ~]# ll  | grep Demo
    drwxr-xr-x  2 root docker       22 2月  11 11:26 Demo
    [root@weltest ~]# ll Demo/
    总用量 4
    -rw-r--r-- 1 root root 21 2月  11 11:26 hello.sh
    [root@weltest ~]# chown -hR rancher Demo/
    [root@weltest ~]# ll Demo/
    总用量 4
    -rw-r--r-- 1 rancher root 21 2月  11 11:26 hello.sh
    [root@weltest ~]# 
    
    
    • 将Demo目录及其子目录下的所有文件的owner都更改为rancher,group改为root:

      chown -hR rancher:root Demo/

      [root@weltest ~]# ll  | grep Demo
      drwxr-xr-x  2 root docker       22 2月  11 11:26 Demo
      [root@weltest ~]# ll Demo/
      总用量 4
      -rw-r--r-- 1 root root 21 2月  11 11:26 hello.sh
      [root@weltest ~]# chown -hR rancher:root Demo/
      [root@weltest ~]# ll  | grep Demo
      drwxr-xr-x  2 rancher root       22 2月  11 11:26 Demo
      [root@weltest ~]# ll Demo/
      总用量 4
      -rw-r--r-- 1 rancher root 21 2月  11 11:26 hello.sh
      [root@weltest ~]# 
      
      
chgrp 修改目录或用户的group
  • 修改文件或目录的group示例:

    • 修改目录Demo的group为docker

      chgrp docker Demo/

      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher root       22 2月  11 11:26 Demo
      [root@weltest ~]# chgrp docker Demo/
      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher docker       22 2月  11 11:26 Demo
      [root@weltest ~]# ll Demo/
      总用量 4
      -rw-r--r-- 1 rancher root 21 2月  11 11:26 hello.sh
      [root@weltest ~]# 
      
    • 修改目录Demo及其子文件的grou为rancher

      chgrp -hR rancher Demo/

      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher docker       22 2月  11 11:26 Demo
      [root@weltest ~]# chgrp -hR rancher  Demo/
      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# ll Demo/
      总用量 4
      -rw-r--r-- 1 rancher rancher 21 2月  11 11:26 hello.sh
      [root@weltest ~]# 
      
      
chmod 修改文件或目录的权限
  • 修改文件或目录权限,如果需要给子目录或子文件赋予权限需要带上-R参数,这里不给出操具体操作示例

    • 修改目录Demo权限为777

      chmod 777 Demo/

      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# chmod 777 Demo/
      [root@weltest ~]# ll | grep Demo
      drwxrwxrwx  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# 
      
      
    • 给目录的group组减去r权限

      chmod g-r Demo 或者 chmod 737 Demo

      加的命令为:chmod g+r Demo 或者chmod 777 Demo

      [root@weltest ~]# ll | grep Demo
      drwxrwxrwx  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# chmod g-r Demo
      [root@weltest ~]# ll | grep Demo
      drwx-wxrwx  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# 
      
      
    • 给目录的owner减去r权限

      chmod u-r Demo 或者 chmod 333 Demo

      增加r权限为:chmod u+r Demo 或者 chmod 733 Demo

      [root@weltest ~]# ll
      总用量 91424
      -rw-------. 1 root    root        1261 2月   6 17:05 anaconda-ks.cfg
      drwxr-xr-x  2 root    root         145 2月   8 09:11 cad
      drwx-wx-wx  2 rancher rancher       22 2月  11 11:26 Demo
      -rw-r--r--. 1 root    root         655 2月   6 10:28 modules.sh
      -rw-------  1 root    root    93609984 2月  10 16:37 tiller2.16.1.tar
      [root@weltest ~]# chmod u-r Demo
      [root@weltest ~]# ll
      总用量 91424
      -rw-------. 1 root    root        1261 2月   6 17:05 anaconda-ks.cfg
      drwxr-xr-x  2 root    root         145 2月   8 09:11 cad
      d-wx-wx-wx  2 rancher rancher       22 2月  11 11:26 Demo
      -rw-r--r--. 1 root    root         655 2月   6 10:28 modules.sh
      -rw-------  1 root    root    93609984 2月  10 16:37 tiller2.16.1.tar
      [root@weltest ~]# 
      
    • 给目录的其他用户减去r权限

      chmod o-w Demo 或者 chmod 331 Demo

      增加r权限为:chmod o+w Demo 或者 chmod 333 Demo

      [root@weltest ~]# ll | grep Demo
      d-wx-wx-wx  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# chmod o-w Demo/
      [root@weltest ~]# ll | grep Demo
      d-wx-wx--x  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# chmod 333 Demo/
      [root@weltest ~]# ll | grep Demo
      d-wx-wx-wx  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# 
      
      
    • 给目录的owner、grou、其他用户增加r权限

      chmod a+r Demo 或者 chmod 777 Demo

      增加r权限为:chmod a-x Demo 或者 chmod 666Demoa

      [root@weltest ~]# chmod a+r Demo/
      [root@weltest ~]# ll
      总用量 91424
      -rw-------. 1 root    root        1261 2月   6 17:05 anaconda-ks.cfg
      drwxr-xr-x  2 root    root         145 2月   8 09:11 cad
      drwxrwxrwx  2 rancher rancher       22 2月  11 11:26 Demo
      -rw-r--r--. 1 root    root         655 2月   6 10:28 modules.sh
      -rw-------  1 root    root    93609984 2月  10 16:37 tiller2.16.1.tar
      [root@weltest ~]# chmod a-w Demo/
      [root@weltest ~]# ll | grep Demo
      dr-xr-xr-x  2 rancher rancher       22 2月  11 11:26 Demo
      [root@weltest ~]# 
      

img

发布了592 篇原创文章 · 获赞 221 · 访问量 130万+

猜你喜欢

转载自blog.csdn.net/henni_719/article/details/104282608