Whenever you create a new file or directory in Ubuntu, the Linux kernel assigns initial access permissions. Rather than requiring users to manually run chmod on every newly created document, shell script, or directory, the operating system uses a setting known as umask (user file-creation mode mask). The umask acts as a filter that automatically subtracts specific permissions from default maximum values upon file creation.
Understanding and configuring your system or user-level umask ensures that sensitive files, server logs, and shared project directories receive secure default permissions from the moment they are generated.
Understanding How the umask Calculation Works
Linux assigns maximum theoretical permissions to newly created items before applying the mask:
- New files: Start with maximum base permissions of
666(read and write for user, group, and others; execution permissions are withheld by default for security). - New directories: Start with maximum base permissions of
777(read, write, and execute/enter for user, group, and others).
The umask value defines which permission bits to strip away. The system subtracts the umask from the base permission:
- Standard default umask
022:- For files:
666 - 022 = 644(-rw-r--r--: owner can read/write; group and others can only read). - For directories:
777 - 022 = 755(drwxr-xr-x: owner has full access; group and others can read and enter).
- For files:
- More restrictive umask
027:- For files:
666 - 027 = 640(owner can read/write; group can read; others have zero access). - For directories:
777 - 027 = 750(owner has full access; group can read/enter; others cannot access).
- For files:
- Strict private umask
077:- For files:
666 - 077 = 600(only the owner can read/write; group and others have zero permissions). - For directories:
777 - 077 = 700(only the owner can enter and manage files).
- For files:
Checking and Testing Your Current umask
To inspect your active umask value in the Ubuntu terminal:
- Open your terminal and type:
umask - The command prints the numeric octal mask (e.g.
0022). - To view the active permission mask in human-readable symbolic notation, run:
This outputs the permissions that will be preserved, such asumask -Su=rwx,g=rx,o=rx. - To test a temporary umask for your current shell session:
Any files or directories created subsequently in this terminal window will follow theumask 027027filter. Closing the terminal restores your original default.
Configuring Permanent umask Settings
To apply a custom umask permanently, you must define it within your shell profile or system-wide configuration files.
For an Individual User
- Open your user’s bash configuration file in a text editor:
nano ~/.bashrc - Scroll to the end of the file and add your desired umask line:
umask 027 - Save the file by pressing Ctrl + O, then press Enter and exit with Ctrl + X.
- Reload the configuration immediately by running:
source ~/.bashrc
For System-Wide Server Profiles
On multi-user Ubuntu servers, you can enforce a default umask for all users by editing /etc/login.defs or PAM configuration:
- Open the system login configuration file with root privileges:
sudo nano /etc/login.defs - Locate the
UMASKentry (typically defaulting to022) and update it to your required security baseline (e.g.027). - Save and close the file.
For related network and access administration, explore our tutorial on how to allow connections from a specific IP address in Ubuntu using UFW.
Troubleshooting umask Conflicts and Overrides
If newly created files do not reflect your configured umask, keep the following considerations in mind:
- Application-specific permissions: Certain programmes (such as
git, database daemons, or text editors with atomic save modes) explicitly specify permission bits when creating files, overriding the shell umask. - POSIX Access Control Lists (ACLs): If a parent directory has default POSIX ACLs assigned (via
setfacl -d), the filesystem inherits ACL defaults and masks, superseding traditional umask behaviour. Check for directory ACLs usinggetfacl [directory]. - SSH non-login sessions: Non-interactive SSH commands run without sourcing
~/.bashrc. To enforce umask across all remote sessions, define it in~/.profileor configurepam_umask.soin/etc/pam.d/common-session.
By establishing appropriate umask values across user accounts and system daemons, you prevent accidental data exposure and maintain consistent, hardened file permissions across your Ubuntu environment.