A team directory
Check: alice's file gets group devs and group write access (with a 002 mask),
and bob cannot delete it:
Special permission bits: setuid and setgid run a program as its owner; setgid and sticky shape shared directories.
ls -ld /tmp /usr/bin/passwd
t on /tmp, s on passwd
sudo chmod g+s /srv/team
setgid: new files get its group
sudo chmod +t /srv/team
sticky: no deleting others' files
sudo chmod 3770 /srv/team
both bits plus rwx for the group
stat -c '%a %A %n' /srv/team
3770 drwxrws--T /srv/team
sudo chmod g-s,-t /srv/team
clear both bits
find / -perm -4000 -type f 2>/dev/null
every setuid program
Besides rwx for the owner, group and others, a file has three more bits:
setuid, setgid and sticky. In numeric
mode they are the first of four digits: 4 is setuid, 2 is setgid, 1 is
sticky. chmod 2775 = setgid + rwxrwxr-x.
| Bit | On a program | On a directory | In ls -l |
|---|---|---|---|
| setuid (4) | runs with the file owner's rights | — | s for the owner |
| setgid (2) | runs with the file's group | new files get the directory's group | s for the group |
| sticky (1) | — | only the owner can delete a file | t for others |
A capital letter means the bit is set but there is no x under it: S is setgid
without g+x, T is sticky without o+x. For a directory, drwxrws--T is a normal
picture: others cannot enter, and sticky works for the group members.
A new file gets the primary group of whoever created it. In a
shared directory that breaks teamwork: alice's file lands in group alice, and her
colleagues cannot edit it. setgid on the directory changes the rule — the file gets
the directory's group, and subdirectories inherit both the group and the bit itself.
w on a directory allows deleting any file in it, even someone else's. sticky
leaves deleting and renaming to the file's owner, the directory's owner and root. This
is how /tmp works:
Group write access on new files comes from the creator's umask, not from the
directory: shared work needs a mask of 002.
Check: alice's file gets group devs and group write access (with a 002 mask),
and bob cannot delete it:
setgid works on new files. Old files and files moved in with mv keep their group
— fix those by hand.
A clean system has a dozen or so: passwd, sudo, su, mount and the like. A new
line in the list is a reason to find out who set the bit and why.
The directory has no setgid (ls -ld shows x rather than s for the group) — run
chmod g+s. If the bit is there, the file was not created but moved in with mv: it
kept its old group, so fix it with chgrp.
The directory has no sticky bit, and the group has w. Run chmod +t.
The file is someone else's, and the directory has the sticky bit (t or T at the end
of ls -ld). That is by design: only the file's owner, the directory's owner or root
can delete it.
GNU chmod with a three- or four-digit number does not clear setuid and setgid on
directories. Clear them with letters, chmod g-s dir, or with an extra leading zero:
chmod 00775.
Only the file's owner and root can change its permissions. Check the owner in ls -l
and use sudo if it is your machine.
The Linux kernel ignores setuid on #! scripts — it applies only to programs. To run a
command as root, configure sudo.
A root-owned setuid program that can read or write arbitrary files or run commands
hands root to any user. Do not set u+s on your own programs or on copies of system
ones, and keep the find -perm -4000 list under control.
chmod with letters and numbers, chgrp.usermod -aG.-perm.Found an inaccuracy? Report an error in the article