How to Manage file permissions on Ubuntu Server 20.04

Under Linux (and UNIX), everything in the file system, including directories and devices, is a file. And every file on your system has an accompanying set of permissions based on ownership. These permissions provide data security by giving specific permission settings to every single item denoting who may read, write, or execute the file. These permissions are set individually for the file’s owner, for members of the group the file belongs to, and for all others on the system.

Step 1 : The ls command then reports on the file, displaying the following (from left to right):

  • 1. The type of file created
  • 2. Permissions : Read, write, and execute permissions may be assigned for the owner, group, and all others on the system.
  • 3. Number of links to the file
  • 4. The owner : This is the account that owns the file; it is originally the file creator, but you can change this designation by using the chown command.
  • 5. The group : This is the group of users allowed to access the file; it is originally the file creator’s main group, but you can change this designation by using the chgrp command.
  • 6. File size
  • 7. creation/modification date
  • 8. File name

Step 2 : Under Linux, permissions are grouped by owner, group, and others, with read, write, and execute permission assigned to each, as follows

  • r : open and read the file.
  • w : open and write to the file.
  • x : execute the file

Many users prefer to use numeric codes, based on octal (base 8) values, to represent permissions. Here’s what these values mean:

  • 4 indicates read permission
  • 2 indicates write permission
  • 1 indicates execute permission

In octal notation, the previous example file has a permission setting of 644 (read + write or 4 + 2, read-only or 4, read-only or 4)

Step 3 : chgrp command

# change the group of "dir" to root
sudo chgrp -R root dir

Step 4 : chmod command

# To set the executable permission
sudo chmod +r filename
# To set write permission
sudo chmod +w filename
# To set more selective permissions with additional parameters before mode expression. u sets the permission for user, g for group, and o for all others
sudo chmod ugo+x filename
# use the Octal format to specify permissions
sudo chmod 777 filename
# To remove permissions, replace + with -.
sudo chmod o-w filename

Step 5 : chown command : You can use the command chown to change the ownership of such files and assign them to respective users.

# To change ownership of a file, use the following command:
sudo chown newuser filename
# To change the owner as well as the group of file, use the following command:
sudo chown newuser:newgroup filename
# You can skip changing owner and change only the group with the following command
sudo chown :newgroup filename

Add Comment