44. File Permissions - chown, chgrp, chmod, umask
Contents
In order to make it easier to manage user and group permissions on files and directories, linux provides some common commands that can assign and reclaim permissions to users and groups in a more standardized way.
Change the Owner or Group of One or More Files or Directories
With the chown command, we can assign an owner or group to a file or directory.
Change Owner, Group or Both of a File or Directory
If you just want to specify the owner:
|
|
If you just want to specify the group:
|
|
Let’s create the file with the privileges of the root user, and then use the chown command to change the owner and group of the file to user:
|
|
Recursively Change Group of a Directory and its Subdirectories and Files
With the -R parameter provided by chown command, we can iteratively assign the same group to the directory, subdirectory, and all files in the directory:
|
|
Change the Group of One or More Files or Directories
We can specify only the group to which the file belongs through the chgrp command:
|
|
Change the Mode Bits of One or More Files or Directories
We can append, remove, or assign permission to users, groups, other people, or everyone else to read, write, or execute files or directories through the chmod command.
Let’s create a file for demonstration:
|
|
Change Permissions With Symbolic Mode Bits
Here is the link to Symbolic Mode Bits: 43. File Permissions
Change User Permissions With rwx
|
|
Change Group Permissions with rwx
|
|
Change Others Permissions with rwx
|
|
Change All Permissions with rwx
|
|
Change Permissions with Numeric Mode Bits
Here is the link to Numeric Mode Bits: 43. File Permissions
Change All Users Permissions with 7654321
|
|
umask
With the umask feature, we can preset permissions for new files. The permissions for a new file are obtained by subtracting the value of umask based on the maximum permission value of 0777.
Check umask in Numeric Value
The default output for umask is numeric mode bits:
|
|
Check umask in Symbolic Value
By adding the -S parameter, we can see it in symbolic mode bits:
|
|
Change the Value Through Numeric Mode Bits
Give No Permission to Anyone - 0777
|
|
The file’s permission is 0000(0+0+0,0+0+0,0+0+0,0+0+0), which happens to be the result of 0777 minus 0777.
Give All Permissions to Anyone - 0000
|
|
The file’s permission is 0666(0+0+0,4+2+0,4+2+0,4+2+0), which happens to be the result of 0777 minus 0111 (execute permission from owner, group and others).
Because this is a non-executable file, it is not given execution permission.
Change the Value Through Symbolic Mode Bits
Give No Permission to Anyone - ugo-rwx
|
|
The file’s permission is ‑‑‑‑‑‑‑‑‑(-+-+-,-+-+-,-+-+-,-+-+-), which happens to be the result of rwxrwxrwx minus rwxrwxrwx.
Give All Permissions to Anyone - ugo+rwx or ugo=rwx
|
|
The file’s permission is rw-rw-rw-(-+-+-,r+w+-,r+w+-,r+w+-), which happens to be the result of rwxrwxrwx minus ‑‑x‑‑x‑‑x (execute permission from owner, group and others).
Because this is a non-executable file, it is not given execution permission.
Restore the umask Value to the Initial Value
|
|
Impact on Non-Executable Files
We can create non-executable files in a number of ways, the three simplest ways are:
|
|
The file’s permission is 0644(0+0+0,4+2+0,4+0+0,4+0+0), which happens to be the result of 0777 minus 0022 and 0111 (execute permission from owner, group and others).
Impact on Executable Files
To create an executable file for demonstration, we need to install a compiler called gcc to compile the C language code:
|
|
Let’s create a file called test.c:
|
|
Let’s compile the test.c file through gcc, run it and check its permissions:
|
|
The binary file’s permission is 0755(0+0+0,4+2+1,4+0+1,4+0+1), which happens to be the result of 0777 minus 0022.
If No User is Specified When Setting Symbolic Mode Bits on Non-Executable Files
If no user is specified when the mode bit is specified, the newly created non-executable file is also granted write permission from the umask:
|
|
The file’s permission is 0755(0+0+0,4+2+1,4+0+1,4+0+1), which happens to be the result of 0777 minus 0022.
References 13.1 chown: Change file owner and group, 13.2 chgrp: Change group ownership, 13.3 chmod: Change access permissions, umask
Author Dong Chen
LastMod Sat Mar 16 2019