I know that:
- A file default mod is
666
umask
value will be removed from default mods.
So why when I set the "umask" to 555
it doesn't set newly created file's permissions to 111
? instead it's setting them to 222
I know that:
666
umask
value will be removed from default mods.So why when I set the "umask" to 555
it doesn't set newly created file's permissions to 111
? instead it's setting them to 222
Short answer:
Because with a
5
you are removing theread (4)
andexecutable (1)
bit, so you end up with onlywrite (2)
.Explanation:
With
555
you are not setting default executable bit on.We got these mods:
The only way that I can create a
5
is from4 + 1
, so5
actually means:It means "remove" executable and read mods if they're are being set.
In other words, with
umask 555
you are removing the read ( 4 ) and executable ( 1 ) bit from default file mode ( 6 ) which brings us to the ( 2 ), because in a 6 we only have a 4 and 2 to remove (not any 1):You removal only effects the 4, so the file ends up with
222
.In binary
Think of it in binary:
File default mode is 666 (110 110 110), and our
umask
value is555
(101 101 101):See? we ended up to the -w-w-w-, or
222
.The result umask value is mask & 0777 (bit wise and)
When mask is 0555
Than 0555 & 0777 result with 0222
The source of the difference between
touch file
andmkdir dir
:Definition
Roughly speaking, in general, the on bits of a mask switch off (if not already off) the bits of what it is masking.
More precisely, in this particular case, the resulting mode of a newly created file or folder follows the following bitwise operation:
where
result
is the resulting mode,mode
is the usual mode (666 for files and 777 for folders), and!mask
is the bitwise negation ofmask
, the set mask.Examples
Masking write (2) permission on the left, and read (4) and execution (1) permissions on the right.
Notes
Observe from the last example that masking does not coincide with subtraction (6-5=1 in both, decimal and binary notation).