Chapter 06Lesson 04~65 minutes

umask, Special Bits, and Shared Directories

Permissions are often created indirectly. A program requests a mode, the process umask removes selected bits, and directory inheritance or ACL defaults may further shape the result. Special bits add execution identity and directory collaboration semantics. This lesson turns those mechanisms into predictable private and shared workspace policies.

IntermediateumaskSpecial bits

Learning objectives

By the end of this lesson

  • Calculate effective creation modes from requested modes and a umask.
  • Inspect and set numeric or symbolic umasks in a controlled scope.
  • Explain setuid, setgid, and sticky behavior on files and directories.
  • Recognize special-bit notation in long listings.
  • Build a setgid shared directory and verify inherited group ownership and default modes.

1. umask removes bits from a program’s requested mode

When a process creates an object, it supplies a requested mode. The kernel clears permission bits selected by the process umask. Conceptually:

\[\text{effective mode} = \text{requested mode} \;\&\; \sim\text{umask}\]

Many utilities request 0666 for regular files and 0777 for directories. Files do not receive execute merely because the umask permits it; the creating program normally does not request execute.

Default-mode creation pipeline
flowchart TD
  P["Program requests mode
file 0666 or dir 0777"] --> U["Process umask clears bits"] U --> A["Default ACL may adjust classes"] A --> O["Object created with final mode"] O --> V["stat / getfacl verification"]
# Display the current mask in numeric and symbolic form.
umask
umask -S

# Demonstrate in a subshell so the interactive shell is unchanged.
(
  umask 027
  mkdir -p demo-dir
  : > demo-file
  stat -c '%A %a %n' -- demo-dir demo-file
  rm -rf -- demo-dir demo-file
)

With umask 027, a requested directory mode of 777 becomes 750, while a requested file mode of 666 becomes 640.

2. umask belongs to a process and is inherited

The shell’s umask is inherited by child processes. A service manager, CI runner, login profile, container entrypoint, or application can establish a different mask. Changing your interactive shell does not retroactively modify existing objects.

umaskTypical file modeTypical directory mode
022644755
027640750
077600700
002664775
Creation defaults are not enforcement

A program can request a narrower mode or call chmod afterward. Treat umask as a safe default, then verify security-sensitive files explicitly.

3. Special bits add identity and directory semantics

BitNumeric valuePrimary behavior
setuid4000Executable runs with the file owner’s effective UID, subject to kernel and mount rules
setgid2000Executable runs with file group; on a directory, new entries inherit the directory group
sticky1000On a directory, restricts deletion/rename to appropriate owners or privileged users

Long listings replace an execute character with s/S for setuid or setgid and t/T for sticky. Lowercase means the corresponding execute bit is also set; uppercase means the special bit exists while execute is absent.

lab="$HOME/devops-academy/linux/chapter06/lesson04"
mkdir -p -- "$lab/setgid-dir" "$lab/sticky-dir"
chmod 2770 -- "$lab/setgid-dir"
chmod 1777 -- "$lab/sticky-dir"
stat -c '%A %a %n' -- "$lab/setgid-dir" "$lab/sticky-dir"
Setuid is a security boundary

Do not create setuid executables as a learning shortcut. Setuid shell scripts are disabled or unsafe on many systems. Privileged helpers require minimal code, strict input validation, controlled environments, and professional review.

4. setgid directories stabilize collaborative group ownership

Without setgid, a newly created object normally receives the creator’s effective group, which may differ across team members. A setgid directory causes new entries to inherit the directory’s group. A cooperative umask such as 002 can then leave group write enabled.

shared="$HOME/devops-academy/linux/chapter06/lesson04/shared"
rm -rf -- "$shared"
mkdir -p -- "$shared"
chgrp -- "$(id -gn)" "$shared"
chmod 2770 -- "$shared"

(
  umask 002
  mkdir -- "$shared/release-a"
  printf 'owner=%s
' "$(id -un)" > "$shared/release-a/manifest.txt"
)

find "$shared" -maxdepth 2 -printf '%M %m %u:%g %p
' | LC_ALL=C sort

The setgid bit controls group inheritance; it does not automatically grant group write to new files. That depends on requested modes, umask, and default ACLs.

5. Sticky directories protect names in open shared spaces

A directory such as /tmp must allow many users to create files, but one user should not remove another user’s entries. The sticky bit adds this deletion/rename restriction. It does not encrypt files or prevent reading when file modes allow reading.

ls -ld -- /tmp
stat -c '%A %a %U:%G %n' -- /tmp

# A private lab example of the mode shape.
drop="$HOME/devops-academy/linux/chapter06/lesson04/dropbox"
mkdir -p -- "$drop"
chmod 1777 -- "$drop"
stat -c '%A %a %n' -- "$drop"

Testing multi-user sticky behavior requires at least two identities, so perform that experiment only in a disposable VM where creating test accounts is permitted.

6. Hands-on lab: compare private and collaborative defaults

set -eu
lab="$HOME/devops-academy/linux/chapter06/lesson04"
rm -rf -- "$lab/private" "$lab/shared-lab"
mkdir -p -- "$lab/private" "$lab/shared-lab"

# Private workspace: owner-only defaults.
(
  umask 077
  : > "$lab/private/secret.env"
  mkdir -- "$lab/private/cache"
)

# Shared workspace: inherited group and cooperative defaults.
chgrp -- "$(id -gn)" "$lab/shared-lab"
chmod 2770 -- "$lab/shared-lab"
(
  umask 002
  : > "$lab/shared-lab/release.txt"
  mkdir -- "$lab/shared-lab/artifacts"
)

{
  printf '=== shell mask ===
'
  umask
  printf '=== private ===
'
  find "$lab/private" -maxdepth 2 -printf '%M %m %u:%g %p
'
  printf '=== shared ===
'
  find "$lab/shared-lab" -maxdepth 2 -printf '%M %m %u:%g %p
'
} > "$lab/default-mode-report.txt"

cat -- "$lab/default-mode-report.txt"
test "$(stat -c %a -- "$lab/private/secret.env")" = 600
test "$(stat -c %a -- "$lab/shared-lab")" = 2770
test "$(stat -c %g -- "$lab/shared-lab/release.txt")" = "$(stat -c %g -- "$lab/shared-lab")"

Verification checklist

7. Common default-mode and special-bit mistakes

Subtracting umask as decimal arithmetic

Use a bit-clearing model. Simple subtraction can produce incorrect intuition when bits do not overlap.

Setting umask globally without scope analysis

Login shells, services, CI jobs, and applications may need different creation policies.

Assuming setgid grants write

It stabilizes group inheritance; mode or ACL entries must still grant write.

Treating sticky as confidentiality

Sticky restricts deletion and rename in a directory. File read permissions remain separate.

8. Knowledge check

Question 1. With requested file mode 666 and umask 027, what mode is normally created?

Question 2. What does setgid do on a directory?

Question 3. What problem does the sticky bit solve on a directory such as /tmp?

9. Summary

umask supplies process-scoped creation defaults by clearing requested bits. Setgid directories provide predictable group inheritance, sticky directories constrain deletion in open shared spaces, and setuid executables cross a sensitive identity boundary. Reliable designs combine scoped defaults, explicit modes, controlled special bits, and post-creation verification.

Next lesson

Access Control Lists and Permission Troubleshooting

The final lesson extends the owner/group/other model with ACL entries and builds a systematic workflow for diagnosing real permission failures.

10. Further reading

  • Linux man-pages: umask(2), inode(7), and credentials(7).
  • GNU Coreutils manual for file permissions and special modes.
  • Bash manual for the umask builtin.
  • Filesystem and security documentation for setuid/setgid mount behavior.

Keep the academy open

Support free, practical DevOps education.

Every lesson is designed to remain readable in a browser, downloadable from GitHub, and usable without a paid learning platform. Contributions help expand and maintain the curriculum.

Ethereum / ERC-20
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0 Send only Ethereum/ERC-20 compatible assets to this address.