Linux chmod Command
The Linux chmod (full spelling: change mode) command is used to control user permissions for files.
Linux/Unix file access permissions are categorized into three levels: file owner (Owner), user group (Group), and other users (Other Users).
Only the file owner and the superuser can modify the permissions of a file or directory. Permissions can be specified using absolute mode (octal numeric mode) or symbolic mode.
Usage Permission: All users
Syntax
chmod [-cfvR] [--help] [--version] mode file...
Parameter Description
mode: Permission setting string, format as follows:
[ugoa...][[+-=][rwxX]...][,...]
Where:
u represents the owner of the file, g represents users in the same group as the owner, o represents others, and a represents all three.
- indicates adding permissions, - indicates removing permissions, and = indicates setting permissions exclusively.
r indicates readable, w indicates writable, x indicates executable, and X indicates executable only if the file is a directory or already has executable permissions set.
-c: Displays the change if the file permissions have actually been modified.
-f: Suppresses error messages if the file permissions cannot be modified.
-v: Displays detailed information about the permission changes.
-R: Recursively changes permissions for all files and subdirectories in the current directory.
--help: Displays help information.
--version: Displays the version.
Symbolic Mode
Symbolic mode allows setting multiple items: who (user type), operator (operator), and permission (permission). Each setting can be separated by commas. The chmod command will modify the access permissions for the user type specified by who, which can be one or more letters as shown in the symbolic mode table for who:who | User Type | Description |
---|---|---|
u | user | File owner |
g | group | Group of the file owner |
o | others | All other users |
a | all | All users, equivalent to ugo |
Operator | Description |
---|---|
+ | Adds permissions for the specified user type |
- | Removes permissions for the specified user type |
= | Sets permissions exclusively for the specified user type |
Mode | Name | Description |
---|---|---|
r | read | Sets read permission |
w | write | Sets write permission |
x | execute | Sets execute permission |
X | special execute | Sets execute permission only if the file is a directory or already has execute permissions set |
s | setuid/gid | Sets setuid or setgid permissions on execution based on the specified user type |
t | sticky bit | Sets the sticky bit, which can only be set by the superuser, and can only be used by the file owner u |
Octal Syntax
The chmod command can specify permissions using octal numbers. File or directory permissions are controlled by 9 permission bits, grouped into three sets of three bits each: read, write, and execute for the file owner (User), user group (Group), and other users (Other). Historically, file permissions were placed in a bitmask, with specified bits set to 1 to indicate a class's priority.# | Permission | rwx | Binary |
---|---|---|---|
7 | read + write + execute | rwx | 111 |
6 | read + write | rw- | 110 |
5 | read + execute | r-x | 101 |
4 | read only | r-- | 100 |
3 | write + execute | -wx | 011 |
2 | write only | -w- | 010 |
1 | execute only | --x | 001 |
0 | none | --- | 000 |
Owner permissions expressed numerically: The sum of the three permission bits for the owner. For example, rwx, which is 4+2+1, should be 7.
Group permissions expressed numerically: The sum of the permission bits for the group. For example, rw-, which is 4+2+0, should be 6.
Other users' permissions expressed numerically: The sum of the permission bits for other users. For example, r-x, which is 4+0+1, should be 5.
Example
Set the file file1.txt to be readable by everyone:chmod a+r file1.txt
chmod ugo+r file1.txt
Set read permission for all users on file1.txt:
chmod a+r file1.txt
Set write permission for the owner and group, but remove write permission for others on file1.txt and file2.txt:
chmod ug+w,o-w file1.txt file2.txt
Add execute permission for the owner of the ex1.py file:
chmod u+x ex1.py
Set read permission for all users on all files and subdirectories in the current directory:
chmod -R a+r *
Alternatively, permissions can be represented by numbers:
chmod 777 file
The syntax is:
chmod abc file
Where a, b, and c are numbers representing the permissions for User, Group, and Other, respectively.
r=4, w=2, x=1
- For rwx permission, 4+2+1=7;
- For rw- permission, 4+2=6;
- For r-x permission, 4+1=5.
chmod a=rwx file
chmod 777 file
are equivalent.
chmod ug=rwx,o=x file
and
chmod 771 file
are equivalent.
Using `chmod 4755 filename` can grant this program root permissions.
More Details
Command | Description |
---|---|
chmod a+r file | Add read permission for all users on file |
chmod a-x file | Remove execute permission for all users on file |
chmod a+rw file | Add read and write permission for all users on file |
chmod +rwx file | Add read, write, and execute permission for all users on file |
chmod u=rw,go= file | Set read and write permission for the owner of file, clear all permissions for the group and others |
chmod -R u+r,go-r docs | Add read permission for the owner and remove read permission for the group and others on all files and directories under docs |
chmod 664 file | Set read and write permission for the owner and group, read permission for others on file |
chmod 0755 file | Equivalent to u=rwx (4+2+1), go=rx (4+1 & 4+1). 0 has no special mode. |
chmod 4755 file | 4 sets the set user ID |
find path/ -type d -exec chmod a-x {} \; | Remove execute permission for all users on all directories under path/ (excluding files) |
find path/ -type d -exec chmod a+x {} \; | Allow all users to browse or traverse directories under path/ |