Detailed explanation of the use of umask under Linux

Detailed explanation of the use of umask under Linux

I recently started learning Linux. After reading Ma Ge's Linux course about umask, I wrote this blog hoping to deepen my understanding of umask and to help bloggers who are not clear about umask.

1 What is umask

When we log in to the system and create a file, there will be a default permission. So where does this permission come from? This is what umask does. umask is used to set the default permissions for users to create files or directories. umask sets the "complement" of permissions, while we often use chmod to set file permission codes. The umask value is usually set in /etc/profile, HOME/.bashprofile or HOME/.profile.

2 What is umask used for?

The default umask value is 022 (you can use the umask command to view it). At this time, the default permissions of the files you create are 644 (6-0, 6-2, 6-2), and the default permissions of the directories you create are 755 (7-0, 7-2, 7-2). You can verify it with ls -l. Now you should know the purpose of umask, which is to control the default permissions.

[root@bogon test]# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
[root@bogon test]# umask
0022
[root@bogon test]# touch a.txt
[root@bogon test]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jul 3 00:40 a.txt
[root@bogon test]# mkdir b
[root@bogon test]# ls -l
total 0
-rw-r--r--. 1 root root 0 Jul 3 00:40 a.txt
drwxr-xr-x. 2 root root 6 Jul 3 00:41 b

As can be seen above, the umask of root is 022 (the first 0 represents the special permission bit, which is not considered here), the default permissions of created files are 644, and the default permissions of created directories are 755.

3 Basic permissions explanation

Before explaining the use of umask, we need to explain the basic permissions of the file.

Linux file permissions

r w x
document You can view the file contents Can modify files You can start the file as a running program
Table of contents You can use ls to view the file names in the directory. You can create or delete files in the directory (you can't create with just w permission, you need x to cooperate) You can use cd to enter this directory ls -l to display the metadata information of the files in the directory

4 umask calculation permissions

For files and directories, the maximum permission is actually 777. However, the execution permission is very scary for files, while it is a basic permission for directories. Therefore, the default maximum permission for a directory is 777, and the default maximum permission for a file is 666.

For the root user's umask=022, the binary code for permission 777 is (111)(111)(111), and the binary code for permission 022 is (000)(010)(010).

  • All permissions binary 1: represents this permission
  • umask binary 1: means to remove this permission. No matter whether you originally have the permission or not, you will eventually not have this permission.
  • The binary value of umask is 0: It means that I don’t care about the permissions of the corresponding bit. If you have permissions, you have permissions. If you don’t have permissions, you don’t have permissions. I won’t affect you.

How to calculate the default permissions for files with umask 002

Owner Owner Owner x Group Group Group x Other Other Otherx
All permissions 777 1 1 1 1 1 1 1 1 1
umask mask 002 0 0 0 0 1 0 0 1 0
Calculated value 1 1 1 1 0 1 1 0 1

How to calculate the default permissions of a directory with umask 002

Owner Owner Owner x Group Group Group x Other Other Otherx
All permissions 666 1 1 0 1 1 0 1 1 0
umask mask 002 0 0 0 0 1 0 0 1 0
Calculated value 1 1 0 1 0 0 1 0 0

How to calculate the default permissions for directories with umask 023

Owner Owner Owner x Group Group Group x Other Other Otherx
All permissions 777 1 1 1 1 1 1 1 1 1
umask mask 023 0 0 0 0 1 0 0 1 1
Calculated value 1 1 1 1 0 1 1 0 0

How to calculate the default permissions of files with umask 023

Owner Owner Owner x Group Group Group x Other Other Otherx
All permissions 666 1 1 0 1 1 0 1 1 0
umask mask 023 0 0 0 0 1 0 0 1 1
Calculated value 1 1 0 1 0 0 1 0 0

The above is the normal calculation process of umask, but it is too troublesome. We use the following simple method to quickly calculate.

  1. For directories, just use 777-umask to get the final result.
  2. For files, use 666-umask first.
    1. If the corresponding position is an even number: the final authority is this even value.
    2. If the corresponding number above is an odd number, then the corresponding position is +1.

The above method is very convenient for calculation. Why do we need to add +1 when we get an odd number?

The maximum permission for a file is 666, which are all even numbers. If you get an odd number, it means your umask has an odd number. The read number is 4 and the write number is 2, which are all even numbers, which means you have execution permission.

Taking umask=023 as an example, when calculating other user permissions, 6-3=3, 6 is read and write, 3 is write and execute. In fact, the write permission should be obtained by subtracting the read permission from the read permission, which is equivalent to subtracting an execution permission. So the result is increased by 1.

5 Modification of umask

There are two types of umask modifications: temporary and permanent.

Temporary modification:

[root@bogon test]# umask 023
[root@bogon test]# umask
0023
[root@bogon test]#

Permanent modification:

You can edit the following file to add umask=022.

The interactive login configuration takes effect:

/etc/profile < /etc/profile.d/*.sh < ~/.bash_profile < ~/.bashrc </etc/bashrc [The configuration of /etc/bashrc is the most effective and can overwrite the previous configuration]

The non-interactive login configuration takes effect:

~/.bashrc < /etc/bashrc < /etc/profile.d/*.sh

6 Commonly used umask

[root@bogon test]# umask 002
[root@bogon test]# umask
0002
[root@bogon test]# umask 022
[root@bogon test]# umask
0022

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • In-depth understanding of umask in new linux file permission settings
  • Detailed explanation of the use principle and calculation method of the umask command under Linux

<<:  MySQL insert json problem

>>:  Several ways to switch between Vue Tab and cache pages

Recommend

MySQL 20 high-performance architecture design principles (worth collecting)

Open Source Database Architecture Design Principl...

How to use Vue's idea to encapsulate a Storage

Table of contents background Function Purpose Ide...

MySQL uses variables to implement various sorting

Core code -- Below I will demonstrate the impleme...

How to isolate users in docker containers

In the previous article "Understanding UID a...

About the implementation of JavaScript carousel

Today is another very practical case. Just hearin...

Detailed explanation of Linux mpstat command usage

1. mpstat command 1.1 Command Format mpstat [ -A ...

DIV common attributes collection

1. Property List Copy code The code is as follows:...

nginx+tomcat example of accessing the project through the domain name

I was curious about how to access the project usi...

Summary of commonly used escape characters in HTML

The commonly used escape characters in HTML are s...

How to disable web page styles using Firefox's web developer

Prerequisite: The web developer plugin has been in...

When you enter a URL, what exactly happens in the background?

As a software developer, you must have a complete...

Install Linux using VMware virtual machine (CentOS7 image)

1. VMware download and install Link: https://www....