← Full reference

umask

Default permission mask — which bits are removed from new files and directories

umask is a shell builtin. It sets a mask of bits that are removed from every new file and directory created by this process and its children. Programs usually create files with mode 666 and directories with 777, and the mask strips the excess.

Syntax

umask [-p] [-S] [mask]

Without an argument it shows the current mask. The mask applies only to the current shell and processes started from it. It doesn't change existing files.

Options and notations

SyntaxWhat it does
umaskshow the mask as a number: 0022
umask -Sshow which permissions remain: u=rwx,g=rx,o=rx
umask -pshow it as a command umask 0022 — handy to save and restore later
umask 027set the mask as a number: which bits to remove
umask u=rwx,g=rx,o=set it symbolically: which permissions to keep (like -S, i.e. the opposite of what the number means)
umask g+w / umask o-rallow / forbid a single bit relative to the current mask

A numeric mask is written the same way as permissions in chmod: digits for owner, group and others, 4 — r, 2 — w, 1 — x.

How the result is computed

The new object's permissions = the permissions the program requested without the mask's bits (requested & ~mask).

MaskNew file (from 666)New directory (from 777)When it's used
022644 rw-r--r--755 rwxr-xr-xthe usual value for root
002664 rw-rw-r--775 rwxrwxr-xusers with a personal group: the group can write
027640 rw-r-----750 rwxr-x---nothing for others
077600 rw-------700 rwx------everything for the owner only: keys, secrets
000666 rw-rw-rw-777 rwxrwxrwxeverything for everyone — almost never needed

Where it's set permanently

PlaceFor whom
~/.bashrc, ~/.profileone user's interactive shells
/etc/profile, /etc/bash.bashrcall users' shells
UMASK in /etc/login.defsthe value at login; applied by the pam_umask module
UMask= in a systemd unit file, umask in a start scriptservices — shell profiles don't affect them

Any process's current mask is visible in /proc/PID/status, the Umask: line.

Examples

# view the current mask in different forms
umask
umask -S
umask -p

# close new files to "others" and check
umask 027
touch report.txt && mkdir data
stat -c '%a %n' report.txt data     # 640 report.txt, 750 data

# change the mask for one command only — in a subshell
(umask 077; echo 'token=…' > ~/secret.env)

# save the mask and restore it after a block of commands
old=$(umask -p)
umask 077
touch private.key
$old

# make the mask permanent for your interactive shells
echo 'umask 027' >> ~/.bashrc

# create a directory with the right permissions without touching the mask
mkdir -m 750 /srv/app/private

# what mask a running process has
grep Umask /proc/1/status

Pitfalls

  • It's not subtraction, it's clearing bits. With mask 033 a file ends up 644, not 633: the x bit wasn't there, so there's nothing to clear.
  • The mask doesn't add the x bit to files. Even with mask 000 a new file is 666 — you can make it executable only with chmod.
  • Symbolic notation is the opposite of numeric. umask 027 and umask u=rwx,g=rx,o= are the same thing: the number lists what to remove, the letters — what to keep.
  • The mask doesn't touch existing files. touch on an existing file doesn't change its permissions. Overwriting with > keeps the old permissions too.
  • The value lives in the process. umask 077 in one terminal doesn't affect other sessions, services and cron. For a permanent setting use the files from the table above.
  • Different users have different masks. On Ubuntu, users with a personal group (group name matches the user name) get mask 002 at login, while root gets 022. Check the mask as the user who creates the files.
  • cp and mkdir -m. cp into a new file takes the source's permissions and applies the mask too. mkdir -m 755 sets permissions explicitly and ignores the mask.
  • Not everything obeys the mask. A program can set permissions itself after creation (ssh-keygen makes the key 600 regardless of the mask), and a default ACL on the directory overrides it.

See also

chmod · chown · acl · mkdir · proc · useradd