Permissions

INPUT · Slides

The default for new things (`umask`)

01 / 08

Who decided on 644?

Let us start with the question you should have noticed last lesson.

~ $ touch new.txt~ $ stat -c %a new.txt644

Nobody asked for it and it came out 644. You typed no chmod.

Where does that number come from? It is not touch that decides, because making it with echo hi > file.txt or copying it with cp gives you 644 as well.

What decides is a setting called umask. Today you learn to read it.

02 / 08

Look at your umask

Type it on its own and out comes the current setting.

~ $ umask0022

0022. Reading it the way you learned last lesson, it looks like "owner 0, group 2, other 2".

But hold on. That is nothing like 644.

Quite so: umask is not the permissions you are given, it is the permissions taken away. The name (mask, as in cover over) says as much.

There is a number before the taking away, a base. The next slide shows it to you.

03 / 08

The base is 666 and 777

There is a way to see the base with your own eyes: set it to take nothing away.

~ $ umask 0~ $ touch wideopen.txt~ $ stat -c %a wideopen.txt666~ $ mkdir widedir~ $ stat -c %a widedir777

There it is.

What you makeBase
a file666 (rw-rw-rw-)
a directory777 (rwxrwxrwx)

Did you notice the base for a file has no x? The rule is that a newly made file never carries the execute mark from the start.

Which makes sense. A file you just made being runnable would be dangerous. That is why making run.sh still meant typing chmod u+x yourself.

04 / 08

Take the umask off the base

That is the whole mechanism: take the umask off the base.

file:       666 minus 022 = 644directory:  777 minus 022 = 755

Read 022 digit by digit: "take nothing from the owner, take 2 (w) from the group, take 2 (w) from everyone else".

So the result is this.

BaseTakenResult
ownerrw-nothingrw-
grouprw-wr--
otherrw-wr--

-rw-r--r--, which is 644. The default of "I can read and write, others can only read" was made by umask 022.

Mystery solved.

05 / 08

Make it stricter

Change the umask and the default for things you make from now on changes.

~ $ umask 077~ $ touch secret.txt~ $ stat -c %a secret.txt600

077 means "take everything (7) from the group and everyone else", so the file comes out 600, yours alone, from the start.

umaskFileDirectoryMeaning
022644755the default. Others can read
027640750the group can read, others nothing
077600700you alone
002664775the group can write too

Set umask 077 before doing secret work and you never have to type chmod 600, which also stops you forgetting.

06 / 08

It removes rather than subtracts

We have been saying "subtract", but strictly it is a bit different. It is not ordinary subtraction; it removes from the permissions you have.

Let us see a case where it shows. What do you think umask 003 gives?

the other digit of 666 is rw- (6)the other digit of 003 is -wx (3)

Ordinary subtraction says 6 minus 3 is 3, but that is not what happens. You cannot remove what the base does not have.

  • w (2) is in the base → removed
  • x (1) is not in the base → nothing happens

The result for the other digit is r-- (4). Not 6 minus 3 = 3, but 4.

Day to day you use numbers like 022 and 077 where the difference never shows, so you need not worry. But "subtract" will trip you up occasionally, so the right word to remember it by is masking.

07 / 08

Only things made from now on

One important warning. umask has no effect on files that already exist.

~ $ ls -l memo.txt-rw-r--r--  memo.txt        <- 644~ $ umask 077~ $ ls -l memo.txt-rw-r--r--  memo.txt        <- unchanged

umask only decides the starting value of things made from now on. To change something that exists, chmod is the only way.

One more. A umask setting only lives inside that terminal and goes back when you close it. To make it stick, write it in a settings file called .profile, which is chapter 10.

Do not mix these two up.

  • chmod — change what is already there
  • umask — set the default for what you make next

08 / 08

Now have a go

Your home holds one memo.txt (at 644). Everything else you make yourself as you check things.

Checking works as it did last lesson, with stat -c %a.

One knack. After changing the umask, always make something new and check. Changing it alone does nothing visible; you have to touch, mkdir or > something to see the result.

The setting survives within a question, but between questions the Linux is rebuilt and it goes back to 022. So start each one by typing your umask.