Set the default permission when creating a file

There is a process flag in Unix/Linux called umask, interpreted as an octal number it says which permissions to take off a newly created file. The flag is inherited by child processes. Setting it for the shell makes all processes started later to use this value.

Permissions as bits are 1 for x, 2 for w and 4 for r, which can be combined into an octal digit. For example, r-x is 4 + 1 = 5. There are 3 sets of permissions (user, group, others). So the 664 is rw-rw-r--.

When creating a "normal" file, base permissions are 666 (nicely daemonic, isn't it?), when creating an executable it is 777. From this the umask is taken off, 002 would give 664 for a regular file.

The shells have builtin commands called umask to set the umask. You can run them interactively, but most often they are executed in some startup file for the shell to make it permanent. In the case of bash(1), this should be done in ~/.bashrc. In Fedora it is set system-wide in /etc/bashrc.

Before changing the default setting, carefully consider implications, as relaxing permissions could allow undesirable access to your files. Allowing anybody in your group writing your files is risky!


If you want to change default umask to all user, add this line:

umask 0002

to /etc/profile.

Although any user can override this value by setting their own in ~/.bashrc.

To calculate file permission generated by umask value, consider this formula:

generated_permission = (NOT umask) AND default_permission

where NOT and AND is logical bitwise operator ~ and &.


Use umask.

For example, in your case:

umask 002