Understanding chmod and File Permissions
Every file and directory in Unix-like systems has three permission layers: owner (the user who created it), group (a set of users), and others (everyone else). Each layer can grant or deny three actions: read (r), write (w), and execute (x).
The chmod commandβshort for "change mode"βadjusts these permissions. You can express permissions symbolically as rwx or numerically as octal digits (0β7). Octal notation emerges because each permission triplet converts to a single digit: read=4, write=2, execute=1. Adding these values gives the octal digit for that class.
For example, rwx (all permissions) = 4+2+1 = 7. A typical permission like 755 breaks down as:
- Owner:
7(rwx β full control) - Group:
5(r-x β read and execute only) - Others:
5(r-x β read and execute only)
Permission Calculation Formula
Each permission class gets a digit based on the sum of its active permissions. The binary breakdown for any class is:
Permission Digit = (Read Γ 4) + (Write Γ 2) + (Execute Γ 1)
Chmod Octal = (Owner Digit Γ 100) + (Group Digit Γ 10) + (Others Digit)
Readβ Permission to view or list file content (value: 4)Writeβ Permission to modify or delete a file (value: 2)Executeβ Permission to run a file or enter a directory (value: 1)Owner Digitβ Sum of owner permissions (0β7)Group Digitβ Sum of group permissions (0β7)Others Digitβ Sum of others permissions (0β7)
Special Permission Modes: Setuid, Setgid, and Sticky Bit
A fourth leading digit in chmod notation (e.g., 4755 instead of 755) activates special modes. This digit itself breaks down as:
- Setuid (4xxx) β When set on an executable file, the program runs with the owner's privileges, not the executor's. Commonly used for password-changing utilities.
- Setgid (2xxx) β An executable runs with the group's privileges. On a directory, newly created files inherit the directory's group.
- Sticky Bit (1xxx) β Typically applied to shared directories like
/tmp. Only the file owner (or root) can delete files within it, preventing accidental or malicious removal of others' files.
These modes require careful administration since they bypass normal permission checks and can create security vulnerabilities if misused.
Common Chmod Mistakes and Best Practices
Avoid these frequent pitfalls when setting file permissions:
- Over-permissive defaults β Using <code>777</code> on files grants execute permission to everyone, which is rarely necessary and creates security gaps. Most files only need <code>644</code> (owner reads/writes, group and others read). Reserve execute only for actual scripts and binaries.
- Confusion with symbolic shortcuts β Symbols like <code>u+x</code> add permissions incrementally, while octal notation replaces them entirely. <code>chmod u+x file</code> preserves existing group and other permissions; <code>chmod 700 file</code> removes all group and other access. The method you choose depends on whether you're fine-tuning or resetting.
- Special bits on regular files β Setuid and setgid on non-executable files are ignored by most systems. Ensure you're applying these modes only to scripts or binaries where privilege escalation is intentional. Always test thoroughly in a non-production environment first.
- Directory vs. file execute permission β Execute permission on a directory means "can enter it," not "can run it." To list directory contents, users need both read (4) and execute (1) permissions, totaling 5. Forgetting execute permission locks users out even if they can technically read.
Common Chmod Examples in Practice
Understanding typical permission schemes helps prevent mistakes:
- 644 on files β Owner can read and write; group and others read only. Standard for documents, config files, and non-executable data.
- 755 on directories and scripts β Owner has full access; everyone else can read and execute (enter the directory or run the script) but cannot modify.
- 600 on sensitive files β Owner only. Used for SSH keys, password files, and private credentials. No group or other access whatsoever.
- 4755 on setuid binaries β The program runs as the owner (usually root), allowing unprivileged users to perform restricted tasks like changing passwords. Example:
/usr/bin/passwd. - 1777 on shared directories β Everyone can create files, but only the owner can delete theirs. Applied to
/tmpand similar shared spaces.