skip to content

What does a process's umask do on Linux, and why does a umask of 022 produce new files with mode 644 but new directories with 755?

level: middleimportance: should knowfreq 58%

answer

  1. a mask, not a default
  2. bits are cleared, never added
  3. requested AND NOT mask
  4. 0666 for files, 0777 for directories
  5. 022 leaves 644 and 755

basics

~20 s

The umask is a per-process mask of permission bits to clear at file creation: the final mode is the requested mode with the mask bits removed. Programs request 0666 for files and 0777 for directories, so a 022 mask leaves 644 and 755.

solid answer

~60 s

The umask is not a default mode — it is a **mask of bits to take away**. Every creation call passes a requested mode, and the kernel stores `requested & ~umask`. The asymmetry comes from what the callers request, not from the mask: routines that create ordinary files ask for `0666`, while `mkdir` asks for `0777`. Apply a mask of `022`, which clears group-write and other-write, and you get `0644` and `0755` respectively. Nothing on Linux ever gives a new regular file the execute bit by default, and that is because of the `0666` request, not the umask. The umask can only ever remove permissions — it cannot grant one that was not requested, so no umask will make new files executable. It is a per-process attribute inherited across fork and exec, set with the shell's `umask` builtin or the `umask()` system call, and it applies only at creation: it has no effect on `chmod`, and tools that explicitly restore a recorded mode, like `cp -p` or `tar -p`, bypass it.

code

bash · 6 lines
bash
umask
umask -S
umask 027
touch /tmp/u-file
mkdir /tmp/u-dir
stat -c '%a %n' /tmp/u-file /tmp/u-dir

go deeper

for a junior

Know that umask controls what permissions new files start with by taking bits away, and that the usual default of 022 gives 644 files and 755 directories.

for a middle

State the formula as requested AND NOT mask, explain that files come out without execute because the creator requests 0666 while mkdir requests 0777, and note that the mask is per-process and inherited.

for a senior

Reason about it operationally: where a daemon's mask actually comes from, why files land unreadable to a consumer, and why security-sensitive code requests a tight mode itself instead of trusting the caller's mask.

for a principal

Set the fleet policy: pick a default mask consistent with the group model in use, decide whether shared directories are solved by masks or by group ownership conventions, and make sure services get their mask from their start-up definition rather than by inheritance accident.

## A mask, not a default The single most common misunderstanding is that the umask *sets* the permissions of new files. It does the opposite: it names the bits that must **not** appear. When a program creates a file it supplies a mode argument, and the kernel computes ``` final_mode = requested_mode & ~umask ``` Read literally: take the requested bits, and clear every bit that is set in the mask. People often describe it as subtraction because `666 - 022 = 644` happens to come out right, but that is a coincidence of the common values. With a umask of `023`, subtraction would suggest `643`; the actual result is `0666 & ~0023 = 0644`, because bit-clearing cannot remove a bit that was not there. Reason with AND-NOT, never with arithmetic. ## Where the asymmetry comes from Why do files come out `644` and directories `755` under the same mask? - Library and shell routines that create a plain file conventionally request `0666` — read and write for everyone, and **no execute for anyone**. - `mkdir` requests `0777`, because a directory without its execute bit is unusable: you could not traverse into it. The mask then does the same thing to both: ``` files: 0666 & ~0022 = 0644 -> rw-r--r-- directories: 0777 & ~0022 = 0755 -> rwxr-xr-x ``` So the execute difference is entirely in the request. This also explains why you cannot make new files executable by choosing a clever umask: the mask has no power to add a bit that the caller never asked for. ## Scope and inheritance The umask is per-process state, like the current working directory. A child inherits it across `fork()` and it survives `exec()`, which is why setting it in a shell affects every command you subsequently run from that shell — and why setting it inside a script does not leak back to the parent shell. You read and set it with the shell builtin: ``` $ umask # 0022 $ umask -S # u=rwx,g=rx,o=rx (symbolic: what is ALLOWED) $ umask 027 # clear group-write, and all three bits for others ``` Note the inversion in the symbolic form: `umask` with no argument prints the bits being **removed**, while `umask -S` prints the bits that survive. Mixing those two readings up is a classic source of confusion. A login session's initial value comes from the login stack — commonly `/etc/profile` and shell startup files, or the `UMASK` setting in `/etc/login.defs` applied by the PAM module that handles it. Typical values: - `022` — the traditional default; new files are world-readable. - `027` — group-readable, nothing for others; common on shared servers. - `002` — group-writable, used on distributions that give every user a private group of their own, so "group-writable" is harmless for personal files but makes shared project directories work. - `077` — private to the owner; appropriate for accounts that handle sensitive material. ## What the umask does not touch - **`chmod`.** An explicit mode change is exactly that; the mask is not consulted. - **Existing files.** Changing the umask has no retroactive effect whatsoever. - **Restored modes.** `cp -p`, `rsync -p` and `tar -p` reapply a mode recorded elsewhere rather than creating with a default request, so the mask does not filter them. - **Programs that request a tight mode deliberately.** Something creating a private key file typically requests `0600`; the mask can only make that *tighter*, never looser. This is the right pattern — security-sensitive code should never rely on the caller's mask to protect it. ## Where it bites in production Two failure shapes recur. First, a daemon that inherits a `077` umask from an unusual login environment writes files nothing else can read, and the failure surfaces far downstream in whatever tries to consume them. Second, a shared upload or spool directory where several accounts must edit each other's files: with `022`, everything lands group-read-only and collaboration breaks. The umask is one half of that fix; the group ownership of the directory is the other half, and both need to be right. When you need a specific mask for a service, set it where the service is actually started rather than in an interactive shell file, because an interactive shell's startup files are not read for a daemon. ## The interview answer in one line It is a per-process mask of bits to clear at creation time, applied as `requested & ~umask`; files come out `644` and directories `755` under `022` because the creators request `0666` and `0777`, and the mask can only ever subtract.

  • Can you choose a umask that makes newly created files executable?
    No. The mask can only clear bits, never set them, and the routines that create ordinary files request `0666` — with no execute bit for any class. Whatever mask you pick, the execute bits were never requested, so they cannot appear. Making a file executable is always an explicit `chmod` after the fact.
  • A daemon writes files that its downstream consumer cannot read. How would you confirm the umask is the cause?
    Read the daemon's effective mask directly from its process rather than guessing from your own shell — the kernel exposes it per process — and compare with the modes on the files it produced. If they line up as `0666 & ~mask`, the mask is the cause, and the fix belongs in the service's start-up environment, not in an interactive shell profile.
  • Why do some distributions ship a default umask of 002 rather than 022?
    Because they create a private group per user, whose only member is that user. Group-write then grants nothing extra for personal files, while making shared directories usable when several accounts are added to a common group. It is only safe under that scheme — on a system where many users share one primary group, 002 makes every new file group-writable to all of them.

saying these in an interview costs you the question

  • Describing the umask as the default mode for new files
  • Computing the result by decimal subtraction instead of clearing bits
  • Expecting the umask to affect chmod or existing files
  • Thinking a umask can grant the execute bit to new files
  • Setting a service's umask in an interactive shell startup file

context