Whenever you create a file or a directory in Linux, the system automatically assign them certain permissions. So if you want to change these default permissions, you meed to use the umask command.
In this post we are going to learn, How to set default file permissions using umask command in Linux.
umask command in linux is used to set the default permissions for files and directories. Every time you create a new file/directory certain permission are assigned to it by the system on its own. If you don’t like these default permissions then we can use umask command to change them.
Where mode signifies the desired set of permissions. Using umask without the mode will show the existing set of permissions
Will assign all permissions to user, read and write to group and only write to others on files and directories that will be created after using this command. It will not affect the existing files and directories.
The working of umask is opposite to chmod i.e., the permissions specified with umask will not be assigned. For example, writing 2 means write will not be assigned, 5 means read and execute will not be assigned and so on. So, in the above example $umask 015 means all permissions to user, read and write to group and only write to others
Exception: Regular files will not get execute permissions even when specified in umask. This is an important point to remember. A regular file like “.txt” is not meant to be executed, hence the system will never assign execute permission to a regular file even if execute permission is specified in umask. But, you can always assign the execute permission if you so desire using chmod command.
We create a file “t1.txt” and a directory “DIR1”. Then ls command is used to view the permissions. These permissions are as per current value of umask which you can check in your system using $umask.
Next, we use the umask command to see the change.
Then we change the value of umask to 025 which means all permissions to user, read and execute for group and only write for others. Then, we create a new file “t2.txt” and a new directory “DIR2” and check the permissions again. Now, both of them get new permissions as per the updated value of umask. The directory gets exactly the same permissions as expected but for the file, the expected execute permission in case of user and group are missing. Remember these are due to the reason that “t1.txt” is a regular file. But, all executable files will get exactly the same permissions as specified in umask. A file created using the gcc command will be an executable file.
Practice questions on How to set default file permissions using umask command?
Q1. True/False..umask can be used to change the permissions of existing file?
Q2. Write the command to change the default permissions of directories to read and write for user, read for group and others.
Q3. What will be the output of the following command
Q4. What will be the output of the following command