What is umask in Linux? Give usage examples
- User
What is umask in Linux? Give usage examples
- Assistant
What is umask in Linux? Give usage examples
In Linux, umask is a command and also a setting that determines the default permissions assigned to newly created files and directories. The term "umask" stands for "user file creation mask." It defines which permission bits should be turned off when new files or directories are created by a user.
Each file and directory has permission bits that control read, write, and execute permissions for the user (owner), group, and others (everyone else). These permissions are represented numerically as follows:
When a new file or directory is created, they start with a default set of permissions. These defaults are usually:
The umask value subtracts from these defaults.
To calculate the default permissions for a new file or directory, you subtract the umask value from the base permissions.
For example:
If your umask is 022, the permissions for a new file will be:
666 - 022 = 644 (rw-r--r--)For a new directory:
777 - 022 = 755 (rwxr-xr-x)View the Current umask: To see the current umask setting, run:
umask
Set a New umask: You can change the umask for the current session with:
umask 027
This sets the umask to 027, which would result in new files created with permissions 640 (rw-r-----) and new directories with permissions 750 (rwxr-x---).
Creating a New Directory:
First, let's set a umask of 077 (this means no permissions for group or others).
umask 077
mkdir mydir
Check the permissions:
ls -ld mydir
The output would show permissions drwx------, which means only the owner has read, write, and execute permissions.
Creating a New File:
Set a umask to 002, which allows group users to have write permissions:
umask 002
touch myfile
Check the file permissions:
ls -l myfile
You should see -rw-rw-r--, meaning that the user (owner) has read and write permissions, the group has read and write permissions, and others have read-only permissions.
To make a umask setting persistent across sessions, you can add it to your shell's configuration file, such as .bashrc or .bash_profile for Bash users:
echo "umask 027" >> ~/.bashrc
source ~/.bashrc
This way, every time you start a new shell session, the umask will be set to 027 automatically.
The umask command is a powerful tool in Linux for controlling the default permissions of newly created files and directories. Understanding and configuring it properly can enhance security by limiting permissions for newly created files, helping to minimize unauthorized access.
Not yet included in a published release.