Umask

Every file that gets created comes with a default set of permissions. If you ever wanted to change that default set of permissions, you can do so with the umask command. This command takes the 3 bit permission set we see in numerical permissions.

Instead of adding these permissions though, umask takes away these permissions.

$ umask 021

In the above example, we are taking nothing away from the user, taking the write permission away from the group, and taking the execute permission away from everyone else.

Now, what is umask subtracting from? Not 777, as you might expect. The system starts from 666 for a new file and 777 for a new directory, and then removes the umask bits. Files start at 666 because it would be a bad idea to make every file you create executable.

So with the common default umask of 022:

new file:      666 - 022 = 644  (rw-r--r--)
new directory: 777 - 022 = 755  (rwxr-xr-x)

This is why a file you just made is not executable even though your umask leaves the user bits alone. If you want to run it as a program, you still have to add the execute bit yourself with chmod.

When you run the umask command it will give that default set of permissions on any new file you make, for as long as that shell lasts. To make it stick, the umask line goes in one of the files your shell reads when it starts, usually ~/.bashrc. That is covered in the Startup files lesson in Your Environment, which also explains which of those files is read when.

Exercise

  1. Create a new file, then note it's permissions.
  2. Modify the umask and then create another new file.
  3. Check the permissions once more on the new file, what do you expect to see?
  4. Make a new directory as well and compare its permissions to the new file's.

Quiz Question

What command is used to change default file permissions?

Show answer

umask