Skip to content

Linux Foundations / Manage a Linux host ยท Step 11 of 50

Lesson: SetuidSwitch
โ† Linux Foundations

System administration

Setuid

Understand the setuid bit, which runs a program with the identity of the file's owner.

Normally a program runs with the permissions of the person who starts it. The setuid (set user ID) bit changes that: the program runs with the user identity of the file's owner.

A real example

Your password hash lives in /etc/shadow, which ordinary users cannot write. Yet you can change your own password. Look at the program that does it:

$ ls -l /usr/bin/passwd
-rwsr-xr-x 1 root root 118168 Apr 19  2025 /usr/bin/passwd

The s in the owner's execute position is the setuid bit. Because the owner is root, passwd runs with root privileges whoever launches it. The program itself checks that you only change what you are allowed to.

Setting and reading the bit

$ chmod u+s mytool
$ ls -l mytool
-rwsr-xr-x 1 maya maya 16000 Mar  6 12:00 mytool

In numeric form, setuid is the value 4 in an extra leading digit, so chmod 4755 mytool does the same here.

A lowercase s means setuid and owner execute are both set. A capital S means setuid is set but the owner execute bit is missing, which is usually a mistake.

Limits and risks

  • Linux ignores setuid on scripts that start with #!. It works on binary programs.
  • A filesystem mounted with the nosuid option ignores it too.

A bug in a setuid root program can hand an attacker full control of the system. Do not add the bit to editors, shells, interpreters or your own experiments on a shared machine. When a user needs to run one command as root, a narrow sudo rule is usually a better choice.

It is good practice to know which setuid programs your system has:

$ find /usr/bin -perm -4000 -type f
/usr/bin/passwd
/usr/bin/su
/usr/bin/mount

The exact list varies between distributions. The -4000 test matches files that have the setuid bit set, whatever their other bits are.

Try it

$ ls -l /usr/bin/passwd /usr/bin/su
$ find /usr/bin -perm -4000 -type f -exec ls -l {} +

For each result, note the owner and think about why an ordinary user would need that program to have extra rights.

Check your understanding

Setuid

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.