Beginner · Permissions, users and sudo · topic 10 of 57 · 30 min
umask and the special bits: SUID, SGID and sticky
Explanation
Where do new files get their permissions?
When a program creates a file, it asks for 666 (rw-rw-rw-) and for a directory 777 (rwxrwxrwx). The umask (user file-creation mask) then takes away bits. Each 1 bit in the umask removes that permission. The result is the requested mode AND NOT umask; for simple cases you can just subtract digit by digit.
| umask | New file | New directory |
|---|---|---|
| 0022 | 644 rw-r--r-- | 755 rwxr-xr-x |
| 0002 | 664 rw-rw-r-- | 775 rwxrwxr-x |
| 0077 | 600 rw------- | 700 rwx------ |
| 0027 | 640 rw-r----- | 750 rwxr-x--- |
$ umask
0002
$ umask 027
$ touch a.txt; mkdir d
$ ls -ld a.txt d
-rw-r-----. 1 asha asha 0 Oct 19 12:10 a.txt
drwxr-x---. 2 asha asha 6 Oct 19 12:10 dFiles never get execute from the umask alone, because the start value 666 has no x. That is a safety feature: a downloaded file cannot run until someone adds x.
On a typical RHEL 9 system, root's umask is 0022 and normal users with a private group get 0002. To change it for good, set it in a shell start-up file such as ~/.bashrc for one user, or in a file under /etc/profile.d/ for everyone.
The special bits
A fourth, leading octal digit holds three special bits: SUID = 4, SGID = 2, sticky = 1. In ls -l they replace the x in one of the sets.
| Bit | Octal | ls shows | On a file | On a directory |
|---|---|---|---|---|
| SUID (set user ID) | 4000 | rws------ (s in owner x) | The program runs as the file's owner | No effect |
| SGID (set group ID) | 2000 | ---rws--- (s in group x) | Runs as the file's group | New files inside get the directory's group |
| Sticky | 1000 | -------rwt (t in others x) | No effect on Linux | Users can delete only their own files |
$ ls -l /usr/bin/passwd
-rwsr-xr-x. 1 root root 32648 Aug 10 2021 /usr/bin/passwd
$ ls -ld /tmp
drwxrwxrwt. 18 root root 4096 Oct 19 12:00 /tmp
# chmod 2770 /srv/team # SGID + rwxrwx---
# chmod g+s /srv/team # the same SGID, with letters
# chmod 1777 /srv/dropbox # sticky, like /tmp
# chmod u+s /usr/local/bin/tool # SUID (be very careful)A capital S or T (for example rwSr--r--) means the special bit is set but the execute bit under it is not, which is usually a mistake.
Why SGID directories matter for teams
Without SGID, a file that asha creates in /srv/team gets her primary group, asha, so the rest of the team cannot edit it. With SGID on /srv/team (group team), every new file gets the group team automatically. Combined with umask 0002 (group can write), the whole team can work on each other's files.
Common mistakes
- chmod 770 /srv/team after setting SGID: on many systems a plain three-digit chmod on a directory keeps the SGID bit, but do not rely on that; use 2770 to be explicit.
- Thinking umask is a permission. It is a mask of bits to REMOVE.
- Setting the sticky bit on a file and expecting something to happen. On Linux it only matters on directories.
Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
umask and the special bits: SUID, SGID and sticky · 1/7
New files ask for six six six, new directories for seven seven seven. The umask takes bits away.
Prefer a video by a teacher? Find free videos on “linux umask suid sgid sticky bit explained” on YouTube ↗
In real life: A shared project folder at an engineering college
At a college in Coimbatore, final-year students work in teams on a lab server. Each team gets /srv/projects/teamN owned by a group, with mode 2770: only members can enter, and every file they create belongs to the team group. A common /srv/dropbox has mode 1777 so any student can upload, but nobody can delete another student's submission.
# mkdir -p /srv/projects/team4
# chown root:team4 /srv/projects/team4
# chmod 2770 /srv/projects/team4
# ls -ld /srv/projects/team4
drwxrws---. 2 root team4 6 Oct 19 12:30 /srv/projects/team4Practice (0/4 done)
The umask is 0077. What mode does a new file get?
Write apply_umask(umask, is_dir=False) that returns the mode (as a 3-digit string) a new file or directory gets. Start from "666" for files or "777" for directories, and remove every bit set in the umask (start AND NOT umask). umask is a string like "022" or "0027"; use only its last 3 digits.
Which command makes new files in /srv/web get the group of the directory?
Create /srv/shared for the group "team" so that: only root and team members can enter, members can create files, new files belong to group team automatically, and members cannot delete each other's files.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Access control lists (ACLs)Permissions, users and sudo · same module
- Users, groups and permissionsDevOps: Git, Docker, Kubernetes and CI/CD · Linux for DevOps
- Positioning and stackingFrontend Web Development · CSS layout and responsive design
- Bits: AND, OR, XOR and shiftsData Structures and Algorithms (DSA) for interviews · Tries, union-find and bit manipulation
- Linux security basicsCyber Security: from zero to SOC analyst · Systems and cryptography
- Network, broadcast and usable host rangeComputer Networking (CCNA level) · IPv4 addressing
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
With umask 0022, what does a new directory get?
Answer: 755
777 minus 022 is 755.
What does the t in drwxrwxrwt mean?
Answer: Sticky bit: users can delete only their own files
t in the others-execute slot is the sticky bit.
Why does /usr/bin/passwd have the SUID bit?
Answer: So a normal user can update /etc/shadow through it
It runs as root, the owner, so it can write the shadow file.
What octal mode sets SGID plus rwxrwx---?
Answer: 2770
SGID is 2 in the leading digit.
Why does a new file never get x from the umask?
Answer: Files start from 666, which has no execute bits
The start value for files has no x, so you must add it with chmod.
A file is owned by asha with mode 604 (rw----r--). asha is also in the file's group. Can asha read it?Related
Answer: Yes, as owner
The owner set rw- applies to asha, so she can read and write.
On a directory, what does the x permission allow?Related
Answer: Entering the directory with cd
For directories, x means you may enter (traverse) it.
Which removes every ACL entry from a file?Related
Which value keeps a header visible while you scroll its section, but in the normal flow before that?Related
Answer: sticky
sticky behaves normally until it reaches the given edge, then sticks.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 2 more topics in “Permissions, users and sudo” before its exam: Access control lists (ACLs), sudo and the sudoers file.
- Module 2 exam: Permissions, users and sudo · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
- Then the next module, Processes, services and logs. 57 topics left in the course before the final exam.