Table of Contents

  1. Introduction to the
  2. User and user group
  3. File attributes
  4. The file type
  5. Permission information
  6. Example Modify the file owner and owning user group
  7. Modifying file Permissions
    1. Numeric type changes file permissions
    2. Symbol types change file permissions
  8. Role of permissions
  9. Refer to the link

Introduction to the

In the process of learning Linux, file permissions are a very important concept, after all, all operations in Linux are inseparable from the support of permissions.

This blog post is a simple summary of learning about Linux file permissions.

User and user group

Linux file permissions are a concept closely related to users and user groups. For a file, users can be divided into three categories:

  • Of the fileThe owneruser
  • The owner of the fileUser groupsOther members of the –group
  • In addition toThe ownerUser groupsUsers other than members –others

File permissions are relative to these three types of users. Users with different identities can have different permissions.

File attributes

Use the ls -l command to obtain detailed information about the file, such as:

drwxr-xr-x 5 root root 4096 Sep 7 19:52 dotemacs drwxr-xr-x 6 root root 4096 Sep 3 22:34 dotfiles drwxr-xr-x 9 root root  4096 Sep 11 10:19 emacs-kitCopy the code

We can divide each piece of information into 8 parts. For example, the first piece of information above can be divided into:

The file type Permission information Number of links The owner User groups The file size Modification date The file name
d rwxr-xr-x 5 root root 4096 Sep 7 19:52 dotemacs

Here is mainly to understand the file type and permission information, for other parts of the interest can Google.

The file type

In Linux, files can be divided into five types. The first character of each message output by ls -L indicates the file type.

Each file type and its characters are:

According to the character The file type
d directory
- file
l Symbolic links and so on
b Interface devices available for storage
c Serial port devices such as keyboards, mice, etc

Permission information

Based on the concept of users and user groups, it can be seen that permissions are related to three types of users. Therefore, the permission information displayed in LS -L is also divided into three groups.

The permission information of each file consists of nine characters and is divided into three groups, corresponding to the permissions of the owner, user group, and others.

For permission information rwxr-xr-x, the permission of the owner is RWX, and the permission of the user group and others is R-x.

Each group is divided into read permission, write permission, and execute permission, which are represented by characters R, w, and x respectively. If you do not have the appropriate permissions, the character – is used.

Therefore, permission information rwxr-xr-x can be interpreted as: the owner has read, write, and execute permissions, while the user group and others have only read and execute permissions.

Example Modify the file owner and owning user group

The chown and CHGRP commands can be used to change the owner and user group of a file in a simple manner:

$chown [-r] Account name file or directory $chown [-r] Account name: group name file or directory $CHGRP [-r] User group name file or directoryCopy the code

The -r parameter changes recursively continuously, that is, with all files in the subdirectory.

Modifying file Permissions

The chmod command is used to modify file permissions. You can modify file permissions in two ways.

Numeric type changes file permissions

We can use numbers to represent each permission, and the corresponding numbers of each permission are:

r:4
w:2
x:1
Copy the code

For example, if the permission is RWXRWX –, the corresponding score is:

user   = rwx = 4+2+1 = 7
group  = rwx = 4+2+1 = 7
others = --- = 0+0+0 = 0
Copy the code

The resulting file permission number is 770, which can be used to modify permissions:

$chmod [-r] 770 File or directoryCopy the code

Obviously, changing file permissions in this way is cumbersome, so chmod provides another, more convenient way to change file permissions.

Symbol types change file permissions

Symbol type Changing file permissions must follow certain syntax rules, including identity symbols, operation symbols, and permission symbols.

Identifiers:

Said operator Identity of representative
u File owner
g User group to which the file owner belongs
o others
a All users

Operation representation:

Said operator Operations on behalf
+ Add permissions
- Remove permissions
= Set permissions

The permission representation is r, W, and x.

From the composition of the representation, you can probably guess how to modify file permissions. For example, the following command gives the owner all permissions and adds execute permissions to user groups and others:

chmod u=rwx,go+x .vimrc
Copy the code

Note that there is no space between u= RWX and go+x.

Role of permissions

The same permission has different meanings for files and directories.

permissions file directory
r File contents can be read A list of directory structures can be read
w You can edit and modify the file content You can change the directory structure list
x Can be executed by the system Users can access directories (cd)

One of the permissions to note here is that you can change the directory structure list, which means you can:

  • Create new files and directories
  • Delete existing files and directories
  • Rename an existing file or directory
  • Move files and directory locations in the directory

Therefore, the permission of W should be used carefully.

Refer to the link