Linux umask usage

Source: Internet
Author: User

Umask usage

A What is umask?

When we log on to the system and create a file, there is always a default permission. How does this permission come from? This is what umask does. Umask sets the default permission for the user to create files, which is the opposite of CHMOD. umask sets the permission "complement", while chmod sets the File Permission code. Generally, set the umask value in/etc/profile, $ [home]/. bash_profile, or $ [home]/. profile.

Your system administrator must set a reasonable umask value for you to ensure that the files you create have the desired default permissions and prevent other users from having the write permission on your files. After logging on, you can use the umask command as your preference to change the default permissions for file creation. The change remains valid until you exit the shell or use another umask command. Generally, the umask command is set in the/etc/profile file. Every user will reference this file during logon. Therefore, if you want to change the umask of all users, you can add corresponding entries to the file. If you want to set your umask value permanently, place it in the. profile or. bash_profile file under your $ home directory.

How does B calculate the umask value?

The umask command allows you to set the default mode when creating a file. Each type of user (file owner, users in the same group, and other users) has a number in the corresponding umask value. For a file, the maximum values of this number are 6. The system does not allow you to grant the execution permission when creating a text file. You must use the CHMOD command to add this permission after the creation. Directory allows you to set the execution permission. In this way, the number in umask can be up to 7 for the directory.

The command is generally in the following format:

Umask NNN

Where NNN is set to umask to 000-777.

Let's look at some examples.

Calculate your umask value:

You can use several methods to calculate the umask value. By setting the umask value, you can set the default permissions for newly created files and directories. The following table lists the umask values corresponding to the permissions.

When calculating the umask value, you can create default permissions for various users in this table to find the corresponding umask value according to the required file/directory.

For example, the default permissions for creating files and directories corresponding to umask 002 are 6 6 4 and 7 7 5, respectively.

There is another method for calculating the umask value. We only need to remember that umask is "taking" the corresponding bit from the permission.

Umask value and permission

Umask file directory

0 6 7

1 6 6

2 4 5

3 4 4

4 2 3

5 2 2

6 0 1

7 0 0

For example, if the umask value is 0 0 2, what is the default creation permission for the corresponding files and directories?

Step 1: first write down the mode with all permissions, that is, 777 (all users have read, write, and execute permissions ).

Step 2: Write the corresponding bits in the following line according to the umask value. In this example, the value is 0 0 2.

Step 3: In the next row, remember that no matching BITs exist in the above two rows. This is the default directory creation permission.

Remember this method with a little practice.

Step 4: The file cannot have the execution permission when it is created. You only need to remove the corresponding execution permission bits.

In this example, the u m a s k value is 0 0 2:

1) Maximum File Permission: rwx (777)

2) umask is 0 0 2 ----w-

3) directory permission: rwx R-X (775). This is the default permission for Directory Creation.

4) File Permission RW-r -- (664) This is the default permission for file creation.

The following is another example. Assume that the u m a s k value is 0 2 2:

1) Maximum File Permission: rwx (777)

2) u m a s k value is 0 2 2 ----w-

3) directory permission: rwx R-X (755). This is the default permission for Directory Creation.

4) File Permission RW-r -- (644) This is the default permission for file creation.

Common umask values in C

The following table lists some umask values and their corresponding directory and file permissions.

Common umask values and corresponding file and directory permissions

Umask value directory file

0 22 7 5 5 6 4 4

0 27 7 5 0 6 4 0

0 02 7 7 5 6 6 4

0 06 7 7 1 6 6 0

0 07 7 0 6 6 0

D umask command

If you want to know the current umask value, you can use the umask command:

$ Umask

To change the umask value, you only need to use the umask command to set a new value:

$ Umask 002

Check whether the system has accepted the new u m a s k value:

$ Umask

002

$ Touch testfile

$ LS-l testfile

RW-r --

Before using the umask command, you must determine the file/Directory Creation default permissions you want. Otherwise, some very strange results may be obtained. For example, if you set the umask value to 6 0 0, the created file/Object

The default permission is 0, 6, and 6!

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.