The article directories

  • 1. Basic introduction to permissions
  • 2. RWX permission details
    • 2.1 RWX applies to files
    • 2.2 RWX Applies to directories
  • 3. Actual cases of file and directory permissions
    • 3.1 The following information is displayed in LS -L:
  • 4. Change the permission by chmod
    • 4.1 Basic Description:
    • 4.2 Method 1: +, -, = Change permission
    • 4.3 Second Method: Change permission digitally
  • 5. Change the file owner to chown
    • 5.1 Basic Introduction
  • 6. Change the file/directory group -chgrp
    • 6.1 Basic Introduction
    • 6.2 Case Demonstration
    • 6.3 Best Practices – Police and Bandit games
    • 6.4 Exercise 1 after class
    • 6.5 Exercise 2 after Class

1. Basic introduction to permissions

  • ls -lIs displayed as follows:
-rwxrw-r-- 1 root root 1213 Feb 2 09:39 abc
Copy the code
  • 0-9A description
  1. The first0Bits determine the file type (d, - , l , c , b)

    lIt’s a link, the equivalent of a Shortcut on Windows

    dIs a directory, the equivalent of a Windows folder

    cIt’s character device file, mouse, keyboard

    bIt’s a device, like a hard disk
  2. The first1-3Bits determine that the owner (the owner of the file) has permissions on the file. –User
  3. The first4 to 6Bit to determine the permission of the owning group (the same user group) to the file, –Group
  4. The first7-9Bits to determine whether other users have permissions on the file —Other

2. RWX permission details

2.1 RWX applies to files

  1. [ r ]Stands for read: can be read, viewed
  2. [ w ]Indicates Write: a file can be modified but cannot be deleted. To delete a file, you must have the write permission for the directory in which the file resides.
  3. [ x ]Execute: indicates that the command can be executed

2.2 RWX Applies to directories

  1. [ r ]Stands for read: the directory can be read, ls to view the contents
  2. [ w ]Indicates Write: you can create, delete, and rename directories in a directory
  3. [ x ]Indicates Execute: The directory can be accessed

3. Actual cases of file and directory permissions

3.1 The following information is displayed in LS -L:

-rwxrw-r-- 1 root root 1213 Feb 2 09:39 ab
Copy the code
  • 10 characters to determine what different users can do with a file
  1. The first character represents the file type:- l d c b

    Other characters in groups of 3 (RWX) Read ® Write (W) Execute (x)
  2. The first grouprwx: The file owner has read, write, and execute permissions
  3. The second grouprw-: Users in the same group as the file owner have read and write permissions but cannot execute the file
  4. The third groupr--: The permission of other users who are not in the same group as the file owner is read and cannot be written or executed
  • The available figures are:r=4,w=2,x=1sorwx=4+2+1=7Numbers can be combined
  • Other instructions
1Files: number of hard connections or directories: number of subdirectories root user Root group1213File size (bytes), if folder, displayed4096Bytes in Feb2 09:39Last modified date ABC file nameCopy the code

4. Change the permission by chmod

4.1 Basic Description:

  • You can modify the permission of a file or directory by using the chmod command.

4.2 Method 1: +, -, = Change permission

  • u: Owner,g: All groups,o: Others,a: All (U, G, OThe sum total of)
  1. Chmod u= RWX,g=rx,o=x Name of the file or directory
  2. Chmod o+w Specifies the name of the file or directory
  3. Chmod a-x Specifies the name of the file or directory
  • Case presentation
  1. toabcThe owner of a file has read and write permissions for the file group and other groups.
chmod u=rwx,g=rx,o=rx abc
Copy the code
  1. toabcThe owner of a file can add the group write permission to the file
chmod u-x,g+w abc
Copy the code
  1. toabcAdd the read permission to all users of the file
chmod a+r abc
Copy the code

4.3 Second Method: Change permission digitally

r=4 w=2 x=1 rwx=4+2+1=7
Copy the code

Chmod u= RWX,g=rx,o=x File directory name is the same as the file or directory name of chmod 751

  • Case presentation

    Requirements:/home/abc.txtChange the file permission torwxr-xr-x, implemented using numbers:
chmod 755 /home/abc.t
Copy the code

5. Change the file owner to chown

5.1 Basic Introduction

Chown newOwner Changes the owner of a file/directoryCopy the code
Chown newowner:newgroup Changes the owner and group of a file or directoryCopy the code

-r: if the directory is a directory, the recursion of all its subfiles or directories takes effect

  • Case presentation
  1. Please send/home/abc.txtThe file owner is changed totom
chown tom /home/abc.txt
Copy the code
  1. Please send/home/testThe owner of all files and directories in the directory is changed totom
chown -R tom /home/test
Copy the code

6. Change the file/directory group -chgrp

6.1 Basic Introduction

CHGRP newgroup file/directoryCopy the code

6.2 Case Demonstration

  1. Please send/home/abc.txtThe file group is changed toshaolin(shaolin)
groupadd shaolin
chgrp shaolin /home/abc.txt
Copy the code
  1. Please send/home/testAll files and directories in the directory are changed toshaolin(shaolin)
chgrp -R shaolin /home/test
Copy the code

6.3 Best Practices – Police and Bandit games

Police, bandit Jack, Jerry, xh, XQCopy the code
  1. Create a groupgroupadd police ; groupadd bandit
  2. Create a user
useradd -g police jack ; useradd -g police jerry
Copy the code
useradd -g bandit xh; useradd -g bandit xq
Copy the code
  1. jackCreate a file that you can read yourselfrw, this group can read, other groups no one has any permission

    First of all,jackLogin;vim jack.txt ; chmod 640 jack.txt
  2. jackModify the file so that other groups can read it and this group can read and write it
chmod o=r,g=r jack.txt
Copy the code
  1. xhGo to the police, see if you can read and write.usermod -g police xh
  2. Test, let’s seexhCan read and write,xqYes or no, small conclusion, is that if you want to operate on files in a directory, you need to have access to that directory

    Appropriate permissions
  3. Schematic diagram

6.4 Exercise 1 after class

  • Practice file permission management. Complete the following steps.
  1. Create two groups (SX, YG)
  2. Create four users (Tang Seng, Wukong, Bajie, Sha Seng)
  3. Set the password
  4. Put Wukong, Eight quit into the monster Tang Seng sand Seng in the immortal
  5. Create a file with monkey. Java to output I am monkey.
  6. Give Bajie a permission to r w
  7. Monkey. Java (I am pig)
  8. Tang Seng Sha Seng has no authority on this file
  9. Put Sand monk in the monster group
  10. Ask the sand monk to modify the file monkey and add a sentence (” I am sand monk, I am monster!” );
  11. Discuss and test the details of folder RWX!! X: indicates that you can access the directory, for example, CD. R: indicates that you can ls and display the contents of the directory. W: indicates that you can delete or create files in the directory
  12. Schematic diagram

6.5 Exercise 2 after Class

  1. Log in as root, create user mycentos, and set your own password
  2. Log in with mycentos and create a directory test/t11/t1 under the home directory
  3. Create a text file aa in T1 and edit its contents to ls — al with vi
  4. Change the permission of AA to an executable file [can append the current date to a file], run the file./aa
  5. Delete the newly created directory test/t11/ T1
  6. Delete the contents of user mycentos and its home directory
  7. Set Up Linux to access the graphical interface
  8. Restart Linux or shut down