chmod 4755 (rwsr-xr-x)
A 755 program with the setuid bit: whoever starts it, the process runs with the effective user ID of the file's owner. On a file owned by root, that is a deliberate door to root privileges.
This tool runs in your browser. We never send what you type or paste to a server.
What chmod 4755 sets
| Who | Digit | Read | Write | Execute | Symbolic |
|---|---|---|---|---|---|
| Owner (u) | 7 | yes | yes | yes | rws |
| Group (g) | 5 | yes | no | yes | r-x |
| Others (o) | 5 | yes | no | yes | r-x |
The leading 4 sets setuid, shown as s in place of an x.
chmod 4755 file # numeric
chmod u=rwxs,go=rx file # the same, in symbolic form
ls -l shows this mode as -rwsr-xr-x on a file and drwsr-xr-x on a directory. On a file, execute means it can be run as a program. On a directory, read lists the names inside, write creates, renames and deletes entries, and execute (search) is what lets you enter it and reach the files inside.
What the setuid bit does
Normally a program runs as the user who started it. With setuid set, the kernel starts it with the effective user ID of the file's owner. That is how an ordinary user gets one specific privileged job done: sudo is installed set-user-ID root (its documentation gives chown root and chmod 4755 as the repair when the bit goes missing), and su and passwd are traditionally set up the same way. The program itself decides what the caller is allowed to do.
The owner is half of the mode. 4755 on a file owned by ana makes it run as ana, not as root. Only root can produce a setuid-root file, and the order matters: chown first, then chmod, because changing the owner clears the bit.
Why it is dangerous
Everything a setuid-root program can be tricked into doing is done as root. A mishandled argument, environment variable or temporary file, harmless in an ordinary tool, becomes a way for any local user to take over the machine. A setuid-root copy of a shell is the classic backdoor for the same reason.
- Never set it on a shell, an interpreter, an editor or a general-purpose tool such as
cporfind. - Never set it on a program that was not written to be setuid.
- Prefer something narrower: a
sudorule for one command, or on Linux a file capability that grants a single privilege, such assetcap cap_net_bind_service=ep /usr/local/bin/serverfor binding to ports below 1024. - To limit who may use a setuid program, drop the last digit:
4750lets only the file's group run it.
When the bit is ignored or disappears
- Scripts. Linux ignores setuid and setgid on anything started through a
#!line.lsshows thes, and the script still runs as you. nosuidmounts. On a file system mounted with this option the bit has no effect.mountlists the options in use.- Traced or restricted processes. It is not honoured while the caller is being traced with ptrace, or has the
no_new_privsflag set. - Writing or re-owning the file. The kernel clears setuid and setgid when an unprivileged process writes to the file and when its owner or group is changed.
- A plain numeric chmod.
chmod 755 fileremoves it from a regular file; so doeschmod u-s file.
The dynamic linker also treats setuid programs differently: it ignores LD_LIBRARY_PATH and restricts LD_PRELOAD, so the caller cannot inject a library.
Finding setuid files
find / -xdev -type f -perm -4000 2>/dev/null # setuid files on the root file system
find / -xdev -type f -perm -4000 -user root -exec ls -l {} +
find / -xdev -type f -perm /6000 2>/dev/null # setuid or setgid (GNU find)
stat -c '%a %U %n' /usr/bin/sudo # GNU/Linux: mode with the special digit, owner
stat -f '%Mp%Lp %N' /usr/bin/sudo # macOS and BSD (%Lp alone leaves the 4 out)
chmod u-s /path/to/file # remove the bit
-xdev keeps find on one file system; repeat the command for other mount points. Anything in the list that did not come from a package deserves an explanation.
A capital S, and directories
rwSr-xr-x with a capital S means setuid is set but the owner's execute bit is not, as in mode 4655. The bit does nothing in that state; it is usually the trace of a mistaken chmod.
On a directory, Linux gives setuid no meaning. A few other systems use it to make new files take the directory's owner. The bit that matters on directories is setgid: see 2775.
4755 compared with its neighbours
Questions
Why does my setuid shell script not run as root?
Because Linux, like most modern Unix systems, ignores the setuid and setgid bits on scripts. The bit only takes effect on compiled executables. Use a sudo rule that allows the specific script instead, and make sure the script is not writable by the users who are allowed to run it.
What does a capital S mean in ls -l?
The setuid bit (in the owner position) or the setgid bit (in the group position) is set, but the execute bit under it is not. A lowercase s means both are set. On a program, a capital S usually points to a mistake, because setuid without execute permission has no effect.
How do I find and remove setuid files?
List them with find / -xdev -type f -perm -4000, run as root so that no directory is skipped. Remove the bit from a file with chmod u-s. Do not strip it from system programs such as sudo, su or passwd: they stop working without it.