Linux umask Command Tutorial with Examples, Numeric and Symbolic Representations – POFTUT

Linux umask Command Tutorial with Examples, Numeric and Symbolic Representations


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.

Syntax

The syntax of the umask command is very simple where we just provide the permissions.

umask PERMISSIONS

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.

number permission
4 read
2 write
1 execute

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

value:

symbolic equivalent:
0 0 0 0
0 0 1 1 x
0 2 0 2 w
0 2 1 3 wx
4 0 0 4 r
4 0 1 5 rx
4 2 0 6 rw
4 2 1 7 rwx

For example, If we want o make given file read and executable bu not writeable we can use numeric 5 or symbolic rx.

Users

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.

Permission Values

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
Permission Values
Permission Values

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
0 rw rwx
1 rw rw
2 r rx
3 r r
4 w wx
5 w w
6 x x
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.

$ umask
Print Current Permission In Numerical Format
Print Current Permission In Numerical Format

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
Print Current Permission In Symbol Format
Print Current Permission In Symbol Format

Set Permission

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
Set Permission
Set Permission

Another way is using following command syntax

$ umask u=rwx,g=rx,o=

Delete Permission

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 0077 permission.

<?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();

?>

LEARN MORE  How To Disable SELinux Temporarily or Permanently in CentOS, RHEL, Fedora

Leave a Comment