Linux permissions (chmod, chown)
Reading an ls -l line, chmod's octal notation (755, 644…) and chown: enough to stop typing 777 out of habit.
Reading an ls -l line
-rwxr-xr-- 1 ana devs 4096 Sep 10 09:00 script.sh
| Block | Meaning |
|---|---|
- |
type (- file, d directory, l symlink) |
rwx |
owner permissions |
r-x |
group permissions |
r-- |
other permissions |
r = read (4) · w = write (2) · x = execute (1)
chmod: octal notation
chmod 755 script.sh # rwx r-x r-x (owner all, others read+execute) chmod 644 file.txt # rw- r-- r-- (owner read/write, others read) chmod 600 secret.env # rw- --- --- (owner only) chmod -R 755 dir/ # recursive on a directory
chmod: symbolic notation
chmod u+x script.sh # add execute for the owner (u) chmod g-w file.txt # remove write for the group (g) chmod o=r file.txt # set others (o) to read-only chmod a+r file.txt # a = everyone (u+g+o)
chown: changing the owner
chown ana file.txt # change the owner chown ana:devs file.txt # change owner and group chown -R ana:devs dir/ # recursive
chmod 777 (everyone can do anything) is almost always a bad idea, especially on a server: prefer the minimum right needed (often 755 for directories/executables, 644 for data files).
Special bits: setuid, setgid, sticky bit
| Notation | Effect |
|---|---|
chmod u+s file |
setuid: runs with the owner's rights |
chmod g+s dir |
setgid: new files inherit the directory's group |
chmod +t dir |
sticky bit: only a file's owner can delete it, even if the directory is world-writable (the /tmp case) |
drwxrwxrwt 12 root root 4096 ... /tmp
The trailing t marks the sticky bit.
Thanks for the feedback!