Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Each file and directory in The Linux system is configured with access permissions for different users to determine how users can access and operate files and directories. That is, only authorized users can access and operate files and directories.
Next, you’ll see how to change access to files and directories using the chmod command.
Linux File Permissions
In Linux, there are three different types of users who can access files or directories:
- File owner
- Group User
- Other users
There are three types of file permissions:
- R: read permission
- W: write permission
- X: execution permission
We can assign different file access permissions to three different categories of users, specifying which users are allowed to read, write, or execute files.
You can use the ls command to view file permissions:
ls -l test.sh
-rwxr-xr-x. 1 root root 2519 May 19 10:13 test.sh
Copy the code
We can see something like -rwxr-xr-x with 10 bits in total:
-rwxr-xr-x
||||||||||
||||||||||
||||||||||
12345678910
Copy the code
- The first one,
-
Represents a file type, which can be a regular file-
, directories,d
Symbolic linkl
Or other types of files - The next three,
rwx
Represents the owner’s permissions - And the next three,
r-x
Represents the permissions of the same group - The last three,
r-x
Represents the rights of other users
Note: – Indicates that no corresponding permission has been granted.
Run the chmod command to change file directory permissions
The chmod command is used to change the access permission of a file or directory.
Syntax format
Chmod [who] [+ | | =] [mode] file nameCopy the code
Mode indicates the mode state of access, usually using several specific symbols to set the status of access.
Set the user status | The operator | permissions |
---|---|---|
u | + | r |
g | – | w |
o | = | x |
a |
The permission state can be divided into three parts.
-
Set user status:
u
: indicates the permission of the ownerg
: Indicates the permission of the groupo
: indicates the permission of other usersa
: indicates the rights of all users
-
Operator:
+
: Adds a permission to an existing permission-
: indicates that an existing permission is removed=
: indicates that the permission is set to the later value
-
Jurisdiction:
r
: Read permissionw
: Write permissionx
: Execute permission
For example, add owner and group user executable permissions to the test file:
chmod ug+x test
Copy the code
In addition, the chmod command also supports numeric representation of file permissions to be changed. You can use a set of 3-digit numbers to represent permissions on a file directory:
- The first number represents the permission of the owner, i.e
u
- The second digit indicates the permission of the group, i.e
g
- The third digit indicates the permissions of other users, i.e
o
Each digit is the sum of the following permission status digits (4, 2, 1, 0) :
- 4: read permission
- 2: write permission
- 1: execution permission
- 0: no corresponding permission is available
Add these numbers together to get a number between 0 and 7 that represents the status of the owner, peer group, and other user privileges.
For example, to grant read, write, and execute permissions to the test file owner, read and execute permissions to the same group, and only read permissions to all other users, you can do the following:
- Owner:
rwx = 4 + 2 + 1 = 7
- Groups:
rx = 4 + 0 + 1 = 5
- Other:
rx = 4 + 0 + 0 = 4
chmod -R 754 test
Copy the code
conclusion
The chmod command can be used to change the access permission of files and directories in both symbol and digit modes.
Original is not easy, if small partners feel helpful, please click a “like” and then go ~
Finally, thank my girlfriend for her tolerance, understanding and support in work and life!