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:
1
chown user file_for_user # This command will only specify the owner of the file file_for_user to user
If you just want to specify the group:
1
chown :user file_for_user # This command will only specify the group of the file file_for_user to user
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:
1
2
3
4
touch file_for_user
ll file_for_user
chown user:user file_for_user # To the left of the colon is the owner of the file, and to the right is the group to which the file belongs
ll file_for_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 owner and group to the directory, subdirectory, and all files in the directory:
1
2
3
4
5
mkdir -p a/b/c/d/e
tree a
ll a && ll a/b && ll a/b/c && ll a/b/c/d
chown -R :user a # This is equivalent to chgrp -R user a
ll a && ll a/b && ll a/b/c && ll a/b/c/d
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:
1
2
3
4
5
mkdir -p 1/2/3/4/5
tree 1
ll 1&& ll 1/2 && ll 1/2/3 && ll 1/2/3/4
chgrp -R user 1# This is equivalent to chown -R :user 1
ll 1&& ll 1/2 && ll 1/2/3 && ll 1/2/3/4
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.
ll file_permission
chmod u+rwx file_permission # This command appends the read, write and execute permissions to the owner of the file
ll file_permission
chmod u-w file_permission # This command removes the write permission from the owner of the file
ll file_permission
chmod u=rw file_permission # This command assigns the read and write permissions to the owner of the file
ll file_permission
Change Group Permissions with rwx
1
2
3
4
5
6
7
ll file_permission
chmod g+rwx file_permission # This command appends the read, write and execute permissions to the group of the file
ll file_permission
chmod g-w file_permission # This command removes the write permission from the group of the file
ll file_permission
chmod g=r file_permission # This command assigns the read permission to the group of the file
ll file_permission
Change Others Permissions with rwx
1
2
3
4
5
6
7
ll file_permission
chmod o+rwx file_permission # This command appends the read, write and execute permissions to others
ll file_permission
chmod o-w file_permission # This command removes the write permission from others
ll file_permission
chmod o=r file_permission # This command assigns the read permission to others
ll file_permission
Change All Permissions with rwx
1
2
3
4
5
6
7
ll file_permission
chmod a+rwx file_permission # This command appends the read, write and execute permissions to all
ll file_permission
chmod a-w file_permission # This command removes the write permission from all
ll file_permission
chmod u=rw,go=r file_permission # This command assigns the read and write permissions to owner, and only read permission to group and others
ll file_permission
ll file_permission
chmod 777 file_permission # This command assigns the read, write and execute permissions to the owner, group and others of the file
ll file_permission
chmod 666 file_permission # This command assigns the read and write permissions to the owner, group and others of the file
ll file_permission
chmod 555 file_permission # This command assigns the read and execute permissions to the owner, group and others of the file
ll file_permission
chmod 444 file_permission # This command assigns the read permission to the owner, group and others of the file
ll file_permission
chmod 333 file_permission # This command assigns the write and execute permissions to the owner, group and others of the file
ll file_permission
chmod 222 file_permission # This command assigns the write permission to the owner, group and others of the file
ll file_permission
chmod 111 file_permission # This command assigns the execute permission to the owner, group and others of the file
ll file_permission
chmod 644 file_permission # This command assigns the read and write permissions to the owner, and read and write permissions to the group and others
ll file_permission
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:
1
umask
Check umask in Symbolic Value
The default output for umask is symbolic mode bits:
1
umask -S
Change the Value Through Numeric Mode Bits
Give No Permission to Anyone - 0777
1
2
3
4
5
umask0777# This is equivalent to 777 or ugo-rwxumaskumask -S
touch new_file_without_any_permission_num
ll new_file_without_any_permission_num
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
1
2
3
4
5
umask0000# This is equivalent to 000 or ugo+rwx or ugo=rwxumaskumask -S
touch new_file_with_every_permissions_num
ll new_file_with_every_permissions_num
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
1
2
3
4
5
umask ugo-rwx # This is equivalent to 777 or 0777umaskumask -S
touch new_file_without_any_permission_sym
ll new_file_without_any_permission_sym
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
1
2
3
4
5
umask ugo+rwx # This is equivalent to 000 or 0000 or ugo=rwxumaskumask -S
touch new_file_with_every_permissions_sym
ll new_file_with_every_permissions_sym
The file’s permission is rw-rw-rw-(-+-+-,r+w+-,r+w+-,r+w+-), which happens to be the result of rwxrwxrwx minus –r–r–r (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
1
2
3
umask0022umaskumask -S
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: