On Unix-like operating systems like Linux, the operating system consists of files and folders. With everything coming back to files and folders, permissions are important. Permissions for newly created files are just as important. Think about it, you don’t want to be constantly thinking about file permissions, and nor should you or your users. That is where umask comes into play, it allows for a safe default to be configured. In this blog post, we will discuss the umask command from what it is to how to use it to set a custom umask.
What Is Umask?
The umask command is a utility that is used to control the initial file permissions for newly created files and directories.
What Is The Difference Between Chmod And Umask?
Chmod changes permissions after a file or directory is created. Umask sets the default permission for files and directories before they are created to be applied as they are created.
How To Calculate Umask Values
The initial mask value is calculated by subtracting the umask value from the default value, which is 777 for directories and 666 for files (because files are never created with execute permissions).
So for example, if we have a umask of 022 (which is the default on many systems) the resulting permissions would be 755 for directories and 644 for files.
Note: Sometimes a umask will have four digits, e.g. 0022. The first 0 is for the setuid, setgid, and sticky bits.
Umask Values
Here are the values, keep in mind that these are being subtracted so a 2 would be removing write permissions.
- or — Which means no permission change
- or –x Which means removing the execute permission
- or -w- Meaning remove write permission
- or -wx Meaning remove write and execute permissions
- or r– Which means remove read permission
- or r-x To remove read and execute permissions
- or rw- To remove Read and write permissions
- or rwx To remove all three Read, write, and execute
Are There Other Ways To Get The Umask?
Here are two websites I found that allows you to calculate the umask without needing to do that math.
How To Find The Current Umask Value?
To find your calculated umask in octal notation run the following:
umask
To See umask in Symbolic notation value run:
umask -S
How Do I Set A Custom Mask?
In this section, we have a few different ways that umask can be set.
Feel free to use your preferred value, Octal or Symbolic.
Temporally Set Umask For One User
To temporally set a custom umask all you have to do is run the umask command with the mask value after it as shown below.
umask [mask_value]
This will take effect immediately but will only affect the current session.
Permanently Set Umask For One User
To change your default permissions for a single user we take the same command but add it to the user’s ~/.bashrc file.
echo "umask [mask_value]" >> ~/.bashrc
Make sure to replace [mask_value]
Be aware that this will only apply on the next login, see the Temporally Set Umask section if you want to apply before for a user.
Permanently Set Umask For All Users
To permanently override the default umask create a file at /etc/profile.d/umask.sh.
sudo echo "umask [mask_value]" > /etc/profile.d/umask.sh.
Make sure to replace [mask_value]
Be aware that this will only apply on the next login, see the Temporally Set Umask section if you want to apply before for a user.
Conclusion
In this post, we started by answering what is umask and how it differs from the chmod command. Moved on to how to calculate umask values, what the values mean, and how to get the current mask value. Finally, we wrap things up with how to set a custom mask.
Additional Links
Done reading and looking for additional webpages to read, why not check these out?
Embracing a lifelong passion for technology since childhood, CJ delved into the intricate workings of systems, captivated by the desire to understand the unknown. This innate curiosity led to his discovery of Linux, a revelation that resonated deeply. With more than 7 years of on the job experience, he’s honed his technical skills as a Geek and Senior Linux Systems Administrator.
Leave a Reply