Learn / Linux / Users & Permissions
Lesson 20 of 74

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.

20 min read Report a problem Hands-on lab included 6-question quiz

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

SUID · 4000s in the owner's x
-rwsr-xr-x/usr/bin/passwd
SGID · 2000s in the group's x
drwxrwsr-x/srv/shared
Sticky · 1000t in others' x
drwxrwxrwt/tmp

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:

bash
ls -l /usr/bin/passwd /etc/shadow
su - anita -c 'cat /etc/shadow'
text
-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 denied

Anita 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

anita runs catprocess runs as anita
open /etc/shadow
Permission deniedanita is "others"
anita runs passwd (SUID root)process runs as root
open /etc/shadow
Allowedpasswd only updates anita's line

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:

text
-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.

Try it · Task 1 of 4

Audit SUID programs

List every SUID program on the system and spot the one that shouldn't be there.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -l /usr/bin/passwd /etc/shadow
su - anita -c 'cat /etc/shadow'
text
-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 denied

The 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

bash
find / -xdev -type f -perm -4000 2>/dev/null
PartMeaning
-xdevstay on this filesystem (skip /proc and mounted disks)
-perm -4000the SUID bit is set (-2000 for SGID, /6000 for either)
2>/dev/nullhide "Permission denied" noise
text
/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/sudo

Every 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.

2 · Your task

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
text
find / -xdev -type f -perm -4000 2>/dev/null > /root/suid-files.txt

What each part does

  • find / -xdev: search the whole system but stay on this one filesystem (skip /proc and other mounts).
  • -type f -perm -4000: files with the SUID bit (4000) set. These run with their owner's rights, often root.
  • 2>/dev/null hides "Permission denied" noise. > saves the list.
First start the lab machine in the terminal panel on the right.
Try it · Task 2 of 4

Remove a dangerous SUID

Stop the rogue log viewer from running as root.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -l /usr/local/bin/logview
su - anita -c '/usr/local/bin/logview /etc/shadow | head -2'
text
-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

bash
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.

text
-rwxr-xr-x 1 root root 39384 Nov 24 15:57 /usr/local/bin/logview
/usr/local/bin/logview: /etc/shadow: Permission denied

The 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.

2 · Your task

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
text
chmod u-s /usr/local/bin/logview

What 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 -l shows rwx instead of rws in the owner part.
First start the lab machine in the terminal panel on the right.

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:

text
/usr/sbin/pam_extrausers_chkpwd
/usr/sbin/unix_chkpwd
/usr/bin/expiry
/usr/bin/chage
/usr/bin/crontab
/usr/bin/ssh-agent

That'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:

text
-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

Both were created in /srv/shared, which belongs to group devops.

Set SGID on the directory and new entries take the directory's group:

bash
chmod g+s /srv/shared
su - rohan -c 'touch /srv/shared/rohan-2.txt; mkdir /srv/shared/plans'
ls -l /srv/shared
text
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
  • 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:

text
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--
Try it · Task 3 of 4

SGID on a shared folder

Make every file created in /srv/shared belong to the devops group.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -ld /srv/shared
su - rohan -c 'touch /srv/shared/rohan-1.txt'
ls -l /srv/shared
text
drwxrwxr-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.txt

The folder belongs to devops, but each file got its creator's own group.

2. Set SGID on the folder

bash
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/shared
text
drwxrwsr-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 rws in the group position: SGID.
  • rohan-2.txt and plans got group devops automatically, and plans inherited the s.
  • 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):

text
2775 drwxrwsr-x /srv/shared

That'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.

2 · Your task
  1. Set the SGID bit on /srv/shared.
  2. 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
text
chmod g+s /srv/shared
chgrp -R devops /srv/shared

What 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 -ld shows rws in the group part.
First start the lab machine in the terminal panel on the right.

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.

bash
ls -ld /srv/dropbox /tmp
chmod +t /srv/dropbox
su - anita -c 'rm /srv/dropbox/priya-report.txt'
text
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.)

Try it · Task 4 of 4

The sticky bit

Let everyone use /srv/dropbox, but only delete their own files.

1 · See how it's done Read this, then run the commands in your terminal (or press ▶ Run)

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

bash
ls -ld /srv/dropbox /tmp
su - anita -c 'rm -f /srv/dropbox/priya-report.txt' && echo deleted
text
drwxrwxrwx 2 root root 4096 Nov 24 15:57 /srv/dropbox
drwxrwxrwt 1 root root 4096 Nov 24 15:57 /tmp
deleted

Anita 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):

bash
su - priya -c 'echo "priya report" > /srv/dropbox/priya-report.txt'

2. Set the sticky bit

bash
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 ok
text
drwxrwxrwt 2 root root 4096 Nov 24 15:57 /srv/dropbox
rm: cannot remove '/srv/dropbox/priya-report.txt': Operation not permitted
ok

Anita 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.

2 · Your task

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
text
chmod +t /srv/dropbox

or, 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 -ld shows a t at the end: drwxrwxrwt, the same as /tmp.
First start the lab machine in the terminal panel on the right.

Numbers and letters

The special bits form a fourth digit at the front of the mode, built the same way as the others:

DigitBits
4SUID
2SGID
1sticky
bash
chmod 1777 /tmp
chmod 2775 /srv/shared
chmod 4755 /usr/local/bin/logview
stat -c "%a %A %n" /tmp /srv/shared /usr/local/bin/logview
text
1777 drwxrwxrwt /tmp
2775 drwxrwsr-x /srv/shared
4755 -rwsr-xr-x /usr/local/bin/logview

Symbolically: 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:

ShownMeans
s / tspecial bit and execute
S / Tspecial bit without execute
text
-rwSr--r-- 1 root root 0 Nov 24 15:57 x

That'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

bash
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?
FormMatches
-perm -4000at least SUID
-perm /6000SUID or SGID
-lsprint ls -l style details
text
/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/sudo

Everything 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, s in the owner's x: runs as the file's owner. Essential for passwd, sudo, su; dangerous on anything else.
  • SGID 2000, s in the group's x: programs run with the file's group; directories hand their group to new files (team folders: 2775/2770).
  • Sticky 1000, t in others' x: only owners delete files in a shared directory (/tmp is 1777).
  • chmod u+s, g+s, +t or four-digit modes. Capital S/T = special bit without x.
  • Audit with find / -xdev -type f -perm -4000 / -2000, and check origins with dpkg -S.
Check your understanding

Quick quiz

Feedback