Linux File Permissions

chmod, chown and the octal values behind read, write and execute


How the numbers work

Each permission is a value: read = 4, write = 2, execute = 1. Add them together for each of owner, group and others.

OctalSymbolicOwnerGroupOthersTypical use
777rwxrwxrwxrwxrwxrwxFull access (avoid for security)
755rwxr-xr-xrwxr-xr-xDirectories and executable scripts
644rw-r--r--rw-r--r--Regular files
700rwx------rwx------Private scripts
600rw-------rw-------Private keys and config
400r--------r--------Read-only private key
750rwxr-x---rwxr-x---Group-executable dirs
640rw-r-----rw-r-----Group-readable files

chmod

chmod 755 script.sh              # set with octal
chmod u+x script.sh              # add execute for owner
chmod g-w file.txt               # remove write for group
chmod -R 755 /var/www            # recursive

chown and chgrp

chown user:group file.txt        # change owner and group
chown -R user:group /var/www     # recursive
chgrp group file.txt             # change group only

Special bits

chmod 4755 file    # setuid: run as the file owner
chmod 2755 dir     # setgid: inherit directory group
chmod 1777 dir     # sticky: only owners can delete (e.g. /tmp)