Intermediate · 7 min · practise every command in the built-in terminal

umask — Default Permissions

Why is a new file -rw-r--r-- (644) and a new folder drwxr-xr-x (755)? Because of the umask.

umask              # 0022
umask -S           # u=rwx,g=rx,o=rx — what new files may get

How it works

New files start from 666 (read and write for everyone), folders from 777. The umask names the bits to take away:

Startumask 022Result
File666 rw-rw-rw-− ----w--w-644 rw-r--r--
Folder777 rwxrwxrwx− ----w--w-755 rwxr-xr-x

022 = nobody but you may write. Execute is never given to files by default — that's always a chmod +x.

Changing it

umask 077          # only I can read and write what I make
touch draft.txt
mkdir private-dir
ls -ld draft.txt private-dir
-rw------- 1 student student    0 Oct 30 10:00 draft.txt
drwx------ 2 student student 4096 Oct 30 10:00 private-dir
umaskFilesFoldersUse
022644755the usual: others may read
002664775a group works on the same files
077600700private: keys, backups, secrets

The umask only changes what new files get — files that already exist keep their permissions (that's what chmod is for).

For one command, or for good

It belongs to the shell it's set in. For a few commands, set it in a subshell ( … ) — it ends with them:

(umask 077; echo "token=abc123" > secret.txt)
ls -l secret.txt   # -rw-------
umask              # still 0022

For good: put umask 077 in ~/.bashrc.

💡 A script that writes passwords or keys should set umask 077 at the top — then there's no moment when the file is readable by others.

Practice tasks

  1. Show your current umask
  2. Show it in symbolic form
  3. Set the umask to 077 and create draft.txt
  4. Make a folder private-dir and check that only you may enter it
  5. Set the umask back to 022

Commands in this lesson

Permissions