Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

umask Command Guide for Default File Permissions

umask controls the permissions that new files and directories get — the default you can’t see until after you create them.


What is umask, and Why Use It?

When you create a file, Linux starts with maximum permissions and then subtracts something. That subtraction is the umask (“user mask”). The same value decides the permissions of every file and directory you create, for your whole shell session.

It’s invisible most of the time — until a file you created shows up rw-r--r-- when you wanted rw-rw-r--, or a script you wrote can’t be executed because it came out rw-r--r--.

Why Learn umask?

  • You create files with predictable permissions instead of being surprised by them.
  • Scripts and services set their umask so their output has the right ownership and access.
  • Security defaults matter — a too-permissive umask (like 000) makes every new file world-readable.
  • It explains why “the file was created with weird permissions” happens at all.

umask is not a permission on a file — it’s the default subtracted from every newly created file. Change it once per session and every file you create follows it.


1. See Your Current umask

umask

Example:

$ umask
0022
  • The value is shown as an octal number, usually four digits (the leading 0 is often dropped): 022.
  • 022 is the classic default: new files end up 644, new directories 755.

2. Understand the Math

The umask is subtracted from the defaults:

Created itemStarting valueMinus umask 022Result
File666 (rw-rw-rw-)022644 (rw-r–r–)
Directory777 (rwxrwxrwx)022755 (rwxr-xr-x)

Example:

$ touch testfile
$ ls -l testfile
-rw-r--r-- 1 alice alice 0 Dec  4 09:30 testfile
  • Files never get the execute bit from umask alone (executables must be marked with chmod).
  • Each umask digit masks one permission class: first = owner, second = group, third = others.

3. Set a umask for the Session

umask 022
umask 002

Example:

# Group can write new files too (useful for shared project dirs)
umask 002

# Everything you create from now on is owner-only
umask 077
  • The change lasts until you close the shell (or set it again).
  • umask 002 is the common choice for collaborative directories: new files are 664, new directories 775.

4. Set a Persistent umask

To make it survive reboots and new shells, put it in the shell startup files:

echo 'umask 002' >> ~/.bashrc

Example:

# For all users on the system (as root):
echo 'umask 027' >> /etc/profile
  • ~/.bashrc covers interactive shells; /etc/profile covers login shells system-wide.
  • Services usually set their own umask in their unit files or init scripts.

5. Check the umask in Scripts

Scripts should set their umask explicitly so their output is predictable:

#!/bin/bash
umask 077
echo "secrets" > private.txt

Example:

$ ./secure.sh
$ ls -l private.txt
-rw------- 1 alice alice 8 Dec  4 09:31 private.txt
  • Without an explicit umask, the script inherits whatever the caller had — private.txt could end up world-readable.
  • Setting it at the top of the script makes behavior independent of the environment.

6. Common umask Values

umaskNew fileNew dirMeaning
022644755Classic default; others read-only
002664775Group-writable; collaboration
027640750Group read, no others
077600700Owner only — private
000666777Everything open (avoid!)

Example:

# Owner-only defaults for a private workspace
umask 077
  • 077 is a sensible default for anything sensitive — keys, notes, credentials.

Common Pitfalls

  • Confusing umask with permissions. umask never changes an existing file; it only affects newly created ones. To fix old files, use chmod.
  • Assuming umask applies globally. It’s per-shell-session. Each terminal, cron job, and service has its own.
  • Setting umask 000. Every file you create becomes world-readable and (for directories) world-writable — a security hole.
  • Forgetting files never get x from umask. Executables must be marked explicitly with chmod +x.
  • Editing the wrong startup file. ~/.bashrc covers interactive shells only; login shells and services need /etc/profile or their own config.

PageWhy it’s related
chmodFix permissions on files umask already created
chownOwnership works together with umask defaults
touchCreating a file is where the umask applies
mkdirNew directories get the umask’d 755/775/700
idCheck your identity before setting defaults

umask Cheatsheet

CommandMeaning
umaskPrint the current umask
umask 022Classic default — others read-only
umask 002Group-writable — collaboration
umask 077Owner-only — private files
umask 000Everything open (avoid)
echo 'umask 002' >> ~/.bashrcMake it persistent for your shells
ls -l newfileVerify what the umask produced