(turn) The Umask value of Linux system and its corresponding file permission explanation

Source: Internet
Author: User
Tags arithmetic create directory file permissions

On the umask value of Linux system and its corresponding file permission explanation

Original: http://blog.51cto.com/oldboy/1060032

Origin:
1, this article is written in particular to thank the 51cto blog staff and leadership, the old boy blog is honored to become an expert blog, the old boy responsibility is heavier, and strive for the future to share articles for everyone.
2, the old boy himself driving test today just get Ben (lasted 2 months, completely by strength through, retake once).
3, some netizens and students always tangled in this umask problem, search everywhere can not find a good description of the article.
Another: This article is the most worthy of Bo friends to think about the place is not content, but diligent thinking and more comprehensive efforts to practice the study mentality, and then able to self under a more accurate conclusion of the learning and memory of the idea, make an analogy, Jin Yong's novels are difficult to remember, then make it into the couplet after no one can forget the lost. In the same way that we learn Linux operations, we should simulate setting up some test scenarios to verify tests, and finally summarize a small conclusion that we can keep in mind, which is the best way to learn Linux.

This article omitted the Linux file permissions and umask basic knowledge of the introduction, the need to know the Bo friends can see other information.

1 ) Simple and easy to use addition and subtraction calculation (old boy recommended)

Special Note: The first method of calculation is simple, easy to use, good to remember, but because most users are directly using subtraction (regardless of the specificity of this method is criticized) or some books are misleading or even do not believe that the method is not dared to. In fact, learning is a memory of the train of thought, there is no right or wrong, is more thinking more practice, and then, summed up a small skill conclusion, conducive to their own learning and memory is enough.

Finally, the old boy would like to say that learning concludes is not to do science, so, may not need so rigorous, there are pseudoscience small conclusions is normal (but the pursuit of rigorous mentality must have), such as: Nginx service configuration of the standard is what, MySQL optimization standards is what, Install Linux system to choose the standard of the installation package is what, to the system partition standards is what, in fact, these are not standard answers, but this does not affect us to learn Linux operation, this is the old boy conduction to everyone's thinking.

File permissions Calculation Small conclusion:

The default maximum permission to create a file is 666 (-rw-rw-rw-), and the file created by default does not have the X-bit executable permission.

For a file, Umask is set to assume that the file has an octal 666 permission, the file's permission is 666 minus umask (umask each bit number is not greater than 6, for example, 077 does not meet the criteria) of the mask value; Focus on the next content, If some or all of the bits in the umask are odd, then 1 of the result of the file permission bits corresponding to the odd number is the final file permission value.

Create directory default maximum permissions 777 (-RWX-RWX-RWX), the default directory created by the owner is the X permission, allowing the user to enter.
For a directory, Umask is set to assume that the file has octal 777 permissions, and the directory octal permission 777 subtracts the mask value of umask.

General calculation methods for file permissions:

Default File Permissions calculation method

1) Assume that the Umask value is: 022 (all bits are even)

6 6 6 Start Permission value for ==> file

0 2 2-value of ==>umask

---------

6 4 4

2) Assume that the Umask value is: 045 (other user group bits are odd)

6 6 6 Start Permission value for ==> file

0 4 5-Value of ==>umask

---------

6 2 1 ==> the calculated permissions. Since the last digit of umask is 5, add 1 to the other user group.

0 0 1 +

---------

622 ==> True File permissions

Default directory Permissions calculation method

7 7 7 Start permission value for ==> directory

0 2 2-value of ==>umask

---------

7 5 5

The specifications are as follows:


Example One: umask when all the bits are even, most readers have no doubt that this example asks

[Email protected] oldboy]# umask

0022 #→umask Current Value

[[email protected] oldboy]# umask 044 #→ changed to 044

[Email protected] oldboy]# umask

0044

[[email protected] oldboy]# mkdir umask_test #→ Build Catalog Test

[Email protected] oldboy]# ls-ld umask_test

DRWX-WX-WX 2 root root 4096 19:21 umask_test #→ corresponding digital permission is 733, is it consistent with the above calculation method?

[email protected] oldboy]# Touch umask_test.txt

[Email protected] oldboy]# ls-l umask_test.txt

-rw--w--w-1 root root 0 19:21 umask_test.txt #→ The corresponding digital permission is 622, is not consistent with the above calculation method?

Example Two: umask when some or all bits of the value are odd, this is the biggest reader question.

when umask when the other genus of the value is an odd group bit

[Email protected] oldboy]# umask 0023

[Email protected] oldboy]# mkdir dir

[[email protected] oldboy]# Touch file

[Email protected] oldboy]# ls-l

Total 4

drwxr-xr--2 root root 4096 11-15 01:04 dir #→ corresponding digital permissions are 754

-rw-r--r--1 root root 0 11-15 01:04 file #→ corresponding digital permission is 644

Tip: According to the previous calculation method, when Umask is 0023, dir should have a permission of 754, and file should be 643, but because the other group bits of umask are odd, the final permission is the other group bit plus 1, that is 643 plus 001 (corresponding to the practice result 644). Note: Do not add 1 umask to even digits.

Example Three: umask when all bits of a value are odd

[Email protected] oldboy]# umask 0551

[Email protected] oldboy]# umask

0551

Umask 0551 is calculated according to the mask method: The directory permission is 226, the file permission 115, and the actual file permission is 226 (umask three permission bits are odd, so each bit 1 is the correct permission)

[Email protected] oldboy]# mkdir DIR5

[email protected] oldboy]# Touch file5

[Email protected] oldboy]# ls-l

Total 4

D-w--w-rw-2 root root 4096 11-15 01:27 dir5 #→ directory corresponds to a digital permission of 226

--w--w-rw-1 root root 0 11-15 01:27 file5 #→ directory corresponds to a digital permission of 226

One more example of verification:

[Email protected] oldboy]# Umask 0333

[Email protected] oldboy]# umask

0333

Umask 0333 is calculated according to the mask method: The directory permission is 444, the file permission 333, and the actual file permission is 444 (umask three bits are odd, so each bit 1 is the correct permission)

[Email protected] oldboy]# mkdir dir3

[email protected] oldboy]# Touch file3

[Email protected] oldboy]# Ls-l|grep 3

dr--r--r--2 root root 4096 11-15 01:30 dir3 #→ directory corresponds to a digital permission of 444

-r--r--r--1 root root 0 11-15 01:30 file3 #→ directory corresponding digital permission is 444

2) calculated by the 8 alphabet symbol (memory This method can also)

Take the example above 3 to verify.

[Email protected] oldboy]# Umask 551

The Umask value is 551, and the corresponding permission is-r-xr-x--x, which means that all files and directories are removed-r-xr-x--x permissions in the permission.

File permissions: "-rw-rw-rw-"-"-r-xr-x--x" = "--w--w--rw-" =226 (no X on file X-bit, no cancellation)

Directory permissions: "-RWXRWXRWX"-"-r-xr-x--x" = "--w--w--rw-" =226 (the X-bit of the directory has x, can be canceled directly)

Continue with the previous example to verify:

[Email protected] oldboy]# Umask 333

The Umask value is 333, and the corresponding permission is-WX-WX-WX, which means that all files and directories are removed-WX-WX-WX permissions in the permission.

File permissions: "-rw-rw-rw-"-"-wx-wx-wx" = "-r--r--r--" =444 (no X on file X-bit, no cancellation)

Directory permissions: "-RWXRWXRWX"-"-wx-wx-wx" = "-r--r--r--" =444 (the X-bit of the directory has x, can be canceled directly)

3) Calculation by binary method (bad memory not recommended)

By converting between the binaries, the octal binary is turned into binary to calculate.

0022

0 2 2

----------#→ octal Conversion binary

0 10 10

Description: Converts the Umask value eight binary into binary

6 6 6

------------#→ octal Conversion binary

110 110 110

Description: Converts the file default maximum permission value eight to binary

0 10 10 #→umask Binary

-------------#→ and arithmetic

110 110 110 #→ file maximum permissions binary

-------------

000 010 010

-------------#→ convert octal

0 2 2

Note: The last obtained value is 022, then 666 minus 022, then the file creation permission is 644

[Email protected] oldboy]# Umask 551

[Email protected] oldboy]# umask

0551

5 5 1

-------------#→ Conversion Binary

101 101 001 #→umask Binary

-------------#→ and arithmetic

110 110 110 #→ file maximum permissions binary

-------------

100 100 000

-------------#→ convert octal

4 4 0

Note: The last value obtained is 440, then 666 minus 440, then the file creation permission is 226.
PostScript: Especially welcome to this article to make suggestions, together to create a better learning Linux ecological environment.

Copyright Notice: Original works, if you need to reprint, please contact the author. Otherwise the legal liability will be investigated

(turn) The Umask value of Linux system and its corresponding file permission explanation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.