English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Linux chmod command (full name: change mode) is a command to control the permissions of users on files
Linux/Unix file access permissions are divided into three levels: file owner (Owner), user group (Group), and other users (Other Users).
Only the file owner and the superuser can modify the file or directory permissions. You can use absolute mode (octal number mode) or symbol mode to specify file permissions.
Use permission : All users
chmod [-cfvR] [--help] [--version] mode file...
mode: Permission setting string, format as follows:
[ugoa...][[+-=][rwxX]...][,...]
Among them:
u means the owner of the file, g means those who belong to the same group (group) as the file owner, o means others, and a means all three.
+ Means to add permissions,- Means to cancel permissions, = means to set permissions uniquely.
r means readable, w means writable, x means executable, and X means only when the file is a subdirectory or the file has been set as executable.
Other parameter descriptions:
-c: Only display the change action if the file permissions have indeed been changed
-f: Do not display error messages even if the file permissions cannot be changed
-v: Display detailed information about permission changes
-R: Change the same permissions for all files and subdirectories under the current directory (i.e., change one by one recursively)
--help: Display help information
--version: Display version
You can set multiple items using the symbol pattern: who (user type), operator (operator), and permission (permission), and the settings of each item can be separated by commas. The chmod command will modify the file access permissions for the user type specified by who, and the user type is indicated by one or more letters at the position of who, as shown in the symbol pattern table:
who | User type | Description |
---|---|---|
u | user | File owner |
g | group | Group of the file owner |
o | others | All other users |
a | all | All users used, equivalent to ugo |
Symbol pattern table of operator:
Operator | Description |
---|---|
+ | Add permissions for specified user types |
- | Remove the permissions of the specified user type |
= | Set the permission settings for the specified user, that is, reset all permissions for the user type |
The symbol mode table of permission symbol mode:
Mode | Name | Description |
---|---|---|
r | Read | Set to read permissions |
w | Write | Set to write permissions |
x | Execution permissions | Set to executable permissions |
X | Special execution permissions | Only when the file is a directory file, or other types of users have executable permissions, will the file permissions be set to executable |
s | setuid/gid | When the file is executed, set the setuid or setgid permissions of the file according to the user type specified by the who parameter |
t | Paste bit | Set the paste bit, only the superuser can set this bit, and only the file owner u can use this bit |
chmod command can use octal numbers to specify permissions. The permission bits of files or directories are determined by9permission bits to control, with three bits in a group, they are the read, write, and execute permissions for the file owner (User), the read, write, and execute permissions for the group (Group), and the read, write, and execute permissions for other users (Other). Historically, file permissions were placed in a bit mask, and the specified bits in the mask were set to1, used to indicate that a class has the corresponding priority.
# | Permissions | rwx | Binary |
---|---|---|---|
7 | Read + Write + Execute | rwx | 111 |
6 | Read + Write | rw- | 110 |
5 | Read + Execute | r-x | 101 |
4 | Only read | r-- | 100 |
3 | Write + Execute | -wx | 011 |
2 | Only write | -w- | 010 |
1 | Only execute | --x | 001 |
0 | No | --- | 000 |
For example, 765 It will be interpreted as follows:
The permission of the owner is expressed in numbers: the sum of the digits of the three permission bits of the owner. For example, rwx, which is 4+2+1 , should be 7.
The permission of the group is expressed in numbers: the sum of the digits of the permission bit of the group. For example, rw- , which is 4+2+0, should be 6.
The permission number expression for other users: the sum of the digits of the permission bits for other users. For example, r-x, which is 4+0+1 , should be 5.
Set the file file1.txt to be readable by everyone:
chmod ugo+r file1.txt
Set the file file1.txt to be readable by everyone:
chmod a+r file1.txt
Set the file file1.txt and file2.txt to be writable by the file owner and members of the same group, but not by others:
chmod ug+w,o-w file1.txt file2.txt
For ex1The .py file owner is granted executable permissions:
chmod u+x ex1.py
Set all files and subdirectories in the current directory to be readable by anyone:
chmod -R a+r *
In addition, chmod can also represent permissions with numbers such as:
chmod 777 file
The syntax is:
chmod abc file
Among them, a, b, c are each a number, representing the permissions of User, Group, and Other respectively.
In order to rwx attribute 4+2+1=7;
In order to rw- The attribute is 4+2=6;
In order to r-x attribute 4+1=5.
chmod a=rwx file
and
chmod 777 file
has the same effect
chmod ug=rwx,o=x file
and
chmod 771 file
has the same effect
If using chmod 4755 filename enables this program to have root privileges.
Command | Description |
---|---|
chmod a+r file | Grant read permissions to all users of file |
chmod a-x file | Delete execute permissions for all users of file |
chmod a+rw file | Grant read and write permissions to all users of file |
chmod +rwx file | Grant read, write, and execute permissions to all users of file |
chmod u=rw,go= file | Set read and write permissions for the owner of file, clear all permissions for the group and other users for file (spaces represent no permissions) |
chmod -R u+r,go-r docs | Grant read permissions to all files in the directory docs and its subdirectory hierarchy for users, and delete read permissions for groups and other users |
chmod 664 file | Set read and write permissions for the owner and group of file, and set read permissions for other users |
chmod 0755 file | is equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1)。0 No special mode. |
chmod 4755 file | 4settings have been setUser IDbit, the rest is equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1)。 |
find path/ -type d -exec chmod a-x {} \; | Delete executable permissions for path/and all its directories (excluding files) for all users, using'-type f'matches files |
find path/ -type d -exec chmod a+x {} \; | Allow all users to browse or access through directory path/ |