07. Permissions & Umask
In Linux, file permissions dictate exactly who can read, modify, or execute a file. Misconfigured permissions are one of the most common causes of application errors (e.g., “Permission Denied” in web services or container mounts) and severe privilege escalation vulnerabilities.
07.1 The Permission Matrix
Section titled “07.1 The Permission Matrix”Every file and directory in Linux has three distinct levels of access control, known as “classes.”
The Three Classes (UGO)
Section titled “The Three Classes (UGO)”- User (u): The owner of the file.
- Group (g): The group that owns the file. Any user in this group receives these permissions.
- Others (o): Everyone else on the system.
- All (a): Shorthand combining User, Group, and Others.
The Three Permissions (RWX)
Section titled “The Three Permissions (RWX)”For each class, there are three types of permissions:
- Read (r): Allows viewing the file’s contents, or listing the contents of a directory.
- Write (w): Allows modifying the file’s contents, or adding, renaming, and deleting files within a directory.
- Execute (x): Allows running the file as a program/script, or navigating (
cd) into a directory.
When running ls -l, you see a 10-character string (e.g., -rwxr-xr--):
- The 1st character is the file type (
-for regular file,dfor directory,lfor symlink). - Characters 2-4 represent User permissions (
rwx). - Characters 5-7 represent Group permissions (
r-x). - Characters 8-10 represent Others permissions (
r--).
- r w x r - x r - - 1 alice staff 1234 Jun 30 12:00 file.txt│ └──┬┘ └──┬┘ └──┬┘ └──┬┘ └──┬┘│ │ │ │ │ └─ Group ownership│ │ │ │ └───────── User owner│ │ │ └───────────────────── Others permissions│ │ └─────────────────────────── Group permissions│ └───────────────────────────────── User (Owner) permissions└────────────────────────────────────── File type (- = file, d = directory)07.2 Ownership & Permission Management Commands
Section titled “07.2 Ownership & Permission Management Commands”Linux provides three core commands to manage file access: chmod, chgrp, and chown.
Command Comparison
Section titled “Command Comparison”| Command | Primary Purpose | Modifies | Typical Example | Key Options |
|---|---|---|---|---|
chmod |
Change file/directory access mode | Permissions (rwx) |
chmod 755 app.sh |
-R, numeric, symbolic |
chgrp |
Change group ownership | Group | chgrp staff file.txt |
-R, -v, -f, -h, --reference |
chown |
Change user and/or group owner | Owner, Group | chown alice:staff app.py |
-R, -h, -v, -f |
chown (Change Owner & Group)
Section titled “chown (Change Owner & Group)”Only the root user can change file ownership. File owners can only reassign group ownership to a group they personally belong to.
# Change owner to 'alice'sudo chown alice file.txt
# Change owner to 'alice' AND group to 'staff' simultaneouslysudo chown alice:staff file.txt
# Change only the group (prefix with colon)sudo chown :developers file.txt
# Recursively change owner and group across an entire directory treesudo chown -R root:admin /var/www
# Change ownership of a symbolic link itself (-h), without dereferencing targetsudo chown -h bob:users /path/to/symlinkchgrp (Change Group Ownership)
Section titled “chgrp (Change Group Ownership)”The chgrp utility modifies group ownership specifically:
# Change group of file.txt to 'staff'sudo chgrp staff file.txt
# Recursively change group for a directory treesudo chgrp -R dev /srv/project
# Set group of target file to match a reference filesudo chgrp --reference=ref.txt file.txt07.3 Changing Permissions (chmod)
Section titled “07.3 Changing Permissions (chmod)”The chmod (Change Mode) command modifies permissions using either Symbolic Mode or Numeric (Octal) Mode.
1. Symbolic Mode
Section titled “1. Symbolic Mode”Symbolic mode uses letters and operators to add (+), remove (-), or explicitly set (=) permissions:
- Classes:
u(user),g(group),o(others),a(all) - Operators:
+(add),-(remove),=(set exact) - Permissions:
r(read),w(write),x(execute)
# Add execute permission for the file ownerchmod u+x file.txt
# Remove write permission for groupchmod g-w file.txt
# Set read-only for otherschmod o=r file.txt
# Add read, write, and execute for both user and groupchmod ug+rwx file.txt
# Give all users execute permissionschmod a+x run_app.sh2. Numeric (Octal) Mode
Section titled “2. Numeric (Octal) Mode”Numeric mode assigns binary-derived decimal values to each permission:
- Read (
r) = 4 - Write (
w) = 2 - Execute (
x) = 1 - None (
-) = 0
Calculate the sum for each class (Owner, Group, Others):
rwx= $4 + 2 + 1 = 7$rw-= $4 + 2 + 0 = 6$r-x= $4 + 0 + 1 = 5$r--= $4 + 0 + 0 = 4$
# 755: User(rwx=7), Group(r-x=5), Others(r-x=5) - Standard for executables/directorieschmod 755 script.sh
# 644: User(rw-=6), Group(r--=4), Others(r--=4) - Standard for documents/config fileschmod 644 config.yaml
# 600: User(rw-=6), Group(---=0), Others(---=0) - Sensitive files (SSH keys, secrets)chmod 600 id_rsa
# 777: Full access for everyone - DANGER: Avoid in production!chmod 777 /shared/folder07.4 Special Permissions: SUID, SGID & Sticky Bit
Section titled “07.4 Special Permissions: SUID, SGID & Sticky Bit”Beyond standard rwx bits, Linux filesystems support three special permission flags that alter execution context and directory deletion rules.
[Special Flag] [User] [Group] [Others] 4 7 5 5| Special Bit | Octal Value | Symbol | Target | Effect |
|---|---|---|---|---|
| SUID (Set User ID) | 4 |
s (User execute) |
Files | Runs process with the privileges of the file owner, regardless of who invokes it |
| SGID (Set Group ID) | 2 |
s (Group execute) |
Files & Directories | Files: Runs with group owner privileges. Directories: Newly created files inherit the directory’s group |
| Sticky Bit | 1 |
t (Others execute) |
Directories | Only the file owner, directory owner, or root can rename or delete files |
SUID (Set User ID)
Section titled “SUID (Set User ID)”- Visual indicator:
sin the user execute field (e.g.,-rwsr-xr-x). If execute (x) is missing, it appears as an uppercaseS. - Primary Example:
/usr/bin/passwdrequires root access to update/etc/shadow, but ordinary users must run it.
# Add SUID to a binarysudo chmod u+s /usr/local/bin/backup-tool# or octal:sudo chmod 4755 /usr/local/bin/backup-tool
# Remove SUIDsudo chmod u-s /usr/local/bin/backup-toolSGID (Set Group ID)
Section titled “SGID (Set Group ID)”- On Directories: Essential for collaborative team folders. When a directory has SGID, all new files created inside inherit the parent directory’s group owner, not the author’s primary group.
- Visual indicator:
sin the group execute field (e.g.,drwxrwsr-x).
# Set SGID on a collaborative project directorysudo chmod g+s /srv/project# or octal:sudo chmod 2770 /srv/project
# Remove SGIDsudo chmod g-s /srv/projectSticky Bit
Section titled “Sticky Bit”- On Directories: Prevents users from deleting or renaming files owned by other users in world-writable directories (e.g.,
/tmp). - Visual indicator:
tin the others execute field (e.g.,drwxrwxrwt).
# Set sticky bitsudo chmod +t /shared/scratch# or octal:sudo chmod 1777 /shared/scratchFinding Files with Special Permissions
Section titled “Finding Files with Special Permissions”Regular audits should detect unauthorized SUID/SGID binaries:
# Find all SUID binaries on the systemsudo find / -perm /4000 -type f 2>/dev/null
# Find all SGID filessudo find / -perm /2000 -type f 2>/dev/null
# Find files with either SUID or SGID setsudo find / -perm /6000 -type f 2>/dev/null07.5 Default Permissions & Umask
Section titled “07.5 Default Permissions & Umask”The umask (User File-Creation Mode Mask) controls the default permissions assigned to newly created files and directories. Rather than granting permissions, the umask acts as a filter that masks (subtracts) permissions from system base maximums.
System Base Permissions
Section titled “System Base Permissions”- Base Files:
666(rw-rw-rw-) — Files are never created with execute permissions by default for safety. - Base Directories:
777(rwxrwxrwx) — Directories require execute permissions to be traversed.
Calculation Method
Section titled “Calculation Method”New File Permission = Base File (666) - umaskNew Directory Permission = Base Directory (777) - umaskCommon Umask Presets
Section titled “Common Umask Presets”| Umask | File Mode (666 - Umask) | Directory Mode (777 - Umask) | Security Profile |
|---|---|---|---|
022 |
644 (rw-r--r--) |
755 (rwxr-xr-x) |
Standard server default. Owner full, others read. |
027 |
640 (rw-r-----) |
750 (rwxr-x---) |
Hardened enterprise. Group reads, others blocked. |
077 |
600 (rw-------) |
700 (rwx------) |
High security. Owner only, group & others completely blocked. |
Inspecting and Setting Umask
Section titled “Inspecting and Setting Umask”# View current umask in octalumask# Output: 0022
# View current umask symbolicallyumask -S# Output: u=rwx,g=rx,o=rx
# Temporarily change umask for current sessionumask 027Setting Permanent Umask
Section titled “Setting Permanent Umask”- Per-User: Add
umask 027to~/.bashrcor~/.profile. - System-Wide: Configure in
/etc/profileor/etc/login.defs(UMASK 027).
Key Use Cases
Section titled “Key Use Cases”- Enforce strict default isolation on shared multi-tenant hosts.
- Prevent accidental creation of world-writable or world-readable application files.
- Guarantee consistent security baselines across automated scripts and CI/CD runner jobs.