Linux chmod command

chmod 777 Launch!

Original Text

This article is transcoded by SimpRead, original URL www.runoob.com Shell File Inclusion Nginx Installation and Configuration

Linux Command Manual

Linux chmod (full English: change mode) command is used to control user permissions to files.

In 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 superuser can modify the permissions of files or directories. Permissions can be specified using absolute mode (octal numeric mode) or symbolic mode.

Usage permissions: all users

Syntax

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

Parameter Description

mode: permission setting string, format as follows:

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

Where:

  • u represents the file owner, g represents the group to which the file owner belongs, o represents others besides these, a means all three.
    • means add permission, - means remove permission, = means set permission exactly.
  • r means readable, w means writable, x means executable, X means executable only if the file is a directory or already has execute permission set.

Other parameters:

  • -c: only show changes if permission is actually modified
  • -f: do not show error messages if permission cannot be changed
  • -v: show detailed information about permission changes
  • -R: apply changes recursively to all files and subdirectories in the current directory
  • –help: display help information
  • –version: display version

Symbolic Mode

Symbolic mode allows setting multiple items: who (user type), operator, and permission, where each item can be separated by commas. The chmod command modifies the access permissions of the specified user type(s) on a file. User types are represented by one or more letters in the ‘who’ position, as shown in the symbolic mode table for who:

whoUser TypeDescription
uuserfile owner
ggroupfile owner's group
oothersall other users
aallall users, equivalent to ugo

Operator symbols:

OperatorDescription
+add permission for specified user type
-remove permission for specified user type
=set permission for specified user type, resetting all permissions

Permission symbols:

SymbolNameDescription
rreadset read permission
wwriteset write permission
xexecuteset execute permission
Xspecial executeexecute permission if file is directory or if other execute bit set
ssetuid/gidset setuid or setgid bit depending on user type specified
tsticky bitset sticky bit, only superuser can set, only file owner (u) can use it

Octal Syntax

The chmod command can use octal numbers to specify permissions. File or directory permissions are controlled by 9 permission bits grouped into three sets: user (owner) read, write, execute; group read, write, execute; others read, write, execute. Historically, file permissions were stored in bit masks where bits set to 1 indicate the corresponding permission.

#PermissionrwxBinary
7read + write + executerwx111
6read + writerw-110
5read + executer-x101
4read onlyr--100
3write + execute-wx011
2write only-w-010
1execute only--x001
0none---000

For example, 765 can be interpreted as:

  • Owner permissions number: sum of owner’s three permission bits. For rwx, 4+2+1 = 7.
  • Group permissions number: sum of group permission bits. For rw-, 4+2+0 = 6.
  • Others permissions number: sum of others permission bits. For r-x, 4+0+1 = 5.

Examples

Set file file1.txt to be readable by everyone:

chmod ugo+r file1.txt

Set file file1.txt to be readable by everyone:

chmod a+r file1.txt

Set file1.txt and file2.txt so that owner and group have write permission, but others don’t:

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

Add execute permission to owner of ex1.py:

chmod u+x ex1.py

Set all files and subdirectories in current directory readable by everyone:

chmod -R a+r *

chmod can also use numbers to represent permissions, e.g.:

chmod 777 file

Syntax:

chmod abc file

Where a, b, c each represent a number for User, Group, and Other permissions respectively.

r=4, w=2, x=1

  • For rwx attribute: 4+2+1=7;
  • For rw- attribute: 4+2=6;
  • For r-x attribute: 4+1=5.
chmod a=rwx file

and

chmod 777 file

have the same effect.

chmod ug=rwx,o=x file

and

chmod 771 file

have the same effect.

Using chmod 4755 filename can make the program have root permissions.

More Descriptions

CommandDescription
chmod a+r fileAdd read permission for all users on file
chmod a-x fileRemove execute permission from all users on file
chmod a+rw fileAdd read and write permissions for all users on file
chmod +rwx fileAdd read, write, execute permissions for all users on file
chmod u=rw,go= fileSet read and write permissions for file owner, clear all permissions for group and others (space means no permissions)
chmod -R u+r,go-r docsAdd read permission for user and remove read permission for group and others on directory docs and all files/subdirectories recursively
chmod 664 fileSet read and write permission for file owner and group, and read permission for others
chmod 0755 fileEquivalent to u=rwx (4+2+1),go=rx (4+1 & 4+1). No special mode (0)
chmod 4755 file4 sets the setuid bit, remaining permissions are 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 traverse or access the directory path/

Linux Command Manual