Skip to content

Beginner · Permissions, users and sudo · topic 10 of 57 · 30 min

umask and the special bits: SUID, SGID and sticky

Explanation

umask decides the permissions of new files: it removes bits from 666 for files and 777 for directories. Three special bits add more: SUID runs a program as its owner, SGID on a directory makes new files inherit its group, and the sticky bit on a shared directory lets people delete only their own files.

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.

umaskNew fileNew directory
0022644 rw-r--r--755 rwxr-xr-x
0002664 rw-rw-r--775 rwxrwxr-x
0077600 rw-------700 rwx------
0027640 rw-r-----750 rwxr-x---
bash
$ 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 d
Running umask without an argument shows it; with an argument sets it for this shell only.

Files 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.

BitOctalls showsOn a fileOn a directory
SUID (set user ID)4000rws------ (s in owner x)The program runs as the file's ownerNo effect
SGID (set group ID)2000---rws--- (s in group x)Runs as the file's groupNew files inside get the directory's group
Sticky1000-------rwt (t in others x)No effect on LinuxUsers can delete only their own files
bash
$ 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)
passwd has SUID so a normal user can update /etc/shadow, which only root may write. /tmp has the sticky bit.

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.

Careful: SUID programs run with their owner's rights, often root. A bug in a SUID-root program can give an attacker root. Never set SUID on scripts or on programs you do not fully trust. Security teams regularly list SUID files with find / -perm -4000 -type f and check each one.

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

666 − 022 = 644 777 − 022 = 755

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.

bash
# 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/team4

Practice (0/4 done)

Task 1 · Question

The umask is 0077. What mode does a new file get?

Task 2 · Code · Python

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.

Task 3 · Question

Which command makes new files in /srv/web get the group of the directory?

Task 4 · Practice

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

Question 1

1.With umask 0022, what does a new directory get?

Question 2

2.What does the t in drwxrwxrwt mean?

Question 3

3.Why does /usr/bin/passwd have the SUID bit?

Question 4

4.What octal mode sets SGID plus rwxrwx---?

Question 5

5.Why does a new file never get x from the umask?

Read more (free)

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.

From File permissions: rwx, chmod and chown

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.

From Users, groups and permissions

Which removes every ACL entry from a file?Related

Answer: setfacl -b file

-b removes all extended entries.

From Access control lists (ACLs)

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.

From Positioning and stacking

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

  1. 2 more topics in “Permissions, users and sudo” before its exam: Access control lists (ACLs), sudo and the sudoers file.
  2. Module 2 exam: Permissions, users and sudo · 14 questions · 25 min, without help. 6 topics of this module still to finish first.
  3. Then the next module, Processes, services and logs. 57 topics left in the course before the final exam.

← Previous: File permissions: rwx, chmod and chown