Easy Tutorial
❮ Linux Comm Uuname Linux Comm Rdev ❯

Linux chmod Command

Linux Command Manual

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:

Other parameter descriptions:

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 symbolic mode table:

Operator Description
+ Adds permissions for the specified user type
- Removes permissions for the specified user type
= Sets permissions exclusively for the specified user type
Permission symbolic mode table:

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
For example, 765 would be interpreted as:

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

and
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/

Linux Command Manual

❮ Linux Comm Uuname Linux Comm Rdev ❯