December 5, 2026

Setuid, Setgid and the Sticky Bit: The Fourth Digit in Linux File Permissions

A brass master key among ordinary keys on a key ring

Run ls -l /usr/bin/passwd on most Linux systems and the owner’s execute slot shows an s instead of an x. That one letter is why an ordinary user can change a password stored in a file only root can write. It belongs to the least understood part of Linux file permissions, the three special bits that live in a fourth octal digit in front of the familiar 755 and 644. The behavior below was tested on Ubuntu with GNU coreutils 9.4, using two test accounts, alice and bob, and a shared group called team.

Video: Shawn Powers on YouTube

The fourth digit

Three extra bits

The chmod manual page describes a numeric mode as up to four octal digits, with omitted digits treated as leading zeros. The first of the four selects the special attributes:

Value Bit On a program On a directory
4 setuid Runs with the file owner’s user ID Usually ignored on Linux
2 setgid Runs with the file’s group ID New files take the directory’s group
1 sticky Ignored on modern Linux Only owners may delete or rename

The digits add like the other three. chmod 4755 is setuid plus rwxr-xr-x. chmod 2775 is setgid plus rwxrwxr-x. chmod 1777 is sticky plus full access for everyone, the classic mode of /tmp.

How ls shows them

The bits borrow the execute positions in ls -l output. Setuid shows in the owner’s execute slot, setgid in the group’s, sticky in the “other” slot:

$ stat -c '%a %A %n' /usr/bin/passwd /tmp
4755 -rwsr-xr-x /usr/bin/passwd
1777 drwxrwxrwt /tmp

A lowercase s or t means the special bit and the execute bit are both set. An uppercase S or T means the special bit is set but execute is not. Removing execute from the setuid test binary turned -rwsr-xr-x into -rwSr-xr-x, and a directory set to 1770 showed drwxrwx--T. The capital letter is usually a sign of a mistake, since a setuid file that can’t be executed has nothing to do.

Silver electronic gate

Setuid: running as the file’s owner

What it does

The execve manual page explains the mechanism: when a program with the set-user-ID bit runs, the process’s effective user ID changes to the owner of the program file. The real user ID stays the same, so the program knows who launched it and can decide what to allow.

Seeing it work

The test copied /usr/bin/id into a working folder, left it owned by root, and set it to 4755. Running it as bob:

$ runuser -u bob -- ./myid
uid=30037(bob) gid=30037(bob) euid=0(root) groups=30037(bob),30038(team)

Bob’s real user ID stayed 30037. His effective ID became 0, root. That is precisely how passwd works: it starts with root’s power, checks which account the real user is allowed to change, and writes only that entry.

Which programs have it

A search of the test system’s program folders listed the setuid files:

$ find /usr/bin /usr/sbin -perm -4000 -type f
/usr/bin/fusermount3
/usr/bin/sudo
/usr/bin/chfn
/usr/bin/chsh
/usr/bin/gpasswd
/usr/bin/mount
/usr/bin/newgrp
/usr/bin/passwd
/usr/bin/su
/usr/bin/umount
/usr/sbin/mount.nfs

Every one of them exists to let a normal user do a narrow, privileged job: change a password, switch users, mount a disk. Each is also a place where a bug could hand out root, which is why security audits run that same find command and question anything unexpected on the list.

The safety rules built into the kernel

Scripts are ignored

A shell script given mode 4755 and owned by root did not run as root:

$ runuser -u bob -- ./s.sh
30037

The execve page states the rule directly: Linux, like most modern Unix systems, ignores the set-user-ID and set-group-ID bits on scripts. Scripts pass through an interpreter, and the gap between the kernel reading the script’s first line and the interpreter opening the file was historically exploitable. The kernel refuses the whole idea.

Changing ownership clears the bit

After chown bob on a setuid copy of id, its mode dropped from 4755 to 755. The chown manual page says changing the owner or group of an executable clears both bits, and that since Linux 2.2.13 root is treated the same as other users. Without that rule, handing a setuid file to a new owner would silently hand that owner’s privileges to anyone who runs it.

Mount options switch it off

The execve page also notes that the set-ID change is skipped when the file sits on a filesystem mounted with nosuid. Removable drives, network shares and temporary filesystems are often mounted that way, so a setuid program copied onto a USB stick gains nothing.

A glass door cabinet with a bunch of folders on top of it

Setgid on directories: shared folders that stay shared

The problem it solves

On a shared project folder, each new file normally takes the creator’s primary group. Alice’s files end up in group alice, bob’s in group bob, and teammates lose access to each other’s work.

The inode manual page describes the fix. With setgid on a directory, files created there inherit the directory’s group instead of the creator’s, and new subdirectories get the setgid bit too.

The test

$ chgrp team proj && chmod 2775 proj
$ stat -c '%a %A %G' proj
2775 drwxrwsr-x team

Alice, whose primary group is alice, then created a file and a folder inside:

-rw-r--r-- alice team proj/a.txt
drwxr-sr-x alice team proj/sub

Both went to group team, and the new subfolder carried the s forward, so the rule holds all the way down the tree. A shared folder usually pairs this with a umask of 002, so files are group-writable as well as group-owned. The default umask on many systems, 022, left a.txt readable but not writable by the group.

Setgid on programs

On an executable, setgid works like setuid with groups: the program runs with the file’s group. The inode page adds an odd historical case. On a file without group execute permission, the setgid bit once meant mandatory file locking instead.

The sticky bit: shared folders where nobody deletes anyone else’s work

What it does

The inode page defines it for directories: a file inside can be renamed or deleted only by the file’s owner, the directory’s owner, or a privileged process. That is what makes a world-writable directory like /tmp usable. Everyone can create files there, but nobody can delete anyone else’s.

The test

A folder was set to 1777. Alice created a file and made it writable by everyone (mode 666). Bob could add text to it, because the file’s own permissions allowed writing. He could not delete it:

bob-append-ok
rm: cannot remove '/tmp/p4/shared/alice.txt': Operation not permitted

Then the sticky bit was removed with chmod 777, and the same rm as bob succeeded. Deleting a file is a change to the directory, not to the file, so write permission on the folder normally lets anyone remove anything in it. The sticky bit is the one exception.

On regular files

The chmod page notes that on some older systems the bit on a program file kept its code in swap for faster loading, which is where the name came from. Modern Linux ignores it on files.

Silver and black combination padlock

The chmod 755 trap on directories

chmod doesn’t clear what it isn’t told to

A test directory was set to 2775, then chmod 755:

$ chmod 2775 g && chmod 755 g && stat -c '%a %A' g
2755 drwxr-sr-x

The setgid bit survived. chmod 0755 kept it too. Only chmod 00755, with five digits, cleared it. The chmod manual page says that for directories, chmod preserves the setuid and setgid bits unless explicitly told otherwise. The GNU coreutils manual, in its section on directories and the set-user-ID and set-group-ID bits (installed with coreutils 9.4 and readable with info coreutils), explains the reasoning: if routine chmod commands wiped these bits, shared folders would break every time someone tidied permissions. It lists the ways to clear them deliberately: chmod g-s, chmod -6000, chmod =755, or a mode with five or more octal digits such as 00755. The manual flags this as a GNU extension that portable scripts should not rely on.

Why this matters in practice

The same behavior caught the test itself. After running chmod 0775 on the shared project folder to “turn off” setgid, alice’s next file still landed in group team, because the bit had never left. A script that resets permissions with plain numbers will leave setgid directories setgid, which is usually what an administrator wants, and occasionally the opposite.

Commands worth keeping

  • chmod u+s file / chmod u-s file: set or clear setuid by name.
  • chmod g+s dir: make a shared folder hand its group to new files.
  • chmod +t dir: protect files in a shared folder from deletion by others.
  • find / -xdev -perm -4000 -type f: list setuid programs on one filesystem.
  • find / -xdev -perm -2000 -type f: list setgid programs.

Three bits, one digit. Two of them grant power and one takes it away, and the safest of the three is the one sitting on /tmp on every Linux machine.

Sources

  • chmod(1), Linux manual page: https://man7.org/linux/man-pages/man1/chmod.1.html
  • inode(7), Linux manual page: https://man7.org/linux/man-pages/man7/inode.7.html
  • execve(2), Linux manual page: https://man7.org/linux/man-pages/man2/execve.2.html
  • chown(2), Linux manual page: https://man7.org/linux/man-pages/man2/chown.2.html
  • GNU coreutils manual, section 27.5, “Directories and the Set-User-ID and Set-Group-ID Bits” (read from the coreutils 9.4 info manual installed with the tools; the gnu.org copy could not be reached at the time of writing, so no link is given)

765 Posts

Peter Kalafatis writes and edits X9M8. He holds a bachelor's degree from LIU Post and a master's in Media Studies from The New School, with academic work in the philosophy of science, human behavior, AI, and memetics.

View All Posts

Leave a Reply

Your email address will not be published. Required fields are marked *