Special permissions and ACLs
setgid, sticky bit, setuid, umask and ACLs: what they do and when you need them.
Special bits
Three extra bits on top of rwx. They show up in place of the x in ls -l.
| Bit | Octal | On a directory | On a file | Shows as |
|---|---|---|---|---|
| setgid | 2000 | New files get the directory’s group (not the creator’s) | Runs with the file’s group | rwxrws--- |
| sticky | 1000 | Only the owner of a file may delete it | (no effect) | rwxrwxrwt |
| setuid | 4000 | (no effect) | Runs as the file owner (e.g. passwd runs as root) | rwsr-xr-x |
sudo chmod g+s /srv/shared # setgid on a shared folder (most useful one)
sudo chmod +t /srv/dropbox # sticky: like /tmp
sudo chmod 2770 /srv/shared # setgid + rwxrwx---, in one go
setgid on shared folders is the one you’ll actually use, see Permission best practices. Don’t set setuid on your own scripts. Find all setuid files with sudo find / -perm -4000 -type f 2>/dev/null.
umask: default permissions
The umask removes rights from new files. Ubuntu’s default is 002 for normal users, so new files are 664 and directories 775 (group may write; that works because every user has a private group).
| umask | Files | Directories |
|---|---|---|
002 (Ubuntu default) | 664 | 775 |
022 | 644 | 755 |
027 | 640 | 750 |
077 | 600 | 700 |
Check with umask. Set it for a service with UMask=0027 in its systemd unit, for users in ~/.bashrc.
ACLs: more than one group
Normal permissions allow exactly one owner and one group. ACLs (Access Control Lists) add rights for extra users and groups on the same file. Ubuntu’s ext4 and XFS support them out of the box. Tools: sudo apt install acl if setfacl is missing.
| What | Command |
|---|---|
| Show ACLs | getfacl /srv/shared |
| Give user ben read + write | setfacl -m u:ben:rw file |
| Give group auditors read on a folder | setfacl -R -m g:auditors:rX /srv/shared |
| Default ACL: also for new files | setfacl -R -d -m g:auditors:rX /srv/shared |
| Remove one entry | setfacl -x u:ben file |
| Remove all ACLs | setfacl -b file |
A + at the end of the rights in ls -l (drwxrws---+) means the file has ACLs. chmod on such a file changes the ACL mask, which can quietly limit what the ACL entries allow. If an ACL “doesn’t work”, look at the mask:: line in getfacl.
Tip: Use ACLs when one group really isn’t enough. If you can solve it with a group and setgid, do that, it is easier to understand later.