Linux directory and file level rights management

Brief description of field meaning

Linux provides directory and file level rights management. Each file and directory has an owner and a group. A file or directory can have different permissions for the owner, users in the group, and all other users.

  • For a file, "r" stands for read permission, and "w" stands for write permission.
  • For a directory, "r" represents the right to view the contents of the directory, "w" represents the right to create or delete new files or directories in the directory, "x" represents the right to access the subdirectories of the directory.

When you execute ll, you will usually see something similar to the following figure:

Permissions are related to the three fields "drwxr XR x-x", "root" and "root". Among them:

  • The first field, "drwxrxrxrx-x," contains the following information:
    • The first character shows whether the path at the end of the line is a file or directory:
      • If the first character is "d", the path corresponds to a directory;
      • If the first character is "-", the path corresponds to a file.
    • The last nine characters can be divided into three groups of three characters:
      • Three characters in the first group represent the permission of the owner of the path;
      • The three characters of the second group represent the permissions of the group to which the path belongs;
      • The three characters in the third group represent the permissions that all other users have on the path.
    • The first of three characters in each group corresponds to "r", the second corresponds to "w", and the third corresponds to "x". If the corresponding position is displayed as a word
      Parent, it means that the corresponding user has the permission represented by the letter. If it is "-", it means that there is no permission.
  • The "rwx" permission of the file system under the operating system can be represented by numbers -- for example, "rwxrwxrwx" = 777, "rwxrxrxrx-x" = 755, and so on. rwx RWX rwx indicates that the binary is: 111 111 111 = 7 7 7 7, indicating that the owner's permission is rwx, the group's permission is rwx, and other users' permission is rwx.
  • The second field "root" corresponds to the owner of the path at the end of the line.
  • The third four segment "root" pairs the group to which the path at the end of the line belongs.

Rights management command

Pre preparation

Create directory: mkdir Demo

Create the file hello.sh in the directory Demo. The contents of the file are as follows:

echo "hello world!"

[failed to save the image in the external link. The source station may have anti-theft chain mechanism. It is recommended to save the image and upload it directly (img-0qwmz6bu-158150119069) (C: \ users \ administrator \ appdata \ roaming \ typora \ typora user images \ 1581495929760. PNG))

Operation command

chown modify the owner or group of a file or directory
  • To modify the owner of a file or directory:

    • Change the owner of Demo to "rancher":

      chown rancher Demo/

    [root@weltest ~]# chown rancher Demo/
    [root@weltest ~]# ll
    //Total consumption 91424
    -rw-------. 1 root    root     1261 2 Month 617:05 anaconda-ks.cfg
    drwxr-xr-x  2 root    root      145 2 Month 809:11 cad
    drwxr-xr-x  2 rancher root       22 2 Month 1111:26 Demo
    -rw-r--r--. 1 root    root      655 2 Month 610:28 modules.sh
    -rw-------  1 root    root 93609984 2 Month 1016:37 tiller2.16.1.tar
    
    
    • Change the owner of Demo to root and the group to docker:

      chown root:docker Demo/

      [root@weltest ~]# chown root:docker  Demo/
      [root@weltest ~]# ll
      //Total consumption 91424
      -rw-------. 1 root root       1261 2 Month 617:05 anaconda-ks.cfg
      drwxr-xr-x  2 root root        145 2 Month 809:11 cad
      drwxr-xr-x  2 root docker       22 2 Month 1111:26 Demo
      -rw-r--r--. 1 root root        655 2 Month 610:28 modules.sh
      -rw-------  1 root root   93609984 2 Month 1016:37 tiller2.16.1.tar
      [root@weltest ~]# 
      
      
    • Change the owner of all files in the Demo directory and its subdirectories to the rancher

      chown -hR rancher Demo/

    [root@weltest ~]# ll  | grep Demo
    drwxr-xr-x  2 root docker       22 2 Month 1111:26 Demo
    [root@weltest ~]# ll Demo/
    //Total dosage 4
    -rw-r--r-- 1 root root 21 2 Month 1111:26 hello.sh
    [root@weltest ~]# chown -hR rancher Demo/
    [root@weltest ~]# ll Demo/
    //Total dosage 4
    -rw-r--r-- 1 rancher root 21 2 Month 1111:26 hello.sh
    [root@weltest ~]# 
    
    
    • Change the owner of all the files in the Demo directory and its subdirectories to lancher, and the group to root:

      chown -hR rancher:root Demo/

      [root@weltest ~]# ll  | grep Demo
      drwxr-xr-x  2 root docker       22 2 Month 1111:26 Demo
      [root@weltest ~]# ll Demo/
      //Total dosage 4
      -rw-r--r-- 1 root root 21 2 Month 1111:26 hello.sh
      [root@weltest ~]# chown -hR rancher:root Demo/
      [root@weltest ~]# ll  | grep Demo
      drwxr-xr-x  2 rancher root       22 2 Month 1111:26 Demo
      [root@weltest ~]# ll Demo/
      //Total dosage 4
      -rw-r--r-- 1 rancher root 21 2 Month 1111:26 hello.sh
      [root@weltest ~]# 
      
      
chgrp modify the directory or user's group
  • group example of modifying a file or directory:

    • Modify the group of the directory Demo to docker

      chgrp docker Demo/

      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher root       22 2 Month 1111:26 Demo
      [root@weltest ~]# chgrp docker Demo/
      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher docker       22 2 Month 1111:26 Demo
      [root@weltest ~]# ll Demo/
      //Total dosage 4
      -rw-r--r-- 1 rancher root 21 2 Month 1111:26 hello.sh
      [root@weltest ~]# 
      
    • Change the grou of the directory Demo and its sub files to lancher

      chgrp -hR rancher Demo/

      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher docker       22 2 Month 1111:26 Demo
      [root@weltest ~]# chgrp -hR rancher  Demo/
      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher rancher       22 2 Month 1111:26 Demo
      [root@weltest ~]# ll Demo/
      //Total dosage 4
      -rw-r--r-- 1 rancher rancher 21 2 Month 1111:26 hello.sh
      [root@weltest ~]# 
      
      
chmod permission to modify a file or directory
  • Modify file or directory permissions. If you need to give permissions to subdirectories or sub files, you need to bring the - R parameter. Here is no specific operation example

    • Modify the directory Demo permission to 777

      chmod 777 Demo/

      [root@weltest ~]# ll | grep Demo
      drwxr-xr-x  2 rancher rancher       22 2 Month 1111:26 Demo
      [root@weltest ~]# chmod 777 Demo/
      [root@weltest ~]# ll | grep Demo
      drwxrwxrwx  2 rancher rancher       22 2 Month 1111:26 Demo
      [root@weltest ~]# 
      
      
    • Subtract r permission from group group of directory

      chmod g-r Demo or chmod 737 Demo

      The command added is: chmod g+r Demo or chmod 777 Demo

      [root@weltest ~]# ll | grep Demo
      drwxrwxrwx  2 rancher rancher       22 2 Month 1111:26 Demo
      [root@weltest ~]# chmod g-r Demo
      [root@weltest ~]# ll | grep Demo
      drwx-wxrwx  2 rancher rancher       22 2 Month 1111:26 Demo
      [root@weltest ~]# 
      
      
    • Subtract the r permission from the owner of the directory

      chmod u-r Demo or chmod 333 Demo

      Add r permission as: chmod u+r Demo or chmod 733 Demo

      [root@weltest ~]# ll
      //Total consumption 91424
      -rw-------. 1 root    root        1261 2 Month 617:05 anaconda-ks.cfg
      drwxr-xr-x  2 root    root         145 2 Month 809:11 cad
      drwx-wx-wx  2 rancher rancher       22 2 Month 1111:26 Demo
      -rw-r--r--. 1 root    root         655 2 Month 610:28 modules.sh
      -rw-------  1 root    root    93609984 2 Month 1016:37 tiller2.16.1.tar
      [root@weltest ~]# chmod u-r Demo
      [root@weltest ~]# ll
      //Total consumption 91424
      -rw-------. 1 root    root        1261 2 Month 617:05 anaconda-ks.cfg
      drwxr-xr-x  2 root    root         145 2 Month 809:11 cad
      d-wx-wx-wx  2 rancher rancher       22 2 Month 1111:26 Demo
      -rw-r--r--. 1 root    root         655 2 Month 610:28 modules.sh
      -rw-------  1 root    root    93609984 2 Month 1016:37 tiller2.16.1.tar
      [root@weltest ~]# 
      
    • Subtract r permission from other users of the directory

      chmod o-w Demo or chmod 331 Demo

      Add the r permission to chmod o+w Demo or chmod 333 Demo

      [root@weltest ~]# ll | grep Demo
      d-wx-wx-wx  2 rancher rancher       22 2 Month 1111:26 Demo
      [root@weltest ~]# chmod o-w Demo/
      [root@weltest ~]# ll | grep Demo
      d-wx-wx--x  2 rancher rancher       22 2 Month 1111:26 Demo
      [root@weltest ~]# chmod 333 Demo/
      [root@weltest ~]# ll | grep Demo
      d-wx-wx-wx  2 rancher rancher       22 2 Month 1111:26 Demo
      [root@weltest ~]# 
      
      
    • Add r permission to the owner, grou and other users of the directory

      chmod a+r Demo or chmod 777 Demo

      Add r permission: chmod a-x Demo or chmod 666Demoa

      [root@weltest ~]# chmod a+r Demo/
      [root@weltest ~]# ll
      //Total consumption 91424
      -rw-------. 1 root    root        1261 2 Month 617:05 anaconda-ks.cfg
      drwxr-xr-x  2 root    root         145 2 Month 809:11 cad
      drwxrwxrwx  2 rancher rancher       22 2 Month 1111:26 Demo
      -rw-r--r--. 1 root    root         655 2 Month 610:28 modules.sh
      -rw-------  1 root    root    93609984 2 Month 1016: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 Month 1111:26 Demo
      [root@weltest ~]# 
      

‚Äč

592 original articles published, 219 praised, 1.3 million visitors+
His message board follow

Tags: Docker Anaconda Linux

Posted on Wed, 12 Feb 2020 05:49:25 -0500 by mad4dweb