That's an excellent follow-up question, as umask directly relates to directory (and file) permissions, which we just touched upon with mkdir -m!
umask stands for "user file creation mask". It's a fundamental concept in Linux/Unix file permissions.
Here's a breakdown:
-
Default Permissions: When you create a new file or directory, the system assigns it a set of default permissions.
- For files, the theoretical maximum permission is usually
666(read/write for owner, group, and others). - For directories, the theoretical maximum permission is usually
777(read/write/execute for owner, group, and others). (Directories need execute permission to be entered/navigated.)
- For files, the theoretical maximum permission is usually
-
The Role of
umask: Theumasksetting acts as a "mask" that removes specific permissions from these default maximums when a new file or directory is created. It's like a filter. -
How it Works (Subtraction):
-
You usually see
umaskas a three-digit octal number, like0022or0002. The leading0is often ignored for calculation. -
To find the actual permissions of a newly created item, you subtract the
umaskvalue from the maximum default permissions. -
Example with
umask 022:-
For a new directory:
- Maximum default:
777 umask:-022- Resulting permissions:
755(rwxr-xr-x) — Owner can read/write/execute, Group can read/execute, Others can read/execute.
- Maximum default:
-
For a new file:
- Maximum default:
666 umask:-022- Resulting permissions:
644(rw-r--r--) — Owner can read/write, Group can read, Others can read.
- Maximum default:
-
-
-
Why
umask?- Security: It's a security measure. Rather than making every new file and directory fully open (
777or666),umaskensures that by default, they have more restrictive permissions, preventing unwanted access. - Consistency: It provides a consistent way to set default permissions across a system or for a specific user.
- Security: It's a security measure. Rather than making every new file and directory fully open (
How to check your current umask:
You can simply type umask in your terminal:
umask
The typical output is often 0002 or 0022. In LabEx, it's usually 0002.
If your umask is 0002:
- New directories get
777 - 002 = 775(rwxrwxr-x) - New files get
666 - 002 = 664(rw-rw-r--)
Understanding umask helps you know why your newly created files and directories have the permissions they do by default, and how mkdir -m can override those defaults.
Does this explanation of umask clarify its role for you?