Previous Section  < Day Day Up >  Next Section

Recipe 9.8. Setting Permissions Defaults with umask

9.8.1 Problem

You want to understand why files are created with a certain set of default permissions, and how to configure them yourself.

9.8.2 Solution

The umask (user file-creation mode mask) controls this behavior. To see what yours is, use:

$ umask

0022

or:

$ umask -S

u=rwx,g=rx,o=rx

To change it temporarily, for the duration of your login session, use:

$ umask 0002

You can set the umask permanently by inserting the line umask 0022 or whatever value you want in your ~/.bashrc file. Table 9-3 shows common umask values.

Table 9-3. Common umask values

umask

User

Group

Other

0002

All

All

Read and Execute

0022

All

Read and Execute

Read and Execute

0007

All

All

None

0077

All

None

None


9.8.3 Discussion

Table 9-4 shows that you can come up with a umask for any occasion.

Table 9-4. All umask values

umask

File permissions

Directory permissions

7

None

None

6

None

Execute

5

Write

Write

4

Write

Write and execute

3

Read

Read

2

Read

Read and execute

1

Read and write

Read and write

0

Read and Write

Read-write-execute


umasks "subtract" permissions—though I warn you, do not say this to a programmer, because you will be subjected to a tedious lecture on how it's not subtraction, it's that "the umask is bitwise and-ed to whatever modes are specified when the file is created." However, I do not have a problem with calling it subtraction.

When a program, such as a text editor or a compiler, creates a file, it is hardcoded to set the file permissions at either 0666 or 0777. If it knows the file it is creating is executable, it sets the permissions to 0777. The most common value is 0666, which is why you have to chmod +x scripts. Neither 0666 nor 0777 are appropriate permissions most of the time, so umask screens out the bits you don't want. For example, a umask of 0002 means regular files will become 0664, and executable files will become 0775.

9.8.4 See Also

  • Chapter 4 of LPI Linux Certification in a Nutshell, for exhaustive detail on umask, right down to the binary level

    Previous Section  < Day Day Up >  Next Section