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
| Syntax | What it does |
|---|---|
umask | show the mask as a number: 0022 |
umask -S | show which permissions remain: u=rwx,g=rx,o=rx |
umask -p | show it as a command umask 0022 — handy to save and restore later |
umask 027 | set 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-r | allow / 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).
| Mask | New file (from 666) | New directory (from 777) | When it's used |
|---|---|---|---|
022 | 644 rw-r--r-- | 755 rwxr-xr-x | the usual value for root |
002 | 664 rw-rw-r-- | 775 rwxrwxr-x | users with a personal group: the group can write |
027 | 640 rw-r----- | 750 rwxr-x--- | nothing for others |
077 | 600 rw------- | 700 rwx------ | everything for the owner only: keys, secrets |
000 | 666 rw-rw-rw- | 777 rwxrwxrwx | everything for everyone — almost never needed |
Where it's set permanently
| Place | For whom |
|---|---|
~/.bashrc, ~/.profile | one user's interactive shells |
/etc/profile, /etc/bash.bashrc | all users' shells |
UMASK in /etc/login.defs | the value at login; applied by the pam_umask module |
UMask= in a systemd unit file, umask in a start script | services — 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
033a file ends up644, not633: thexbit wasn't there, so there's nothing to clear. - The mask doesn't add the
xbit to files. Even with mask000a new file is666— you can make it executable only withchmod. - Symbolic notation is the opposite of numeric.
umask 027andumask 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.
touchon an existing file doesn't change its permissions. Overwriting with>keeps the old permissions too. - The value lives in the process.
umask 077in 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
002at login, while root gets022. Check the mask as the user who creates the files. cpandmkdir -m.cpinto a new file takes the source's permissions and applies the mask too.mkdir -m 755sets permissions explicitly and ignores the mask.- Not everything obeys the mask. A program can set permissions itself after creation (
ssh-keygenmakes the key600regardless of the mask), and a default ACL on the directory overrides it.