English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Linux chmod command

Linux Command大全

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

Syntax

chmod [-cfvR] [--help] [--version] mode file...

Parameter description

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

Symbol pattern

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:

whoUser typeDescription
uuserFile owner
ggroupGroup of the file owner
oothersAll other users
aallAll users used, equivalent to ugo

Symbol pattern table of operator:

OperatorDescription
+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:

ModeNameDescription
rReadSet to read permissions
wWriteSet to write permissions
xExecution permissionsSet to executable permissions
XSpecial execution permissionsOnly when the file is a directory file, or other types of users have executable permissions, will the file permissions be set to executable
ssetuid/gidWhen the file is executed, set the setuid or setgid permissions of the file according to the user type specified by the who parameter
tPaste bitSet the paste bit, only the superuser can set this bit, and only the file owner u can use this bit

octal syntax

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.

#PermissionsrwxBinary
7Read + Write + Executerwx111
6Read + Writerw-110
5Read + Executer-x101
4Only readr--100
3Write + Execute-wx011
2Only write-w-010
1Only execute--x001
0No---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.

Online examples

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.

r =4, w =2, x =1

  • 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.

More Description

CommandDescription
chmod a+r fileGrant read permissions to all users of file
chmod a-x fileDelete execute permissions for all users of file
chmod a+rw fileGrant read and write permissions to all users of file
chmod +rwx fileGrant read, write, and execute permissions to all users of file
chmod u=rw,go=  fileSet 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 docsGrant 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 fileSet read and write permissions for the owner and group of file, and set read permissions for other users
chmod 0755 fileis equivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1)。0 No special mode.
chmod 4755 file4settings 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/

Linux Command大全