Information
The user file-creation mode mask (umask) is use to determine the file permission for newly created directories and files. In Linux, the default permissions for any newly created directory is 0777 (rwxrwxrwx), and for any newly created file it is 0666 (rw-rw-rw-). The umask modifies the default Linux permissions by restricting (masking) these permissions. The umask is not simply subtracted, but is processed bitwise. Bits set in the umask are cleared in the resulting file mode.
umask can be set with either octal or Symbolic values:
Octal (Numeric) Value - Represented by either three or four digits. ie umask 0027 or umask 027. If a four digit umask is used, the first digit is ignored. The remaining three digits effect the resulting permissions for user, group, and world/other respectively.
Symbolic Value - Represented by a comma separated list for User u, group g, and world/other o. The permissions listed are not masked by umask. ie a umask set by umask u=rwx,g=rx,o= is the Symbolic equivalent of the Octal umask 027. This umask would set a newly created directory with file mode drwxr-x--- and a newly created file with file mode rw-r-----.
The default umask can be set to use the pam_umask module or in a System Wide Shell Configuration File. The user creating the directories or files has the discretion of changing the permissions via the chmod command, or choosing a different default umask by adding the umask command into a User Shell Configuration File, ( .bash_profile or .bashrc), in their home directory.
Setting the default umask:
pam_umask module:
will set the umask according to the system default in /etc/login.defs and user settings, solving the problem of different umask settings with different shells, display managers, remote sessions etc.
umask=<mask> value in the /etc/login.defs file is interpreted as Octal
Setting USERGROUPS_ENAB to yes in /etc/login.defs (default):
will enable setting of the umask group bits to be the same as owner bits. (examples: 022 -> 002, 077 -> 007) for non-root users, if the uid is the same as gid, and username is the same as the <primary group name>
userdel will remove the user's group if it contains no more members, and useradd will create by default a group with the name of the user
System Wide Shell Configuration File:
/etc/profile - used to set system wide environmental variables on users shells. The variables are sometimes the same ones that are in the .bash_profile, however this file is used to set an initial PATH or PS1 for all shell users of the system. is only executed for interactive login shells, or shells executed with the --login parameter.
/etc/profile.d - /etc/profile will execute the scripts within /etc/profile.d/*.sh. It is recommended to place your configuration in a shell script within /etc/profile.d to set your own system wide environmental variables.
/etc/bashrc - System wide version of .bashrc. In Fedora derived distributions, etc/bashrc also invokes /etc/profile.d/*.sh if non-login shell, but redirects output to /dev/null if non-interactive. Is only executed for interactive shells or if BASH_ENV is set to /etc/bashrc.
User Shell Configuration Files:
~/.bash_profile - Is executed to configure your shell before the initial command prompt. Is only read by login shells.
~/.bashrc - Is executed for interactive shells. only read by a shell that's both interactive and non-login
Rationale:
Setting a very secure default value for umask ensures that users make a conscious choice about their file permissions. A default umask setting of 077 causes files and directories created by users to not be readable by any other user on the system. A umask of 027 would make files and directories readable by users in the same Unix group, while a umask of 022 would make files readable by every user on the system.
Solution
Review /etc/bashrc, /etc/profile, and all files ending in *.sh in the /etc/profile.d/ directory and remove or edit all umask entries to follow local site policy. Any remaining entries should be: umask 027, umask u=rwx,g=rx,o= or more restrictive.
Configure umask in one of the following files:
A file in the /etc/profile.d/ directory ending in .sh
/etc/profile
/etc/bashrc
Example:
# vi /etc/profile.d/set_umask.sh
umask 027
Run the following command and remove or modify the umask of any returned files:
# grep -RPi '(^|^[^#]*)\s*umask\s+([0-7][0-7][01][0-7]\b|[0-7][0-7][0-7][0-6]\b|[0-7][01][0-7]\b|[0-7][0-7][0-6]\b|(u=[rwx]{0,3},)?(g=[rwx]{0,3},)?o=[rwx]+\b|(u=[rwx]{1,3},)?g=[^rx]{1,3}(,o=[rwx]{0,3})?\b)' /etc/login.defs /etc/profile* /etc/bashrc*
Follow one of the following methods to set the default user umask:
Edit /etc/login.defs and edit the UMASK and USERGROUPS_ENAB lines as follows:
UMASK 027
USERGROUPS_ENAB no
Edit the files /etc/pam.d/password-auth and /etc/pam.d/system-auth and add or edit the following:
session optional pam_umask.so
OR Configure umask in one of the following files:
A file in the /etc/profile.d/ directory ending in .sh
/etc/profile
/etc/bashrc
Example: /etc/profile.d/set_umask.sh
umask 027
Note: this method only applies to bash and shell. If other shells are supported on the system, it is recommended that their configuration files also are checked.
Default Value:
UMASK 022
Additional Information:
Other methods of setting a default user umask exist
If other methods are in use in your environment they should be audited
The default user umask can be overridden with a user specific umask
The user creating the directories or files has the discretion of changing the permissions:
Using the chmod command
Setting a different default umask by adding the umask command into a User Shell Configuration File, (.bashrc), in their home directory
Manually changing the umask for the duration of a login session by running the umask command