My umask is 0022
When I create directory using
mkdir mydir
the permissions I get are drwxr-xr-x
However when I create file
touch file
the permissions I get are -rw-r--r--
Why do directories get x(search) permission by default whereas files don't? How are the default settings brought into force? Is there a way to change them (I understand it may not be a good idea to just change them). Just want to experiment.
The
umask
has nothing to say about what permissions will be set. Rather, it only says which permissions will never be set if a tool/utility tries to set them. This is why it is only a 'mask'If you set
umask 0000
then you will see what the native file permissions are for various utilities; they are not always the same as you have noticed.First off "x" means different things, depending. For files, it means "allow this to be executed" and for directories, it means "allow access", that is, allow someone to cd into it. I'm glossing over some finer points here. It does not, however, mean "searchable".
Generally speaking, for regular users on a modern linux system, the umask is set to be 0002 (in octal). This means that this mask is used when calculating the effective permissions. The starting place for that is different for directories and files. Making a directory defaults to 0777 in octal. The default for files depends on how the open() call was used by the program, but touch, for example, uses 0666 in octal. So if your umask is set to 0002, you end up with 0664 for files, and 0775 for directories, corresponding to rw-rw-r-- for files and drwxrwxr-x for directories.
You can play with setting the umask using the
umask
command. I would suggest a careful reading of the chmod(1) man page, too, and definitely look up how the sticky-bit and suid and sgid bits come into play, since these are often where a lot of people get confused.