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.

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.

numberpermission
4read
2write
1execute

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:
11x
22w
213wx
44r
415rx
426rw
4217rwx

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.

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 digitdefault file permissionsdefault directory permissions
rwrwx
1rwrw
2rrx
3rr
4wwx
5ww
6xx
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 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.

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.

Set Permission

Set Permission

Another way is using following command syntax

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.

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.

We can also print current file and folder permissions without providing any parameter to the PHP umask() function like below.

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)