Knowledge about file permissions in Linux

In a Linux system, each file has an owner and a user group. In addition, the system defines a category of "Others".

The owner of a file is usually the user who created the file, and the user group is specified when the file was created. If not specified, defaults to the creating user's primary user group. The user group can be specified when the file is created, or can be modified by the chgrp command after the file is created.

Others: Refers to other users except the owner and user group. In some places, "others" is also called "public".

Question: What is the meaning of "create the user's main user group" mentioned above?
Answer: In the Linux system, each user has a main user group. Each user account has a user ID (UID) and a user group ID (GID).

When creating a new user, you will be asked to specify the primary user group to which the user belongs. This main user group is automatically created when creating a user.

In a Linux system, a group is a collection of multiple users. After a group is created in the system, users can be added to the group. This way, all users belonging to the group inherit the group's permissions. A user can belong to multiple groups, but there can only be one primary user group.

Normally, a user's primary user group has the same name as their user name, but this is not required. When creating a user, you can use the -g option to specify the user's primary user group. For example, use the following command to create a user named myuser and set its primary user group to mygroup:

sudo useradd -g mygroup myuser

This will create a new user named myuser, add it to the mygroup group, and set the mygroup group to be myuser's primary user group. If the -g option is not specified, the default is to use the same group as the username as the primary user group.

Since each file has three identity attributes of owner, user group and others, its file permissions are divided into three identities.
1. Owner's authority;
2. User group's authority;
3. Other people's authority, also called "other people" or "public".

As shown below:
insert image description here

From the screenshot above, we can see that there are three permissions for each identity, which are "read, write, and execute". We can use numbers to indicate the permissions allowed, and we can also use letters to indicate the permissions allowed. .

Let’s first talk about the situation of using numbers to indicate permission permission:
the permission of each identity is represented by three binary bits. If the permission is allowed, the value is 1, otherwise the value is 0.
In the above screenshot, the permission of the file owner is the binary value "111", corresponding to the number 7 in decimal; in the above
screenshot, the permission of the file user group is the binary value "101", corresponding to the number 5 in decimal; the
above screenshot , the authority of the public user group (other user groups) is the binary value "100", corresponding to the number 4 in decimal;
so the final authority value of this file is: 754

Let's talk about letters to indicate the permission of the permission:
three kinds of permissions, namely read permission, write permission, and execute permission, are represented by letters r, w, and x respectively.
If there is no such permission, it is represented by the symbol "-".
For example, the above 754 permissions can be expressed in letters as:

rwxr-xr--

You can use the command "chmod" to change the permission settings of the file. "chmod" is an abbreviation for "chage mode". Here are some examples:

chmod +x /opt/python_scripts/your_code.py

The above command will add executable permission for all users including owner, owning group and others. The symbol "+" means adding permission, and the letter "x" means executing permission, so "+x" means adding executable permission.

If you just want to add executable permissions to the owner, you can use the command:

chmod u+x /opt/python_scripts/your_code.py

Among them, the symbol "u" ​​means the user (that is, the owner of the file), and the letter "x" means the executable permission, so "u+x" means adding executable permission for the user.

What if I want to change permissions using numbers? How to write chmod statement. For example, I want to adjust the permission of the file your_code.py to 755, how should I do it?

To change file permissions numerically, use the command:

chmod 755 /opt/python_scripts/your_code.py

The meaning of the number 755 has already been introduced above, so I won’t repeat it here.

Guess you like

Origin blog.csdn.net/wenhao_ir/article/details/130151125