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.
| Octal | Symbolic | Owner | Group | Others | Typical use |
|---|---|---|---|---|---|
| 777 | rwxrwxrwx | rwx | rwx | rwx | Full access (avoid for security) |
| 755 | rwxr-xr-x | rwx | r-x | r-x | Directories and executable scripts |
| 644 | rw-r--r-- | rw- | r-- | r-- | Regular files |
| 700 | rwx------ | rwx | --- | --- | Private scripts |
| 600 | rw------- | rw- | --- | --- | Private keys and config |
| 400 | r-------- | r-- | --- | --- | Read-only private key |
| 750 | rwxr-x--- | rwx | r-x | --- | Group-executable dirs |
| 640 | rw-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)