$_ CLI In Depth
Learning progress 0%
lesson permissions users sudo

setuid, setgid & the Sticky Bit

5 min read Module 5: Users, Permissions & sudo Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:27

Three bits above the nine

The mode has room for three more bits above the nine permission bits. In octal they are written as a leading digit. The value 4 is setuid, 2 is setgid, and 1 is the sticky bit. A mode such as 4755 combines setuid with rwxr-xr-x. In ls -l the bits appear in place of an execute letter, as an s for setuid or setgid and as a t for the sticky bit. A capital S or T means the bit is set but the execute bit is not, which is usually a mistake.

Setuid on executable files

When a program file has the setuid bit set, the kernel gives the new process an effective UID equal to the owner of the file, instead of the UID of the user who started it. The real UID stays the same, so the program knows who started it. This is how an ordinary user can run passwd, which must write to a file that only root may change. The program runs as root for the duration of its work.

The effective UID is the identity the kernel uses for permission checks. For a setuid program, that identity is the owner of the file. For every other program, the effective UID equals the real UID. The setgid bit does the same job for the group. It sets the effective GID of the new process to the group of the file.

Setuid is powerful, and it is also a common source of security bugs. A setuid root program that trusts its input or its environment can be tricked into doing things its author never intended. That is why the set of setuid files on a system is kept small, and why you should never set the bit on a script that you wrote yourself.

Setgid on directories

On a directory, the setgid bit has a different effect. New files and subdirectories created inside it take the group of the directory, instead of the primary group of the creator. This is useful for a shared project, because every file lands in the project group without anyone having to run chgrp. New subdirectories also inherit the setgid bit, so the rule continues down the tree.

BASH3 lines
mkdir shared
chmod 2775 shared
ls -ld shared

The mode 2775 gives the directory setgid plus full access for the owner and group, and read and execute for others. The ls -ld line shows an s in the group execute position. The lab in this unit checks the sticky bit, and a later unit covers the setgid bit on directories in more depth.

The sticky bit on directories

The sticky bit on a directory restricts deletion. A directory that is writable by everyone, such as /tmp, would otherwise let any user remove any file in it. With the sticky bit set, a user may remove or rename an entry only if the user owns the entry, owns the directory, or is root. The mode of /tmp is 1777, which is the sticky bit on top of full access for everyone.

BASH3 lines
mkdir drop
chmod 1777 drop
stat -c "%A %a %n" drop

The output shows drwxrwxrwt and the octal value 1777. The t in the last position marks the sticky bit. Setting the bit on a directory you own is a safe experiment, because it only changes who may delete entries inside it.

Note

The sticky bit has a different meaning on regular files on some older systems. On Linux it has no effect on regular files, and you should treat it as a directory-only bit.

Finding special bits with find

The find command can search by mode. The expression -perm -4000 matches files that have every bit in 4000 set, which means setuid. The expression -perm -2000 matches setgid files. Using a minus sign means at least these bits, rather than exactly these bits.

BASH1 lines
find /usr/bin -perm -4000 -type f 2>/dev/null | head

The output varies from one system to another, and in the lab it depends on which programs are installed. The point of the search is to know which programs elevate their privilege, and to review them when you audit a machine. A short list is normal, and a long unexpected list deserves a closer look.

The sticky bit combines with the other bits in a predictable way. A directory with mode 1777 accepts new entries from anyone, and it protects the entries that others create. A directory with mode 1755 lets only its owner create entries, so the sticky bit adds nothing there. The bit by itself never grants new access. It only narrows who may delete or rename entries.

Setuid and setgid should be audited, not assumed. A program with the setuid bit that appears on a system without a deliberate install is worth reviewing. The find command in this unit gives you the list, and stat gives you the owner, so the two together show what a program is and who it runs as.

Copying a setuid program with cp does not keep the bit unless you ask for it with the preserve option. That default is a protection, because it stops an ordinary copy from silently creating a new privileged program. The bit has to be set on purpose, which is the behavior you want for any mechanism that grants elevated rights.

Hands-on: set and find the bits

Set the sticky bit on a directory you own, then search a directory of your own for files with setuid and setgid bits. The labs for this unit ask you to build exactly that setup, using files you create, and to record the list that find prints. The checks compare the recorded output with the modes you set, so you can see how find matches the bit.

How the kernel decides who may access a file

Visualizer

The kernel picks one class (owner, group or other) and uses only that class’s bits. Directories read r, w and x differently, and the sticky bit limits who can delete.

1 / 4
-type
-owner
-
-
rgroup
w
x
-other
-
-
process
uid 1000, gid 1000
file
owner 1000, group 1000
mode
070

The kernel knows the process’s uid and gid, and the file’s owner and group. It picks exactly one class for the check and tries them in order: owner, group, other.

cd "$(mktemp -d /tmp/perm.XXXX)" && touch own.txt && chmod 070 own.txt && cat own.txt; echo "status $?"; stat -c "%a %A" own.txt
Terminal lab · 3 steps · real Linux in your browser

setuid, setgid & the Sticky Bit

Set the sticky bit on a directory, then use find to list the setuid and setgid files in a small tree you control.

01
Set the sticky bit on a directory
The directory /workspace/drop currently has mode 777. Add the sticky bit to it, so the directory ends with mode 1777.
Show hint for step 1
A symbolic mode with +t adds the sticky bit and leaves the other bits alone.
02
Find the setuid file
The directory /workspace/bin holds three files, and only one of them has the setuid bit. Use find with -perm -4000 and save the result, one path per line, in /workspace/suid.txt.
Show hint for step 2
Redirect the output of find to suid.txt, and keep the path that find prints unchanged.
03
Find the setgid file
Use the same tree in /workspace/bin and find the file with the setgid bit. Save the path in /workspace/sgid.txt.
Show hint for step 3
The setgid value is 2000, and the test is the same shape as the setuid search.
Check yourself Question 1 of 4
Concept Check Setuid digit recall

Which octal digit sets the setuid bit in a mode such as 4755?

Recap & Key Takeaways

The setuid bit, value 4, makes a program run with the effective UID of the file owner, and the setgid bit, value 2, does the same for the group.
On a directory, setgid makes new entries inherit the directory group, and the sticky bit, value 1, lets only the owner of an entry, the owner of the directory, or root delete it.
The mode 1777 on /tmp combines the sticky bit with write access for everyone.
Use find with -perm -4000 or -perm -2000 to list setuid and setgid files, and review the list when you audit a system.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue