Linux Command Reference ยท Concepts
Linux permission bits explained
How Linux file permission bits work: read, write, execute for user, group and others, octal and symbolic notation, setuid, setgid, the sticky bit and umask.
Every file and directory carries a mode: twelve bits that decide who may read, write or run it. Nine of them are the familiar rwxr-xr-x; three more (setuid, setgid, sticky) change how execution and deletion behave. The mode lives in the inode and is changed with chmod.
You can write a mode as letters (u=rwx,go=rx) or as an octal number (755). They are two spellings of the same bits, and switching between them is a skill worth having.
ls -l script.sh # -rwxr-xr-x ... script.sh chmod 755 script.sh # octal chmod u=rwx,go=rx script.sh # symbolic, same result
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
How the bits are laid out
The mode has three classes (owner u, group g, others o), each with three bits:
| Bit | Value | On a file | On a directory |
|---|---|---|---|
r |
4 | read the content | list the names inside |
w |
2 | change the content | create, rename and delete entries (needs x too) |
x |
1 | run it as a program | enter it and reach entries by name |
Add the values within a class to get one octal digit: rwx is 4+2+1 = 7, r-x is 5, rw- is 6, r-- is 4. Three digits give owner, group and others, so 640 means owner rw-, group r--, others nothing.
Linux checks one class only: if you are the owner, the owner bits decide (even if the group bits are more generous); otherwise if you are in the file's group, the group bits; otherwise the others bits. root bypasses read and write checks, and needs at least one x bit set to execute a file.
The three special bits
A fourth leading octal digit holds the special bits:
| Octal | Symbol | Name | Effect |
|---|---|---|---|
| 4000 | s in owner x slot |
setuid | A program runs with the file owner's identity, not the caller's. passwd relies on it. |
| 2000 | s in group x slot |
setgid | A program runs with the file's group. On a directory, new files inherit the directory's group. |
| 1000 | t in others x slot |
sticky | In a directory, only the file's owner (or the directory owner or root) may delete or rename entries. /tmp uses it. |
A capital S or T means the special bit is set but the matching x is not, which is rarely what you want. Linux ignores setuid and setgid on interpreted scripts (the shebang kind), so they work only on real binaries.
Symbolic notation
chmod accepts who (u g o a), an operator (+ add, - remove, = set exactly) and permissions (r w x, plus s, t, and X). chmod g+w file adds to the current mode; chmod 664 file replaces it. The capital X sets execute only on directories or files that already have an execute bit, which makes chmod -R u=rwX,go=rX tree the safe way to fix a whole tree.
Usage notes
- Default modes come from
umask. New files start at 666 and new directories at 777, minus the mask bits. Withumask 022that gives 644 and 755. - Never use 777 as a fix. It lets any user on the machine change the file. Find who actually needs access and use group permissions or ACLs.
- Directory
xis the gate. Withoutxon every directory in a path you cannot reach the file, whatever the file's own mode says. - Deleting needs the directory, not the file.
won the directory lets you remove a read-only file inside it, unless the sticky bit says otherwise. - Symbolic links show
lrwxrwxrwxand their mode is ignored; the target's mode applies. - Octal modes in scripts: leading zeros matter to some tools (
mkdir -m 0755), butchmod 755andchmod 0755are the same. - Read the output of
ls -lfrom left: file type, then nine permission characters, then link count, owner, group, size.
Related
chmod,chown,umask,stat,getfaclandsetfacl- Lessons: Users and Permissions, ACLs and Attributes