setuid, setgid and Sticky Bit Explained

In standard Linux systems administration, the vast majority of operations involve three-digit permissions such as chmod 755 or chmod 644. However, the Unix permission architecture incorporates an advanced fourth dimension: special permission bits. By prefixing a fourth digit to standard octal modes—creating configurations like chmod 4755, chmod 2755, chmod 2775, or chmod 1777—administrators can enable privileged command execution, enforce automated group inheritance across shared folders, and protect multi-user scratch directories from accidental or malicious file deletion.

Advertisement

The 4-Digit Mode

When you execute a three-digit chmod command, the leading special digit is implicitly assumed to be 0 (e.g., 755 is interpreted by the kernel as 0755). The explicit leading digit represents a bitmask of three special attributes:

  • setuid (Set User ID = 4): Represented numerically by adding 4 to the first digit. In symbolic listings, an s (or S) replaces the owner execute bit.
  • setgid (Set Group ID = 2): Represented numerically by adding 2 to the first digit. In symbolic listings, an s (or S) replaces the group execute bit.
  • Sticky Bit (1): Represented numerically by adding 1 to the first digit. In symbolic listings, a t (or T) replaces the others execute bit.

Just like standard read, write, and execute bits, these values can be combined. For example, combining setuid (4) and setgid (2) produces chmod 6755. Adding the sticky bit (1) to world-writable permissions yields chmod 1777.

setuid

When an executable binary possesses the setuid bit (mode 4xxx), it executes with the effective privileges of the file owner rather than the user who launched it:

# Inspect the passwd binary on Ubuntu
$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 68208 Jan 15 10:00 /usr/bin/passwd

# Notice the 's' in the owner execute position!
# This corresponds to chmod 4755.

Consider what happens when a normal user updates their password. Modifying an account password requires writing the new hashed password into /etc/shadow, a file strictly owned by root with chmod 600 or chmod 640 permissions. Without setuid, an ordinary user could never write to /etc/shadow. Because /usr/bin/passwd is configured as chmod 4755 and owned by root, the kernel temporarily elevates the process to root privileges solely for the duration of the command, allowing the password update to proceed safely.

setgid on Files and Directories

The setgid bit (mode 2xxx) behaves differently depending on whether it is applied to an executable file or a directory:

  • On an executable file: The binary executes with the effective group ID of the file group, enabling controlled access to group-restricted resources like mail spools or audio hardware.
  • On a directory: Any new file or subfolder created inside the directory automatically inherits the group ownership of the parent directory, rather than the primary group of the user who created it.
# Create a shared team project directory with setgid
sudo mkdir /var/www/team_project
sudo chown -R deployer:developers /var/www/team_project
sudo chmod 2775 /var/www/team_project

# Verify directory permissions
ls -ld /var/www/team_project
drwxrwsr-x 2 deployer developers 4096 Jan 15 10:05 /var/www/team_project

By enforcing chmod 2775, any developer belonging to the developers group can create files inside the folder, and those files will immediately belong to developers. Without setgid, newly created files would inherit each individual developer personal primary group, locking out colleagues and breaking collaborative workflows.

Sticky Bit

The sticky bit (mode 1xxx) is designed specifically for public, shared directories where multiple independent users must create temporary files without interfering with one another:

# Inspect the system /tmp directory
$ ls -ld /tmp
drwxrwxrwt 20 root root 4096 Jan 15 10:10 /tmp

# The trailing 't' signifies chmod 1777!

In a standard directory with world-write permissions (chmod 777), any user on the system can delete or rename any file inside, regardless of who created it. In a sticky directory (chmod 1777), Linux enforces an essential security constraint: a user can only delete or rename files that they personally own (or if they possess root privileges). This prevents malicious users or buggy scripts from wiping out other processes socket files and temporary caches in /tmp.

Finding Special-Permission Files

Because special permissions alter the normal security boundary of user processes, security auditors routinely scan file systems to identify unauthorized setuid and setgid binaries:

# Find all setuid files on the system
sudo find / -perm -4000 -type f -exec ls -ld {} + 2>/dev/null

# Find all setgid files
sudo find / -perm -2000 -type f -exec ls -ld {} + 2>/dev/null

# Find all files with either setuid OR setgid
sudo find / -perm /6000 -type f -exec ls -ld {} + 2>/dev/null

Regularly reviewing this list ensures that malicious actors who gain temporary foothold on your server have not dropped a custom setuid root binary (e.g., a copy of /bin/bash with chmod 4755) to establish permanent privilege escalation backdoors.

Security Considerations

While special permissions are indispensable for core system functionality, they introduce substantial security surface area when misused:

  • Never apply setuid to custom scripts: Most modern Linux kernels intentionally ignore the setuid bit on interpreted scripts (like Bash or Python) because race conditions in shebang parsing allow attackers to hijack execution environments.
  • Avoid setuid on complex binaries: Setting setuid on large, third-party software packages or interpreters like vim or python immediately allows any local user to spawn a root shell.
  • Use Linux Capabilities when possible: Modern Linux systems prefer fine-grained capabilities (like setcap cap_net_raw+ep /usr/bin/ping) over blanket setuid root execution, minimizing the blast radius of software vulnerabilities.

Special permissions are powerful tools in your administrative arsenal. Used deliberately in configurations like chmod 2775 for team collaboration and chmod 1777 for shared directories, they provide robust, automated system governance.

Advertisement

Frequently Asked Questions

What does the fourth digit mean in a 4-digit chmod command?

The leading digit in a 4-digit chmod mode represents special permission flags: 4 for setuid, 2 for setgid, and 1 for the sticky bit. These values sum together just like standard permissions.

Why does /tmp have a 't' at the end of its permissions?

The 't' indicates the sticky bit (chmod 1777). In a sticky directory, any user can create files, but only the file owner or root can delete or rename those files, preventing users from tampering with each other's data.

Why does Linux ignore setuid on shell scripts?

Most modern Linux kernels intentionally ignore setuid on interpreted shell scripts because race conditions between shebang parsing and interpreter execution allow trivial local privilege escalation attacks.

Advertisement