SUID, SGID & the sticky bit
Build a shared group directory and explain each special permission bit.
By Invicly Tech, , in Linux Administration (RHCSA-style). About 11 minutes to read.
In short
Three extra permission bits change how files and directories behave. SUID runs a program with its owner's privileges, SGID on a directory makes new files inherit the directory's group, and the sticky bit stops users from deleting each other's files in a shared directory. SGID plus a group is the standard answer for team collaboration directories.
Explained simply
Besides read, write and execute, Linux has three extra permission bits. Each one solves a specific problem that normal permissions cannot, and each one shows up as a letter in place of an x in ls -l.
SUID (set user ID) is for programs. When you run a SUID program, it runs with the rights of the file's owner, not your own. This is how a normal user can change their own password: the passwd program is owned by root and has SUID, so it is allowed to update the protected password file.
SGID (set group ID) is mostly used on directories. Every new file created inside such a directory gets the directory's group, instead of the creator's own group. That keeps a team folder's files in the team group automatically.
The sticky bit is for shared directories like /tmp, where everyone can create files. With it, people can only delete or rename their own files, not other people's. Together, these bits are powerful, so admins check regularly that no unexpected programs have SUID.
Press Play on a diagram to walk through it one step at a time.
- On executables
- Runs as the file's owner
- Example: /usr/bin/passwd
- Shown as s in the user x slot
- On directories: new files get its group
- On executables: runs as its group
- Use for shared team folders
- Shown as s in the group x slot
- On directories
- Only owners can delete their files
- Example: /tmp is 1777
- Shown as t in the other x slot
- 1alice creates plan.txt
Her primary group is alice
- 2Kernel sees SGID on the directory
Group comes from /srv/team
- 3File group is team
Not alice's own group
- 4umask sets the mode
002 gives rw-rw-r--, 022 gives rw-r--r--
- 5bob in team opens it
Group bits decide what he may do
In depth
Real and effective user IDs
Every process carries two user IDs. The real UID says who started it. The effective UID is what the kernel checks for permissions. Normally both are the same. When you run a program file that has SUID, the kernel sets the effective UID to the file's owner at the moment the program starts, while the real UID stays yours.
That is how passwd, owned by root, can write /etc/shadow while still knowing which user called it, so it only lets you change your own password. A well-written SUID program checks the real UID and drops extra rights as soon as possible.
Linux ignores SUID on scripts (files starting with #!) because they are too easy to abuse. File systems mounted with the nosuid option ignore the bit completely, which is common for /tmp, USB disks and network shares.
- alice's shell to kernel: exec /usr/bin/passwd1exec /usr/bin/passwd
- kernel to passwd (SUID root): Start with real UID alice, effective UID root2Start with real UID alice, effective UID root
- 3Ask for and check alice's old passwordpasswd (SUID root) to passwd (SUID root): Ask for and check alice's old password
- passwd (SUID root) to /etc/shadow: Write new hash for alice only4Write new hash for alice only
- passwd (SUID root) to alice's shell: Exit, root rights end with the process5Exit, root rights end with the process
SGID directories: what is and is not inherited
On a directory with SGID, new files take the directory's group, and new subdirectories take the group and the SGID bit too, so the rule spreads down the tree. Files that already existed keep their old group; fix them once with chgrp -R team dir and find dir -type d -exec chmod g+s {} +.
SGID controls the group, not the permissions. If a user's umask is 022, their new files are rw-r--r--, so teammates can read them but not edit them. For a real shared workspace, also set umask 002 for team members or add a default ACL such as setfacl -d -m g:team:rwX dir, which gives group write to new files regardless of the umask.
On an executable file, SGID runs the program with the file's group as its effective group, a rarer use for programs that need access to one group-owned resource.
The sticky bit and its kernel helpers
In a directory with the sticky bit, a file can only be deleted or renamed by its owner, the directory's owner or root. It does not change who may write to a file; that still follows the file's own mode. /tmp and /var/tmp are 1777: everyone can create files, nobody can remove anyone else's.
Shared world-writable directories have a classic attack: an attacker creates a symlink in /tmp with a name a root program is about to use, so root writes somewhere else. The kernel settings fs.protected_symlinks and fs.protected_hardlinks block this, and fs.protected_regular limits opening other users' files there. Modern distributions enable them by default; check with sysctl fs.protected_symlinks, which should print 1.
- chmod +t dir or chmod 1777 dir sets the sticky bit
- A capital T means sticky is set but others have no x
- Use mktemp for temporary files in scripts, never fixed names in /tmp
Octal modes, capital letters and a GNU chmod quirk
The special bits form an optional fourth digit in front: 4 for SUID, 2 for SGID, 1 for sticky. chmod 4755 file gives rwsr-xr-x and chmod 3770 dir gives SGID and sticky with rwxrwx---. A lowercase s or t means the bit and execute are both set. A capital S or T means the special bit is set but execute is not, which is almost always a mistake on a file.
One surprise from GNU chmod: on directories, a plain numeric mode like chmod 755 dir does not clear SUID or SGID. That protects shared folders from accidental changes. To remove SGID from a directory, use chmod g-s dir, or a five-digit mode such as chmod 00755 dir. On regular files, chmod 0755 clears the special bits as you would expect.
- ls -l: rwsr-xr-x means SUID plus execute
- rwSr--r-- means SUID set but no execute: suspicious
- stat -c '%a' shows the four-digit mode, for example 2770
Auditing SUID and SGID files
Any SUID root program with a bug is a way to become root, so admins keep the list short and known. find / -xdev -type f -perm -4000 lists SUID files and -perm /6000 lists files with SUID or SGID. Save the list once on a clean system and compare it later with diff; a new entry in a home directory or /tmp is a serious warning sign.
On RHEL-family systems, rpm -qf file tells you which package owns a binary, and rpm -V package reports files whose mode, owner or content changed from what the package shipped. On Ubuntu, dpkg -S file does the first job.
Many programs that once needed SUID now use file capabilities instead, which grant one narrow power rather than full root. getcap -r / 2>/dev/null lists them.
Learn it in 4 short steps
About 30m each. Track them with a timer in your plan.
- 1Explain SUID on executables~30m
- 2Use SGID for shared group directories~30m
- 3Protect shared dirs with the sticky bit~30m
- 4Audit SUID files with find~30m
Key points
- chmod 2770 /srv/team gives an SGID shared directory for one group.
- A capital S or T means the bit is set but execute is not.
- Unexpected SUID root binaries are a classic privilege escalation path.
- The special bits are the leading digit in a four-digit octal mode.
Key terms
Tap a card to flip it. 0/4 known
Examples
sudo groupadd team
sudo mkdir -p /srv/team && sudo chgrp team /srv/team
sudo chmod 2770 /srv/team # SGID + rwx for owner and group
ls -ld /srv/team /tmp # drwxrws--- and drwxrwxrwt
sudo find / -perm -4000 -type f 2>/dev/null # audit SUID filesBuild a collaboration directory and audit SUID binaries.
Build a collaborative directory with SGID and sticky
Team devs needs /srv/devs where everyone in the group can create and edit files, but nobody can delete a colleague's file. Step 1: create the group and the directory owned by it. Step 2: set mode 3770: SGID (2) so files stay in devs, sticky (1) so only owners delete, rwx for owner and group. Step 3: add a default ACL so new files are group-writable whatever each user's umask is. Step 4: test as two different members.
sudo groupadd -f devs
sudo usermod -aG devs alice; sudo usermod -aG devs bob
sudo mkdir -p /srv/devs
sudo chown root:devs /srv/devs
sudo chmod 3770 /srv/devs
sudo setfacl -d -m g:devs:rwX /srv/devs
ls -ld /srv/devs # drwxrws--T+ root devs
sudo -u alice touch /srv/devs/a.txt
sudo -u bob rm /srv/devs/a.txt # Operation not permitted
sudo -u bob sh -c 'echo hi >> /srv/devs/a.txt' && echo bob can editSpot a planted SUID shell
After an incident you want to know whether anyone left a SUID copy of a shell behind. Step 1: list all SUID and SGID files on local file systems. Step 2: compare with a baseline saved when the server was clean. Step 3: for anything new, check if a package owns it. A file that no package owns, sitting in /tmp or a home directory with SUID root, should be removed and investigated.
sudo find / -xdev -type f -perm /6000 2>/dev/null | sort > /root/suid-now.txt
diff /root/suid-baseline.txt /root/suid-now.txt
# > /var/tmp/.cache/bash
ls -l /var/tmp/.cache/bash # -rwsr-xr-x root root
rpm -qf /var/tmp/.cache/bash # not owned by any package
sudo chmod u-s /var/tmp/.cache/bashQuick reference
- Fourth octal digit: 4 = SUID, 2 = SGID, 1 = sticky; chmod 2770 dir is a typical team folder
- find / -xdev -type f -perm /6000 lists SUID or SGID files on local file systems
- Capital S or T in ls -l means the special bit is set but execute is not
- chmod 755 dir keeps an existing SGID bit; use chmod g-s dir to remove it
- Linux ignores SUID on #! scripts; mount with nosuid to ignore it entirely
- rpm -V PACKAGE reports files whose mode or owner changed from the package
- getcap -r / 2>/dev/null lists file capabilities, a narrower alternative to SUID
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Show the SUID bit on /usr/bin/passwd and the sticky bit on /tmp, and print both modes as numbers.
- Exercise 2medium
Convert an existing project directory /srv/proj and everything under it so that all files belong to group proj and every subdirectory has SGID.
- Exercise 3hard
Create a baseline of SUID and SGID files, then write a small check that prints any new ones and exits with status 1 if there are any, so it can run from a timer.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Is SUID dangerous?
- It is safe for well-reviewed system programs like passwd and sudo. It is dangerous on anything else, because a bug in a SUID root program gives an attacker root.
- What does the t at the end of drwxrwxrwt mean?
- It is the sticky bit with execute for others. In that directory, users can only delete or rename their own files.
- Does SGID on a directory make files group-writable?
- No. It only sets the group. Whether the group can write depends on the umask or on a default ACL.
- Why does my SUID shell script not run as root?
- Linux ignores the SUID bit on interpreted scripts for security reasons. Use a sudo rule for that script instead.