In most places, permissions are defined as an octal number in the format of 0777
. But UNIX's umask
command (thus os.umask()
) needs 0o000
to produce the permission bits of 0o777
and 0o022
equals to 0o755
in my understanding.
I heard that UNIX's umask
is inverted for some reason and I do not understand the reason behind it. Could someone please explain this inconsistancy?
Using this example, umask 777 creates a file with chmod 000 , umask 112 will be equal to chmod 664 . As far as I know, this happened because the umask command was originally created to indicate what permission bits the file will NOT have after it's created (hence the invertion).
The difference between umask and chmod is that umask changes the default permissions and thus the permissions for all newly created files and folders, while chmod sets permissions for files and folders that already exist.
A umask of 022 allows only you to write data, but anyone can read data. A umask of 077 is good for a completely private system. No other user can read or write your data if umask is set to 077. A umask of 002 is good when you share data with other users in the same group.
umask() method in Python is used to set the current numeric umask value and get the previous umask value. umask stands for user file-creation mode mask. This is used to determine the file permission for newly created files or directories. Syntax: os.umask(mask)
There is no real inconsistency, as the relation between umask
and chmod
can purely be written down with equations. Apparently, umask
sets the opposite of chmod
, it was created like this back in the old days.
Example: 022
(the default usual umask
) creates 755
. It works like this:
7 - 0 = 7
becomes the first byte7 - 2 = 5
becomes the second and third bytesUsing this example, umask 777
creates a file with chmod 000
, umask 112
will be equal to chmod 664
. As far as I know, this happened because the umask
command was originally created to indicate what permission bits the file will NOT have after it's created (hence the invertion).
While it could be annoying, it's really not hard to get used to it. Just think how you would chmod
your files, and subtract the byte you want from 7
, and you will get the umask
value. Or, when you are at the IDE, writing your code, don't use umask
, but rather create the file (with the default umask
of course) and then use, in Python, os.chmod()
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With