r/linuxadmin • u/Own_Wallaby_526 • Oct 13 '25
Logic Behind User Masks(umask)??
Hey, I am new to learning Linux system administration and I wanted to ask this:-
What is the point of umask(user masks)? I get the default permission part but I don't like the subtracting part of it. Why can't processes/programs who create files just have base permissions set for the type of the file(directory, regular files, sockets, symbolic links.....).
We already do have base permissions which are global and umask for different processes. Again, why couldn't we just have had base permissions changing depending on the process??
Why go the lengthy route of subtracting from the base permissions to get the actual permissions??
17
u/wise0wl Oct 13 '25
Don't think of it as a single number that's being subtracted. It is, but it doesn't work that way in practice. What you are doing with a umask is setting specific bits to zero. Those bits represent specific permissions. So if you set a specific umask it will always mask those bits and set those permissions off. So, if you don't want a process to have the "other" bits set (read, write, execute, directory execute) you can mask just those bits.
You don't have to know the existing permissions and then iterate through things to figure out the new permissions, just mask the bits you want to mask and let it go.
2
u/Entaris Oct 13 '25
Depending on context there are different permissions that might be set by default. umask isn't telling the system what permissions you want, its telling the system what permissions you dont want, which allows the system to do logic to derive sane permissions based on whatever context is there.
Remember that directories need execute permissions in addition to read permissions to be accessible. So to set defaults you have to account for the difference between 644 and 755.
By making it a simple bit mask it allows a user to set expectations easily, without creating weird logic around whether or not setting a default permission is going to accidentally end up with random files having execute permissions on them.
Remember that these were designed at a point where system resources were at a premium. every tiny bit of logic you put into any sort of process was costly. masking creates a simple global logic chain that lets a user set sane default permissions without having extra logic checks elsewhere
3
u/michaelpaoli Oct 13 '25
Selectively deny (or possibly allow) permissions from what would otherwise typically be the default(s). Sorry if you don't like it, but that's how it is.
They do ... in part. E.g. typically text editors will create files with rw but no x permissions, they typically presume it's desired to be able to read and write it (hey, just created it, perhaps with nothing there ... probably want to edit it since you did that in a text editor, eh?), but probably don't want to execute it - at least unintentionally, so, x off, and can set that on when one is ready or before intending to actually execute it (at least for the first time). Likewise some other programs that operating in security sensitive contexts may further adjust permissions appropriately (or more specifically, will typically set umask appropriate in their context before creating files/directories. E.g. one might observe the behavior of visudo and ssh-keygen, and how they may behave even if umask is/were set to 0. And, yeah, generally shouldn't be using a umask of 0.
Other examples that jump to mind, mkdir generally defaults to including x permissions on directory - can't cd into the directory or generally stat contents of directory without that (with some exceptions or partial exceptions regarding stat, for some filesystem types). And compilers for binary executables, will generally set the x permission on the output binary file - at least if the compilation was considered successful.
But for various reasons (mostly historical and backwards compatibility), *nix often starts with a umask of 0, and then things further restrict from there, by tightening up umask, e.g. typically 022 or 027 or 077 for login sessions. Many OS flavors will also, early, and by default, at least reasonably tighten up umask, e.g. kernel or init system may tighten it up to at least 022 highly early on.
But the programs aren't mind readers. They have no idea if you want to share that text file you're editing that you just created with the entire world, or if it's some super-secret content you want to share with exactly nobody else. Likewise regarding who you may or may not want to allow to write(/edit) it, e.g. just yourself, or also those having same group membership, or do you want/trust all on the host to write/edit such? So, ... umask (and chmod, etc.) - but umask is often quite preferable, as that effects the initial permissions at creation. Note that *nix checks permissions at the open (or attempt thereof), once the file is open (for reading or writing or whatever), permissions mostly no longer matter for the process that has the file open - so generally best to start with proper permissions - so again, umask, rather than, e.g. create file rwxrwxrwx, and then change permissions after - as that generally won't protect it from other processes that have already opened the file and still have it open.
What are these "base" ... "global" permissions you speak of? Processes have a umask value, starting at least with init, if not kernel itself before that. So, what "global" permissions?
Anyway, *nix, and again, backwards compatibility, history, etc. - generally starts with a relatively permissive model, and one can then generally further tighten that up with umask or relevant configurations thereof.
So, anyway, very much a mask, and think of it that way. Permission bits to be turned off with the file creation, that would or might otherwise have been on/enabled, were the umask not there (or set to 0, as "not there" really isn't an option - all processes have a umask value).
1
u/gordonmessmer Oct 13 '25
Because there's no guarantee that every program you use will allow you to specify the "base permissions" that you want.