Set the default permission when creating a file

When I create a file, the permission is 644 (user rw, group r, other r).
How can I make the default file generated to have permission as 664?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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!

Solution 2

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 &.

Solution 3

Use umask.

For example, in your case:

umask 002

Solution 4

In former times (in the 1970s) people did write simple code that causes things to be hard to understand. This is what the other answers are related to.

If you are using a shell that is less than 30 years old, you may do this in a way that is easier to understand:

umask g+w

This is nicer since in this mode, the mask is inverted.

Background: the kernel interface still manages a mask that is applied to the third parameter of open().

Since 1988 (with ksh88) a new shell interface for the chmod command in the shell has been established.

umask -S

prints the file creation mode mask in the modern symbolic chmod syntax and

umask u=rw,g=rw,o=r

allows to set the file creation mask using the chmod syntax.

Solution 5

If you want to set the permission temperorily Use below command

Example: umask 022

In above case user have rw Permission
Group have read permission
Others have read permission

How it is calculated

for file we will calculate like this

Suppose if you want only read permission  calculated like this

since read permission value is 4 so we need to get value of 4 post subtracting

Totalvalue(6) - writevalue(2) =4(Read)

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply