Chmod Quick Dev Reference

This quick reference cheat sheet provides a brief overview of file permissions, and the operation of the chmod.

Getting Started

Syntax

$ chmod [options] <permissions> <file> 

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

$ chmod -R 755 my_directory

The chmod command stands for "change mode"

Chmod Generator

Read β€” β€” β€”

Write β€” β€” β€”

Execute β€” β€” β€”

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

Common Permissions

400 β€” r-------- β€” Readable by owner only

500 β€” r-x------ β€” Avoid Changing

600 β€” rw------- β€” Changeable by user

644 β€” rw-r--r-- β€” Read and change by user

660 β€” rw-rw---- β€” Changeable by user and group

700 β€” rwx------ β€” Only user has full access

755 β€” rwxr-xr-x β€” Only changeable by user

775 β€” rwxrwxr-x β€” Sharing mode for a group

777 β€” rwxrwxrwx β€” Everybody 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

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

--- β€” No Permission β€” 000 β€” 0 (0+0+0)

--x β€” Execute β€” 001 β€” 1 (0+0+1)

-w- β€” Write β€” 010 β€” 2 (0+2+0)

-wx β€” Execute and Write β€” 011 β€” 3 (0+2+1)

r-- β€” Read β€” 100 β€” 4 (4+0+0)

r-x β€” Read and Execute β€” 101 β€” 5 (4+0+1)

rw- β€” Read and Write β€” 110 β€” 6 (4+2+0)

rwx β€” Read, Write and Execute β€” 111 β€” 7 (4+2+1)

Objects

u β€” User

g β€” Group

o β€” Others

a β€” All, same as ugo

Permissions

r β€” Read β€” 4

w β€” Write β€” 2

x β€” Execute β€” 1

- β€” No permission β€” 0

File Types

d β€” Directory

- β€” Regular file

l β€” Symbolic Link

Chmod Examples

Operators

+ β€” 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

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

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