SUID, SGID and the Sticky Bit
The three special permission bits: how SUID lets passwd change /etc/shadow, why a SUID copy of the wrong program hands out root, SGID programs and SGID team folders, the sticky bit on /tmp, the four-digit modes 4755, 2775 and 1777, capital S and T, and how to audit a server for them.
Beyond rwx
Normal permissions answer "who may read, write or run this?". Three extra bits change how things behave:
- SUID: a program runs as its owner, not as you.
- SGID: a program runs with its group; on a directory, new files inherit the directory's group.
- Sticky bit: in a shared directory, you can only delete your own files.
They're rare, but they're behind everyday things (changing your password, sudo, /tmp), behind every well-run team folder, and behind a classic way attackers turn a normal account into root. You'll see all three in every security audit.
Where the special bits appear
They take over the x positions of ls -l. As numbers they form a fourth digit in front: 4755, 2775, 1777.
SUID: run as the file's owner
Every process has an identity, normally inherited from whoever started it. When a program file has the SUID bit, the kernel starts it with the identity of the file's owner instead.
The textbook example is changing your password:
ls -l /usr/bin/passwd /etc/shadow
su - anita -c 'cat /etc/shadow'-rw-r----- 1 root shadow 605 Nov 24 15:57 /etc/shadow
-rwsr-xr-x 1 root root 64152 Apr 9 2024 /usr/bin/passwd
cat: /etc/shadow: Permission deniedAnita can't even read /etc/shadow, yet she can change her password. passwd is owned by root and has s in the owner's execute position, so whoever runs it, it runs as root and can write the file. Its code makes sure it only ever changes the caller's own entry.
What SUID changes
Other SUID programs you'll find on Ubuntu: sudo and su (they need root to switch users), mount/umount (users may mount allowed devices), chsh and chfn (change your own shell or name), newgrp and gpasswd.
Why SUID is dangerous
A SUID-root program is only safe if it's written to do exactly one careful thing. Make an ordinary tool SUID root and it does its ordinary thing as root, for anyone:
-rwsr-xr-x 1 root root 39384 Nov 24 15:57 /usr/local/bin/logview
root:*:20781:0:99999:7:::
daemon:*:20781:0:99999:7:::logview is just a copy of cat that someone made SUID root "so the team can read logs". Here a normal user runs logview /etc/shadow and gets the password hashes. A SUID vim, find, python3 or bash is worse: it's a root shell. Attackers who get a foothold on a server look for exactly this, and the public GTFOBins list catalogues which programs give root away when SUID.
Linux ignores SUID on shell scripts (it would be too easy to abuse), and on filesystems mounted with nosuid. Containers run with no-new-privileges ignore it as well. If you need to give someone one privileged action, use sudo for that exact command, never SUID.
Audit SUID programs
List every SUID program on the system and spot the one that shouldn't be there.
Normally a program runs with the identity of whoever started it. A SUID program runs as the file's owner instead. That's how a normal user can change their own password: passwd must write /etc/shadow, which only root can, so passwd is SUID root.
SUID root is powerful and dangerous: any bug in such a program, or any SUID program that can read or run arbitrary things, hands out root access. Security audits always start by listing them.
1. A legitimate SUID program
ls -l /usr/bin/passwd /etc/shadow
su - anita -c 'cat /etc/shadow'-rw-r----- 1 root shadow 605 Nov 24 15:57 /etc/shadow
-rwsr-xr-x 1 root root 64152 Apr 9 2024 /usr/bin/passwd
cat: /etc/shadow: Permission deniedThe s in rws is SUID. Anita can't read /etc/shadow with cat, but when she runs passwd, it runs as root and can update her hash. passwd is careful to only ever change your own entry.
2. List every SUID and SGID program
find / -xdev -type f -perm -4000 2>/dev/null| Part | Meaning |
|---|---|
-xdev | stay on this filesystem (skip /proc and mounted disks) |
-perm -4000 | the SUID bit is set (-2000 for SGID, /6000 for either) |
2>/dev/null | hide "Permission denied" noise |
/usr/lib/openssh/ssh-keysign
/usr/local/bin/logview
/usr/bin/gpasswd
/usr/bin/newgrp
/usr/bin/chfn
/usr/bin/umount
/usr/bin/mount
/usr/bin/passwd
/usr/bin/chsh
/usr/bin/su
/usr/bin/sudoEvery entry in /usr/bin comes from an Ubuntu package and has a job: su and sudo switch users, mount lets users mount allowed disks, chsh changes your own shell. One stands out: /usr/local/bin/logview. /usr/local is where admins put their own software, and nothing installed by a package lives there.
On a real server, compare the list with the packages: dpkg -S /usr/bin/passwd names the package a file came from, and dpkg -S /usr/local/bin/logview finds nothing. Any SUID file no package owns deserves a close look.
Save the list of all SUID files on the root filesystem to /root/suid-files.txt, exactly as find prints them.
Expected result: the file lists /usr/bin/passwd, /usr/bin/sudo and /usr/local/bin/logview, among others.
Hint: the commands, explained
find / -xdev -type f -perm -4000 2>/dev/null > /root/suid-files.txtWhat each part does
find / -xdev: search the whole system but stay on this one filesystem (skip/procand other mounts).-type f -perm -4000: files with the SUID bit (4000) set. These run with their owner's rights, often root.2>/dev/nullhides "Permission denied" noise.>saves the list.
Remove a dangerous SUID
Stop the rogue log viewer from running as root.
logview is a copy of cat owned by root with SUID set. cat prints any file you name, so a SUID-root cat prints any file on the system for any user. This is a real and common privilege escalation: lists like GTFOBins catalogue which programs give away root when made SUID.
1. Prove the problem
ls -l /usr/local/bin/logview
su - anita -c '/usr/local/bin/logview /etc/shadow | head -2'-rwsr-xr-x 1 root root 39384 Nov 24 15:57 /usr/local/bin/logview
root:*:20781:0:99999:7:::
daemon:*:20781:0:99999:7:::Anita, a normal user, just read the password hash file. On a real server those hashes can be cracked offline.
2. Remove the SUID bit
chmod u-s /usr/local/bin/logview
ls -l /usr/local/bin/logview
su - anita -c '/usr/local/bin/logview /etc/shadow | head -2'u-s removes the special bit from the owner position. The program still runs, but as the user who starts it.
-rwxr-xr-x 1 root root 39384 Nov 24 15:57 /usr/local/bin/logview
/usr/local/bin/logview: /etc/shadow: Permission deniedThe s became a plain x, and Anita gets "Permission denied" like with ordinary cat.
The right way to let someone read logs is a group, not SUID: put them in the adm group (which can read most of /var/log), or grant one exact command with sudo. Both are in this module.
Remove the SUID bit from /usr/local/bin/logview, leaving it executable.
Expected result: su - anita -c '/usr/local/bin/logview /etc/shadow' is refused, and find / -xdev -perm -4000 no longer lists it.
Hint: the commands, explained
chmod u-s /usr/local/bin/logviewWhat each part does
chmod u-s FILE: remove the SUID bit. The program now runs with the rights of whoever starts it, not its owner's (root).ls -lshowsrwxinstead ofrwsin the owner part.
SGID on programs
SGID on a program works like SUID but for the group: it runs with the file's group. It's used for programs that need one specific group's access, not full root:
/usr/sbin/pam_extrausers_chkpwd
/usr/sbin/unix_chkpwd
/usr/bin/expiry
/usr/bin/chage
/usr/bin/crontab
/usr/bin/ssh-agentThat's find / -xdev -type f -perm -2000 on Ubuntu. For example, crontab runs with group crontab so it can write your file in /var/spool/cron/crontabs, which normal users can't touch directly, and unix_chkpwd runs with group shadow so it can read (not write) password hashes to check a password.
SGID on directories: team folders
This is the use you'll actually set up yourself. Normally, a new file gets its creator's primary group. In a team folder, that leaves every file in a different private group:
-rw-r--r-- 1 priya priya 19 Nov 24 15:57 release-notes.txt
-rw-r--r-- 1 rohan rohan 0 Nov 24 15:57 rohan-1.txtBoth were created in /srv/shared, which belongs to group devops.
Set SGID on the directory and new entries take the directory's group:
chmod g+s /srv/shared
su - rohan -c 'touch /srv/shared/rohan-2.txt; mkdir /srv/shared/plans'
ls -l /srv/sharedtotal 8
drwxr-sr-x 2 rohan devops 4096 Nov 24 15:57 plans
-rw-r--r-- 1 priya priya 19 Nov 24 15:57 release-notes.txt
-rw-r--r-- 1 rohan rohan 0 Nov 24 15:57 rohan-1.txt
-rw-r--r-- 1 rohan devops 0 Nov 24 15:57 rohan-2.txt- New file
rohan-2.txt: group devops. - New folder
plans: group devops and it inherited the SGID bit (r-s), so the rule carries down the whole tree. - Files that already existed keep their old group; fix them once with
chgrp -R devops /srv/shared.
The complete recipe for a folder a team can share:
chgrp -R devops /srv/shared # the team's group owns everything
chmod 2775 /srv/shared # SGID + rwxrwxr-x
umask 002 # in each member's ~/.profile: new files rw-rw-r--SGID on a shared folder
Make every file created in /srv/shared belong to the devops group.
A new file normally gets the creator's primary group. In a team folder that's wrong: Rohan's files get group rohan, Priya's get priya, and teammates end up as "others". The SGID bit on a directory changes the rule: new files and folders inside it get the directory's group, and new subfolders inherit the SGID bit too.
1. See the problem
ls -ld /srv/shared
su - rohan -c 'touch /srv/shared/rohan-1.txt'
ls -l /srv/shareddrwxrwxr-x 2 root devops 4096 Nov 24 15:57 /srv/shared
total 4
-rw-r--r-- 1 priya priya 19 Nov 24 15:57 release-notes.txt
-rw-r--r-- 1 rohan rohan 0 Nov 24 15:57 rohan-1.txtThe folder belongs to devops, but each file got its creator's own group.
2. Set SGID on the folder
chmod g+s /srv/shared
ls -ld /srv/shared
su - rohan -c 'touch /srv/shared/rohan-2.txt; mkdir /srv/shared/plans'
ls -l /srv/shareddrwxrwsr-x 2 root devops 4096 Nov 24 15:57 /srv/shared
total 8
drwxr-sr-x 2 rohan devops 4096 Nov 24 15:57 plans
-rw-r--r-- 1 priya priya 19 Nov 24 15:57 release-notes.txt
-rw-r--r-- 1 rohan rohan 0 Nov 24 15:57 rohan-1.txt
-rw-r--r-- 1 rohan devops 0 Nov 24 15:57 rohan-2.txt- The folder shows
rwsin the group position: SGID. rohan-2.txtandplansgot group devops automatically, andplansinherited thes.- Older files keep their groups: SGID only affects new files. Fix those once with
chgrp -R devops /srv/shared.
As a number, SGID is 2000, so a team folder is typically 2775 (or 2770):
2775 drwxrwsr-x /srv/sharedThat's stat -c "%a %A %n" /srv/shared. Combine SGID with a 002 umask (previous lesson) and new files are both in the team's group and group-writable.
- Set the SGID bit on
/srv/shared. - Give the files already in it the group devops.
Expected result: ls -ld /srv/shared shows drwxrwsr-x, every file inside is in group devops, and a new file Rohan creates there gets group devops.
Hint: the commands, explained
chmod g+s /srv/shared
chgrp -R devops /srv/sharedWhat each part does
chmod g+s /srv/shared: SGID on a folder. New files inside get the folder's group instead of their creator's main group.chgrp -R devops /srv/shared: set the group of the folder and everything already in it to devops.ls -ldshowsrwsin the group part.
The sticky bit
Remember that deleting a file is a change to its directory. So in any folder you can write to, you can delete every file, even ones you can't read. For a folder like /tmp that everyone writes to, that would let any user delete everyone else's files.
The sticky bit on a directory adds a rule: a file can only be deleted or renamed by its owner, the directory's owner, or root.
ls -ld /srv/dropbox /tmp
chmod +t /srv/dropbox
su - anita -c 'rm /srv/dropbox/priya-report.txt'drwxrwxrwx 2 root root 4096 Nov 24 15:57 /srv/dropbox
drwxrwxrwt 1 root root 4096 Nov 24 15:57 /tmp
rm: cannot remove '/srv/dropbox/priya-report.txt': Operation not permitted/tmp ends in t. After chmod +t, the drop folder does too, and Anita can no longer delete Priya's file, though she can still create and delete her own. (On files, the sticky bit is ignored on modern Linux.)
The sticky bit
Let everyone use /srv/dropbox, but only delete their own files.
Deleting a file is a change to the directory, so anyone with w on a directory can delete any file in it, even files they can't read. In a folder everyone can write to, that means anyone can delete everyone's files. The sticky bit on a directory adds one rule: only a file's owner (or the directory's owner, or root) can delete or rename it. /tmp has it for exactly this reason.
1. The problem
ls -ld /srv/dropbox /tmp
su - anita -c 'rm -f /srv/dropbox/priya-report.txt' && echo deleteddrwxrwxrwx 2 root root 4096 Nov 24 15:57 /srv/dropbox
drwxrwxrwt 1 root root 4096 Nov 24 15:57 /tmp
deletedAnita just deleted Priya's report. Notice the difference between the two folders: /tmp ends in t.
Put Priya's report back before you continue (running as priya, so she owns it again):
su - priya -c 'echo "priya report" > /srv/dropbox/priya-report.txt'2. Set the sticky bit
chmod +t /srv/dropbox
ls -ld /srv/dropbox
su - anita -c 'rm /srv/dropbox/priya-report.txt'
su - anita -c 'echo hi > /srv/dropbox/anita.txt; rm /srv/dropbox/anita.txt' && echo okdrwxrwxrwt 2 root root 4096 Nov 24 15:57 /srv/dropbox
rm: cannot remove '/srv/dropbox/priya-report.txt': Operation not permitted
okAnita can still create and delete her own files, but not Priya's. As a number, sticky is 1000, so the folder is now 1777, the same as /tmp.
Set the sticky bit on /srv/dropbox, keeping it writable by everyone.
Expected result: ls -ld /srv/dropbox shows drwxrwxrwt, anita can't delete Priya's file, and can still create her own.
Hint: the commands, explained
chmod +t /srv/dropboxor, as a number: chmod 1777 /srv/dropbox
What each part does
chmod +t /srv/dropbox: set the sticky bit on the folder. Everyone can still create files, but each person can delete or rename only their own.ls -ldshows atat the end:drwxrwxrwt, the same as/tmp.
Numbers and letters
The special bits form a fourth digit at the front of the mode, built the same way as the others:
| Digit | Bits |
|---|---|
4 | SUID |
2 | SGID |
1 | sticky |
chmod 1777 /tmp
chmod 2775 /srv/shared
chmod 4755 /usr/local/bin/logview
stat -c "%a %A %n" /tmp /srv/shared /usr/local/bin/logview1777 drwxrwxrwt /tmp
2775 drwxrwsr-x /srv/shared
4755 -rwsr-xr-x /usr/local/bin/logviewSymbolically: u+s (SUID), g+s (SGID), +t or o+t (sticky), and - to remove them.
Lowercase or capital? The special letter replaces the x, so ls needs a way to show whether x is also set:
| Shown | Means |
|---|---|
s / t | special bit and execute |
S / T | special bit without execute |
-rwSr--r-- 1 root root 0 Nov 24 15:57 xThat's a plain file after chmod u+s. The capital S says SUID is set but nobody can execute it, so it does nothing. Capital letters are almost always a mistake.
Auditing a server
find / -xdev -type f -perm -4000 2>/dev/null # SUID files
find / -xdev -type f -perm -2000 2>/dev/null # SGID files
find / -xdev -type f -perm /6000 -ls 2>/dev/null # either, with details
dpkg -S /usr/bin/passwd # which package installed it?| Form | Matches |
|---|---|
-perm -4000 | at least SUID |
-perm /6000 | SUID or SGID |
-ls | print ls -l style details |
/usr/lib/openssh/ssh-keysign
/usr/local/bin/logview
/usr/bin/gpasswd
/usr/bin/newgrp
/usr/bin/chfn
/usr/bin/umount
/usr/bin/mount
/usr/bin/passwd
/usr/bin/chsh
/usr/bin/su
/usr/bin/sudoEverything under /usr/bin and /usr/lib came from Ubuntu packages. /usr/local/bin/logview didn't: /usr/local is for software admins install themselves, and dpkg -S finds no package for it. That's the one to investigate. Hardening guides recommend saving this list and alerting when it changes.
Making a tool SUID to "fix" a permission problem. A SUID cat, less, find, vim or interpreter gives every user root's reach. Use a group or a specific sudo rule.
Expecting SGID to fix existing files. It only affects files created afterwards. Run chgrp -R once.
chmod 777 on a shared folder and forgetting the sticky bit. Anyone can delete anyone's work. A public folder should be 1777; a team folder 2770 or 2775.
Copying with cp and losing the bits. A normal cp drops SUID/SGID on the copy. cp -a or rsync -a keep modes (and need root to keep the owner).
"What is SUID? Give an example." The program runs as its file's owner. passwd is SUID root so users can update /etc/shadow.
"What does SGID do on a directory?" New files and subdirectories get the directory's group, and subdirectories inherit SGID. Used for team folders.
"What's the sticky bit and where is it used?" On a directory, only a file's owner (or the directory owner, or root) can delete or rename it. /tmp is 1777.
"How would you find SUID binaries, and why?" find / -xdev -perm -4000 -type f 2>/dev/null. An unexpected SUID root binary is a common privilege escalation path.
"Does SUID work on a bash script?" No, Linux ignores it on interpreted scripts for security.
Summary
- SUID
4000,sin the owner'sx: runs as the file's owner. Essential forpasswd,sudo,su; dangerous on anything else. - SGID
2000,sin the group'sx: programs run with the file's group; directories hand their group to new files (team folders:2775/2770). - Sticky
1000,tin others'x: only owners delete files in a shared directory (/tmpis1777). chmod u+s,g+s,+tor four-digit modes. CapitalS/T= special bit withoutx.- Audit with
find / -xdev -type f -perm -4000/-2000, and check origins withdpkg -S.
Quick quiz
You've practised everything in this lesson
Every task was checked on your own machine. You can end the lab, or keep experimenting in the terminal.