Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux chmod Command

Linux chmod command

Linux Command Reference Linux Command Reference

The Linux chmod (short for "change mode") command is used to control users' permissions on files.

chmod (change mode) is a command in Linux used to change the permissions of files or directories. It controls the access rights of the file owner, the owning group, and other users.

Only the file owner and the superuser can modify the permissions of a file or directory.

File access permissions in Linux/Unix are divided into three levels: file owner (Owner), user group (Group), and other users (Other Users).

You can use absolute mode (octal numeric mode) or symbolic mode to specify file permissions.

Permissions : all users

Syntax

chmod [选项] 权限模式 文件...

chmod [选项] --reference=参考文件 文件...

Common options

mode: the permission setting string, in the following format:

Permission modes

1. Symbolic mode (ugoa+/-permissions)

[ugoa...][[+-=][rwxX]...][,...]

Where:

Symbolic mode can set multiple items: who (user type), operator, and permission; the settings for each item can be separated by commas.

The chmod command modifies the file access permissions for the user type specified by who. The user type is indicated by one or more letters in the who position, as shown in the symbolic mode table for who:

who User types Description
u user File owner
g group The group of the file owner
o others All other users
a all All users, equivalent to ugo

Symbolic mode table for operator:

Operator Description
+ Add a permission for the specified user type
- Remove a permission for the specified user type
= Set the permissions of the specified user type, i.e., reset all permissions for that user type

Permission types:

Mode Name Description
r Read Set as readable permission
w Write Set as writable permission
x Execute permission Set as executable permission
X Special execute permission Only set the file as executable if the file is a subdirectory, or if other types of users have execute permission
s setuid/gid When the file is executed, set the file's setuid or setgid permission according to the user type specified by the who parameter
t Sticky bit Set the sticky bit. Only the superuser can set this bit, and only the file owner (u) can use it

2. Octal syntax

The chmod command can use octal numbers to specify permissions.

Digit Permission
4 Read (r)
2 Write (w)
1 Execute (x)

Combination:

Common combinations:

The permissions of a file or directory are controlled by 9 permission bits, grouped in threes: read, write, and execute for the file owner (User), read, write, and execute for the user group (Group), and read, write, and execute for other users (Other).

# 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 is interpreted as follows:

Example

Make the file file1.txt readable by everyone:

chmod ugo+r file1.txt

Make the file file1.txt readable by everyone:

chmod a+r file1.txt

Make the files file1.txt and file2.txt writable by the owner and by users in the same group, but not writable by others:

chmod ug+w,o-w file1.txt file2.txt

Add execute permission for the owner of the file ex1.py:

chmod u+x ex1.py

Make all files and subdirectories in the current directory readable by anyone:

chmod -R a+r *

In addition, chmod can also use numbers to represent permissions, for example:

chmod 777 file

The syntax is:

chmod abc file

Here a, b, and c are each a number, representing the permissions of User, Group, and Other respectively.

r=4, w=2, x=1

chmod a=rwx file

and

chmod 777 file

Same effect

chmod ug=rwx,o=x file

and

chmod 771 file

Same effect

Using chmod 4755 filename can give this program root privileges.

More details

Command Description
chmod a+r file Add read permission for all users of file
chmod a-x file Remove execute permission for all users of file
chmod a+rw file Add read and write permissions for all users of file
chmod +rwx file Add read, write, and execute permissions for all users of file
chmod u=rw,go= file Set read and write permissions for the owner of file, and clear all permissions for the group and other users on file (a space means no permission)
chmod -R u+r,go-r docs Add read permission for the user on the directory docs and all files in its subdirectory hierarchy, while removing read permission for the group and other users
chmod 664 file Set read and write permissions for the owner and group of file, and set read permission for other users
chmod 0755 file is equivalent tou=rwx (4+2+1),go=rx (4+1 & 4+1).0 No special mode.
chmod 4755 file 4The set-user-ID bit is set, and the rest is equivalent to u=rwx (4+2+1), go=rx (4+1 & 4+1).
find path/ -type d -exec chmod a-x {} ; Remove execute permission for all users on path/ and all its directories (not files); use '-type f' to match files
find path/ -type d -exec chmod a+x {} ; Allow all users to browse or pass through the directory path/

Linux Command Reference Linux Command Reference