umask command is used to set default file and folder permission in Linux operating system. File and folder permissions are important because the permission enables or deny different users, groups and others to access, read, write and execute of the given file. In this tutorial, we will learn how to use
umask command to set default read, write and execute permissions.
The syntax of the
umask command is very simple where we just provide the permissions.
Linux is a file-based operating system where there are 3 permissions.
- `r` means read where reading given file or folder is specified
- `x` means execute where execution of the given file specified. As folders can not be executed we can not use this permission for folders or directories.
- `w` means write where writing or appending to the specified file or folder.
The permissions has also numeric representation like below.
If we want to specify multiple permission we need to sum up their numeric representation and line symbolic representation like below.
|read value +||write value +||execute value =||numeric
For example, If we want o make given file read and executable bu not writeable we can use numeric 5 or symbolic rx.
Linux operating systems use 3 category user types like owner, group and others
- `owner` is the user who owns the file completely. This user generally has all rights like read, write and execute. But in some cases to prevent accidents some permissions can be changed or removed.
- `group` is the group owns the given file or directory. For example, `sudoer` group owns some administrative files where they can use them according to their permissions.
- `other` means all other users except owner and group. This can be useful for entities that do not have any relationship with the given file. Generally, we can make a file do not have any read, write or execute permission to a file for other users. But in some cases, we may provide the read permission.
We have learned the default user and permission usage for Linux operating systems.
umask command uses these users and permission a bit different. Actually, permission values are working a bit different. We do a bitwise operation on the permission values in order to use with
umask. For example, in order to set 775 for default permission, we need to provide 002 to the umask command. This can create some problems and errors during umask command usage but we can check with the
-S option the regular permission values of the current files and folders.
$ umask $ umask -S
We can see that
0022 is equal to user rwx, group rx and others rx.
umask Permission Digit Presentation
We can use the following table to set umask permission digit presentation.
|umask digit||default file permissions||default directory permissions|
|7||(no permission allowed)||(no permission allowed)|
Print Current Permission In Numerical Format
We can print currently active umask permissions which will show file and folder default permissions like below.
Print Current Permission In Symbol Format
We can use
-S option with the umask command in order to list the current file and folder permissions.
$ umask -S
Now we will set the file and folder default permissions with the umask command. As learned previously we will use umask type permissions. In this example, we will set the permission user read+write+execute , group read+execute and others none.
$ umask 027
Another way is using following command syntax
$ umask u=rwx,g=rx,o=
We can also delete or remove currently existing permissions to the owner, group or others. We will use
- and provide the user and permission. In this example, we will remove group execution permission.
g is used for the group and
x is used for executing.
$ umask g-x
PHP umask Function
PHP is a Linux based programming language where is provides native Linux functions. PHP provides umask function which can be used to list and set default file and folder permissions.
In this example, we will set the current file and folder permission with the
<?php umask(0077); ?>
We can also print current file and folder permissions without providing any parameter to the PHP umask() function like below.
<?php umask(); ?>