Support >
  About cybersecurity >
  How does umask in Linux manage file permissions?
How does umask in Linux manage file permissions?
Time : 2025-12-17 14:18:05
Edit : Jtti

In Linux systems, access control is a core security mechanism for managing files and directories. When a new file or directory is created, the system assigns a set of default access permissions. These default permissions are not determined by a fixed value but by a process mask called `umask`. Understanding how `umask` works is crucial for ensuring file security, achieving expected access control, and troubleshooting permission-related issues.

`umask`, short for "user file-creation mask," is an octal number held by a process and used to "mask" or "filter" a portion of the basic permissions for newly created files or directories. Its core function is not to "assign" permissions, but to "restrict" them, ensuring that newly created files and directories are not overly open, thus adhering to the principle of least privilege.

In Linux, the permissions of any file or directory are represented by three sets of "read, write, execute," corresponding to the file owner, group, and other users, respectively. When a process (such as your shell terminal, a script, or a program) requests to create a new file, the system first assigns a "maximum permissive permission" for that type of object. Then, it subtracts the portion blocked by `umask` from this permission to obtain the final initial permissions.

For newly created regular files, the maximum permissive permission assigned by the system is typically `666` (octal), meaning all users can read and write, but not execute. This is to prevent newly created text, data, and other files from accidentally acquiring execute permissions.

For newly created directories, the maximum permissive permission assigned by the system is `777`, meaning all users can read, write, and enter. This is because executable permissions for directories represent "entry and search," which is essential for directory functionality.

The `umask` operation is not mathematical subtraction, but a bitwise AND operation. Simply put, a bit with a value of 1 in `umask` will "turn off" the corresponding bit in the final permissions. A more intuitive way to understand this is: Final permissions = Base permissions & (~umask), or it can be seen as "removing" the permissions specified by `umask` from the base permissions.

The most common default umask value is `0022`. Let's see how it works:

When creating a file: Base permissions `666` (binary: 110 110 110), umask `022` (binary: 000 010 010). The umask suppresses write permissions for the "owner group" and "other users". The final file permissions are `644` (binary: 110 100 100), meaning the owner can read and write, and others can only read.

When creating a directory: Base permissions `777` (111 111 111), minus umask `022`. The final directory permissions are `755` (111 101 101), meaning the owner can read, write, and enter, and others can read and enter but cannot create/delete files within it.

Another common umask is `0002`, which only blocks write permissions for "other users":

Creating a file: `666` minus `002` equals `664` (owner has read and write access, group members have read and write access, other users have read access only).

Creating a directory: `777` minus `002` equals `775` (owner and group members have read, write, and access permissions, other users have read and access permissions).

In the shell, viewing the current umask is very simple:

umask
# Possible output: 0022

If you want to see the symbolic form (similar to `rwx`), you can add the `-S` parameter:

umask -S
# Possible output: u=rwx,g=rx,o=rx

Setting umask can be temporary or permanent:

Temporary setting: Executes the command directly in the current shell session, and it only applies to that session and processes started within it. For example, to set the session's umask to the more restrictive `0077` (only the owner has read and write permissions):

umask 0077

Permanent setting: The umask command needs to be added to the user's shell initialization file so that it takes effect automatically upon each login. Common configuration files are `~/.bashrc` (for Bash) or `~/.bash_profile`. Simply add the following line to the end of the file:

echo "umask 0022" >> ~/.bashrc

Then execute `source ~/.bashrc` to make the changes take effect immediately in the current session. Note that the system-wide umask setting is defined in `/etc/profile` or `/etc/bash.bashrc`, but it is generally not recommended to modify it directly unless there is an explicit global policy requirement.

For accurate calculation, familiarity with octal permission notation is required. Each permission group (owner, group, others) is represented by a number from 0 to 7, which is the sum of three binary bits:

Read permission (r) = 4

Write permission (w) = 2

Execute permission (x) = 1

Therefore, `rwx` (4+2+1) = 7, `rw-` (4+2+0) = 6, `r-x` (4+0+1) = 5. So the number `755` corresponds to the symbolic representation `rwxr-xr-x`.

umask is a process attribute and is inherited. When a process creates a child process (for example, when running a script in a shell), the child process inherits the parent process's umask value. This means that the umask you set in a shell will affect the file permissions created by all commands and programs launched from that shell.

It's important to note that umask only affects files and directories created via system calls (such as `open()`, `mkdir()`). It has no effect on permissions of existing files modified by the `chmod` command, or on permissions of files created through copying (`cp`). The behavior of the `cp` command is usually determined by its own options (such as `-p` preserving permissions).

Umask is particularly important in certain scenarios:

Shared directory collaboration: In team project directories, setting a lenient umask (such as `0002`) ensures that team members can smoothly read and write newly created files, facilitating collaboration.

Security-sensitive environments: In scripts or services that handle sensitive data, setting a strict umask at the beginning (such as `0077`) is a defensive programming technique that prevents the accidental creation of globally readable files.

Service process configuration: Many daemons (such as web servers and databases) have their own startup scripts or configuration files where independent umasks can be set to ensure that files they generate (such as logs and session files) have secure default permissions, unaffected by the umask of the user shell that started them.

When using umask, there are a few points to keep in mind:

1. umask settings are "sticky." Once set in a process, they continue to affect it until changed again.

2. Different shells (such as `zsh` and `fish`) may have slightly different display formats or configuration files for umask, but the core concept remains the same.

3. At the system call level, processes can query and modify their own umask at runtime using the `umask()` system call.

4. The safest approach is to choose an appropriate umask value based on the needs of your working environment and permanently configure it in a configuration file, rather than relying on potentially inconsistent default values ​​each time.

Understanding umask essentially means understanding how Linux automatically and consistently enforces permission policies. It acts like a dutiful gatekeeper, silently working behind the scenes for every file creation request, ensuring that every new resource is equipped with the appropriate "access restriction armor" from its inception.

Pre-sales consultation
JTTI-Jean
JTTI-Eom
JTTI-Selina
JTTI-Defl
JTTI-Ellis
JTTI-Amano
JTTI-Coco
Technical Support
JTTI-Noc
Title
Email Address
Type
Sales Issues
Sales Issues
System Problems
After-sales problems
Complaints and Suggestions
Marketing Cooperation
Information
Code
Submit