Skip to content

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.


Every file and directory in Linux has three distinct levels of access control, known as “classes.”

  1. User (u): The owner of the file.
  2. Group (g): The group that owns the file. Any user in this group receives these permissions.
  3. Others (o): Everyone else on the system.
  4. All (a): Shorthand combining User, Group, and Others.

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, d for directory, l for 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 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

Only the root user can change file ownership. File owners can only reassign group ownership to a group they personally belong to.

Terminal window
# Change owner to 'alice'
sudo chown alice file.txt
# Change owner to 'alice' AND group to 'staff' simultaneously
sudo 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 tree
sudo chown -R root:admin /var/www
# Change ownership of a symbolic link itself (-h), without dereferencing target
sudo chown -h bob:users /path/to/symlink

The chgrp utility modifies group ownership specifically:

Terminal window
# Change group of file.txt to 'staff'
sudo chgrp staff file.txt
# Recursively change group for a directory tree
sudo chgrp -R dev /srv/project
# Set group of target file to match a reference file
sudo chgrp --reference=ref.txt file.txt

The chmod (Change Mode) command modifies permissions using either Symbolic Mode or Numeric (Octal) 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)
Terminal window
# Add execute permission for the file owner
chmod u+x file.txt
# Remove write permission for group
chmod g-w file.txt
# Set read-only for others
chmod o=r file.txt
# Add read, write, and execute for both user and group
chmod ug+rwx file.txt
# Give all users execute permissions
chmod a+x run_app.sh

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$
Terminal window
# 755: User(rwx=7), Group(r-x=5), Others(r-x=5) - Standard for executables/directories
chmod 755 script.sh
# 644: User(rw-=6), Group(r--=4), Others(r--=4) - Standard for documents/config files
chmod 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/folder

07.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
  • Visual indicator: s in the user execute field (e.g., -rwsr-xr-x). If execute (x) is missing, it appears as an uppercase S.
  • Primary Example: /usr/bin/passwd requires root access to update /etc/shadow, but ordinary users must run it.
Terminal window
# Add SUID to a binary
sudo chmod u+s /usr/local/bin/backup-tool
# or octal:
sudo chmod 4755 /usr/local/bin/backup-tool
# Remove SUID
sudo chmod u-s /usr/local/bin/backup-tool
  • 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: s in the group execute field (e.g., drwxrwsr-x).
Terminal window
# Set SGID on a collaborative project directory
sudo chmod g+s /srv/project
# or octal:
sudo chmod 2770 /srv/project
# Remove SGID
sudo chmod g-s /srv/project
  • On Directories: Prevents users from deleting or renaming files owned by other users in world-writable directories (e.g., /tmp).
  • Visual indicator: t in the others execute field (e.g., drwxrwxrwt).
Terminal window
# Set sticky bit
sudo chmod +t /shared/scratch
# or octal:
sudo chmod 1777 /shared/scratch

Regular audits should detect unauthorized SUID/SGID binaries:

Terminal window
# Find all SUID binaries on the system
sudo find / -perm /4000 -type f 2>/dev/null
# Find all SGID files
sudo find / -perm /2000 -type f 2>/dev/null
# Find files with either SUID or SGID set
sudo find / -perm /6000 -type f 2>/dev/null

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.

  • 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.
New File Permission = Base File (666) - umask
New Directory Permission = Base Directory (777) - umask
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.
Terminal window
# View current umask in octal
umask
# Output: 0022
# View current umask symbolically
umask -S
# Output: u=rwx,g=rx,o=rx
# Temporarily change umask for current session
umask 027
  • Per-User: Add umask 027 to ~/.bashrc or ~/.profile.
  • System-Wide: Configure in /etc/profile or /etc/login.defs (UMASK 027).
  1. Enforce strict default isolation on shared multi-tenant hosts.
  2. Prevent accidental creation of world-writable or world-readable application files.
  3. Guarantee consistent security baselines across automated scripts and CI/CD runner jobs.