Programming
Chmod

Getting Started

Syntax

$ chmod [options] <permissions> <file> 

Example

$ chmod 755 foo.txt
$ chmod +x cheatsheets.py
$ chmod u-x cheatsheets.py
$ chmod u=rwx,g=rx,o= cheatsheets.sh

Change files and directories recursively

$ chmod -R 755 my_directory

The chmod command stands for "change mode"

Chmod Generator

Chmod Generator allows you to quickly and visually generate permissions in numerical and symbolic.

Common Permissions

CommandsMeaning
400r--------Readable by owner only
500r-x------Avoid Changing
600rw-------Changeable by user
644rw-r--r--Read and change by user
660rw-rw----Changeable by user and group
700rwx------Only user has full access
755rwxr-xr-xOnly changeable by user
775rwxrwxr-xSharing mode for a group
777rwxrwxrwxEverybody can do everything

Explains

$ ls -l
-rw-r--r--  1 root root 3 Jun 29 15:35 a.log
drwxr-xr-x  2 root root 2 Jun 30 18:06 dir

Permission analysis of "dir"

d  rwx  r-x  r-x
┬  ─┬─  ─┬─  ─┬─  
│   │    │    │  
│   │    │    └─ 4. Other|5 (4+0+1)
│   │    └────── 3. Group|5 (4+0+1)
│   └─────────── 2. User |7 (4+2+1)
└─────────────── 1. File Type | directory

Permission Modes {.col-span-2}

PermissionDescriptionOctalDecimal
---No Permission0000 (0+0+0)
--xExecute0011 (0+0+1)
-w-Write0102 (0+2+0)
-wxExecute and Write0113 (0+2+1)
r--Read1004 (4+0+0)
r-xRead and Execute1015 (4+0+1)
rw-Read and Write1106 (4+2+0)
rwxRead, Write and Execute1117 (4+2+1)
{.show-header}

Objects

Who (abbr.)Meaning
uUser
gGroup
oOthers
aAll, same as ugo
{.show-header}

Permissions

AbbreviationPermissionValue
rRead4
wWrite2
xExecute1
-No permission0
{.show-header}

File Types

AbbreviationFile Type
dDirectory
-Regular file
lSymbolic Link
{.show-header}

Chmod Examples

Operators

SymbolDescription
+Add
-Remove
=Set

chmod 600

$ chmod 600 example.txt
$ chmod u=rw,g=,o= example.txt
$ chmod a+rwx,u-x,g-rwx,o-rwx example.txt

chmod 664

$ chmod 664 example.txt
$ chmod u=rw,g=rw,o=r example.txt
$ chmod a+rwx,u-x,g-x,o-wx example.txt

chmod 777

$ chmod 777 example.txt
$ chmod u=rwx,g=rwx,o=rwx example.txt
$ chmod a=rwx example.txt

Symbolic mode {.row-span-3}

Deny execute permission to everyone.

$ chmod a-x chmodExampleFile.txt

Allow read permission to everyone.

$ chmod a+r chmodExampleFile.txt

Make a file readable and writable by the group and others.

$ chmod go+rw chmodExampleFile.txt

Make a shell script executable by the user/owner.

$ chmod u+x chmodExampleScript.sh

Allow everyone to read, write, and execute the file and turn on the set group-ID.

$ chmod =rwx,g+s chmodExampleScript.sh

Removing Permissions {.row-span-3}

In order to remove read write permissions given to a file, use the following syntax:

$ chmod o-rw example.txt

For our file example.txt, we can remove read write permissions using chmod for group by running the following command:

$ chmod  g-rx example.txt

To remove chmod read write permissions from the group while adding read write permission to public/others, we can use the following command:

$ chmod g-rx, o+rx example.txt

But, if you wish to remove all permissions for group and others, you can do so using the go= instead:

$ chmod go= example.txt

Executable

$ chmod +x ~/example.py
$ chmod u+x ~/example.py
$ chmod a+x ~/example.py

chmod 754

$ chmod 754 foo.sh
$ chmod u=rwx,g=rx,o=r foo.sh

Chmod Practices

SSH Permissions

$ chmod 700 ~/.ssh
$ chmod 600 ~/.ssh/authorized_keys
$ chmod 600 ~/.ssh/id_rsa
$ chmod 600 ~/.ssh/id_rsa.pub
$ chmod 400 /path/to/access_key.pem

Web Permissions

$ chmod -R 644 /var/www/html/
$ chmod 644 .htaccess
$ chmod 644 robots.txt
$ chmod 755 /var/www/uploads/
$ find /var/www/html -type d -exec chmod 755 {} \;

Batch Change

$ chmod -R 644 /your_path
$ find /path -type d -exec chmod 755 {} \;
$ find /path -type f -exec chmod 644 {} \;

See: Command Substitution (opens in a new tab)

Also see