Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

The Three Special Bits

BitOn a fileOn a directory
SUID (4)Runs as the file's ownerNo effect on Linux
SGID (2)Runs as the file's groupNew files get its group
Sticky (1)IgnoredOnly owners may delete

In numeric chmod modes, the special bits are a fourth digit in front of the usual three: in 2775, the 2 is SGID and 775 are the normal permissions. The numbers add up, so 3 would be SGID plus sticky.

The Sticky Bit on /tmp

ls -ld /tmp

The permissions read drwxrwxrwt. Everyone may create files in /tmp, and the t at the end is the sticky bit: a file there can be deleted or renamed only by its owner, the owner of the directory, or root. Without it, any user could delete anyone else's temporary files.

A Shared Scratch Directory

Sometimes you want a directory where every local account may drop files, like /tmp but kept outside it. Create it on purpose with the same mode as /tmp:

sudo mkdir /srv/scratch
sudo chmod 1777 /srv/scratch
ls -ld /srv/scratch

The 1 is the sticky bit; 777 lets every user read, write, and enter the directory. That is what makes it a shared drop area, and also why it fits only this case: anything placed there is visible to every account on the server, so it must never hold secrets and must never sit inside a web root. Do not use this mode anywhere else; chmod 777 explains why. To add the sticky bit to an existing directory without touching its other permissions, use chmod +t.

SGID on a Team Directory

For a team, a group directory is the better tool. SGID makes every new file in it belong to the directory's group, not to the creator's personal group:

sudo groupadd developers
sudo mkdir /srv/project
sudo chown root:developers /srv/project
sudo chmod 2775 /srv/project

ls -ld /srv/project now shows drwxrwsr-x. Add the team with how to add a user to a group, and give members a umask of 002 so new files stay writable for the group. Existing files keep their old group until you change it with chgrp or chown.

SUID on Programs

ls -l /usr/bin/passwd

The owner's execute position shows s, typically as -rwsr-xr-x, and the owner is root. passwd needs this because it writes to /etc/shadow, which normal users cannot touch. sudo works the same way.

The risk is plain: a bug in a SUID root program gives whoever finds it root. That is why distributions keep the list short and why you should not add SUID to programs or scripts yourself. The kernel ignores SUID on scripts anyway. When an account needs to run one command as root, write a narrow rule in the sudoers file instead.

Reading the Bits in ls -l

  • s in the owner position: SUID is set and the owner can execute.
  • s in the group position: SGID is set and the group can execute.
  • t in the others position: the sticky bit is set and others can enter or execute.
  • Capital S or T: the special bit is set but the execute bit underneath is not. On a file this is usually a mistake.

Find SUID and SGID Files

sudo find / -xdev -type f -perm -4000 -ls
sudo find / -xdev -type f -perm -2000 -ls

Expect programs such as passwd, sudo, su, and mount. A SUID file in a home directory, in /tmp, or under /var/www is a warning sign that deserves a closer look. Check which package a file belongs to with dpkg -S on Debian and Ubuntu or rpm -qf on AlmaLinux and Rocky Linux; a file no package claims needs an explanation. Saving the list and comparing it after updates makes changes stand out. More search options are in the find command guide.

Was this answer helpful?
Loading...