Shell principle and understanding of Linux permissions

Shell commands and operating principles

Linux strictly speaking is an operating system, which we call the "kernel", but our general users cannot directly use the kernel, but through the kernel's "shell" program, which is the so-called shell. kernel communication.
But how to understand this operation? Why can't the kernel be used directly?
From a technical point of view: the simplest definition of shell: command interpreter (command interpreter) mainly includes:

  • Translate user commands to the kernel for processing
  • At the same time, the core processing results are translated to the user.
    Compared with the Windows GUI (graphical interface), we operate Windows instead of directly operating the window kernel, but through the graphical interface, click to complete our operation. The
    shell is the same for Linux The role of is mainly to parse our instructions and parse the instructions to the Linux kernel. The feedback result is then run through the kernel and then parsed to the user through the shell.
  • Special way of understanding: If you are a sullen and shy programmer, then the shell is like a matchmaker, and the operating system kernel is the beautiful flower in the village next to you and your heartbeat. You fell in love with Xiaohua, but you are embarrassed to confess directly, then let your family find a matchmaker to help you propose marriage. Then you only need to communicate with the matchmaker for the next thing, and the matchmaker will convey your thoughts to Xiaohua. The matchmaker we found was named Wang, so we called him Wangpo, which corresponds to the bash we often use

    Understanding of Linux permissions

    First of all, we know that there are two kinds of users under Linux: super user (root), ordinary user

  • Super user: can do anything under Linux system without restriction
  • Ordinary users: do limited things under the Linux system
  • The command prompt of the super user is "#", and the command prompt of the ordinary user is "$". We can distinguish based on this.
    Shell principle and understanding of Linux permissions

Command: su [user name]
Function: switch users.
For example, to switch from root user to normal user user, use su user. To switch from the normal user user to the root user, use su root (root can be omitted). At this time, the system will prompt for the root user's password

Linux permission management

01. Classification of file visitors (person)

  • Owners of files and file directories; u-Users
  • The user of the ancestor where the owner of the file and file directory is located; g-Group
  • Other users; o-Others

02. File types and access permissions (things attributes)

Shell principle and understanding of Linux permissions

  • d: folder
  • -: ordinary file
  • l: soft link (similar to Windows shortcut)
  • b: Block device files (such as hard disks, optical drives, etc.)
  • p: pipe file
  • c: Character device files (such as serial devices such as screens)
  • s: socket file
  • b) Basic permissions
    i. Read (r/4): For a file, Read has the permission to read the content of the file; for a directory, it has the permission to browse the directory information.
    ii. Write (w/2): Write has the right to modify file content for files; for directories, it has the right to delete/move files in the directory
    iii. Executable (x/1): execute is for files In other words, it has the permission to execute files; for the directory, it has the permission to enter the directory
    iv. "-" means that it does not have the permission

03. Representation method of file permission value

a) Character notation

Linux said Description Linux said Description
r-- Read only -w- Writable only
--x Executable only rw- Read and write
-wx Writable and executable r-x Readable and executable
rwx Readable, writable and executable --- No permission

b) Octal numeric representation

Permission symbol (read and write execution) Octal Binary
r 4 100
w 2 010
x 1 001
rw 6 110
rx 5 101
wx 3 011
rwx 7 111
--- 0 000

04. Related setting methods for file access permissions

a). chmod

  • Function: Set file access permissions
  • Format: chmod [parameter] permission file name
  • Common options:
  • [] R -> Recursively modify the permissions of directory files
  • [] Note: Only the owner and root of the file can change the file permissions

chmod command permission value format

  1. User indicator += permission character/ user indicator -= permission character

    • [] +: Add the authority represented by the authority code to the authority
    • [] -: Cancel the authority represented by the authority code from the authority
    • [] =: Grant the authority represented by the authority code to the authority
    • [] User symbol:
    • [] u: Owner
    • [] g: For the owner in the same group
    • [] o: Other users
    • [] a: All users

Example:

# chmod u+w /home/abc.text
# chmod o-x /home/abc.text
# chmod a=x /home/abc.txt
  1. Three-digit octal number

Example:

# chmod 664 /home/abc.txt
# chmod 640 /home/abc.txt

b). chown

  • Function: modify the owner of the file
  • Format: chown [parameter] username file name
  • Examples:
# chown user1 f1
# chown -R user1 filegroup1

c). chgrp

  • Function: modify the group of a file or directory
  • Format: chgrp [parameter] user group name file name
  • Common options:
    -R recursively modify the group belonging to a file or directory

  • Instance
# chgrp users /abc/f2

d). umask

  • Function: View or modify the file mask, the default permission of a new folder is=0666, and the default permission of a new directory=0777. But in fact, the permissions of the files and directories you create are often not the above values. The reason is that the creation of files or directories is also affected by umask. Assuming that the default permission is mask, the file permissions actually created are: mask & ~umask
  • Format: umask permission value
  • Note: After subtracting the permission mask from the existing access permissions, the default permissions when creating the file can be generated. Superuser default mask is 0022, with ordinary
    household defaults to 0002.

Directory permissions

  • Executable permissions: If the directory does not have executable permissions, you cannot cd into the directory
  • Read permission: If the directory does not have readable permission, you cannot use commands such as ls to view the contents of the files in the directory
  • Writable permission: If the directory does not have write permission, you cannot create files in the directory, nor can you delete files in the directory.

In other words, as long as the user has the write permission for the directory, the user can delete the file in the directory, regardless of whether the user has the write permission for the file. This is unscientific, so the concept of sticky bits is introduced in Linux.
When a directory is set to "sticky bit" (using chmod +t), the files in the directory can only be

  • 1. Super administrator delete
  • 2. Delete the owner of the directory
  • 3. Deleted by the owner of the file

Summary of permissions

  • The executable permission of the directory indicates whether you can execute commands in the directory.
  • If the directory does not have the -x permission, you cannot execute any commands on the directory, or even cd into the directory, even if the directory still has the -r read permission (this place is easy to make mistakes, you can enter the directory and read the files under the directory if you have the read permission )
  • If the directory has the -x permission but not the -r permission, the user can execute commands and cd to enter the directory. However, because there is no read permission of the directory, even if the ls command can be executed in the directory, it still does not have the permission to read the files in the directory.

Guess you like

Origin blog.51cto.com/14289099/2545236