Linux Commands Ubuntu

How to Change File Permissions and Ownership in Linux/Ubuntu?

How to Change File Permissions and Ownership in Linux Ubuntu(1)

Linux/Ubuntu is a multi-user Operating System, i.e., multiple users can share system resources at once. Linux controls access to these shared system resources by managing the Permission and ownership of files and directories. This article will demonstrate ways of changing permissions and ownership of files on Linux/Ubuntu 22.04 LTS as below:

  • Changing File Permissions.

Changing File Ownership.

How to Change File Permissions in Linux/Ubuntu?

In this section, we will discuss file permissions in Linux/Ubuntu systems. For example, file permissions of “SampleFile.txt” can checked by:

$ ls -l

From the above image, we can see that the file permission of file “SampleFile.txt” is “-rw-rw-r–”. The first set is the user permission, the second set is of the group of users’ permission, and the third set consists of other users’ permissions as shown below:

There are three types of owners:

  • User(u): Owner of the file.
  • Group(g): Collection of users. In Linux, permissions are assigned in bulk, i.e., all users in the group have permission to access a file.
  • Other(o): Any user that is not part of the above two groups (i.e., everyone else).

Additionally, there are three permission categories in each owner category:

  • Read (r): Read permissions grants the read access to the user, i.e. the user can access the files and see the content.
  • Write (w): Write permissions that allow the user to edit, save, and delete files.
  • Execute (x): Execute permissions allow the user to run the file.

The chmod command is frequently used for changing the file permissions. The general syntax of chmod command is

$ chmod <permissions> <filename>

where:

  • Permissions: File permissions, i.e., read, write, and executable permissions, or a combination of these permissions for a user group.
  • Filename: Name of the file whose permissions are required to be changed.

The file permissions can be changed by the following methods using chmod command:

  • Symbolic Mode
  • Absolute Mode

How to Change File Permissions Using Symbolic Mode?

In chmod command, we use a combination of “file owners”, i.e., user(u), group(g), others(o), and mathematical operator in <permissions> while using symbolic code:

  • +: Adds permission to a file.
  • : Remove a certain permission.
  • =: Assigns the permission to the file.

For example, below is an example of changing user permissions of a SampleFile.txt:

$ chmod u+x SampleFile.txt

From the above image, it can be seen that user permission is modified from “-rw-rw-r–” to “-rwxrw-r–”, i.e., execution permissions are added to file owner (u).

How to Change File Permissions Using Absolute Mode?

In this, Real Numbers represent permissions. In absolute mode, permissions are added by adding 4, 2,1 for read, write, and execute respectively while permissions are revoked by subtracting 4, 2, 1 respectively for each file owner, i.e., the user(u), group(g), others(o). For example, we can view the file permissions of “SampleFile.txt” as follows:

In the above image, the permission of “SampleFile.txt” is “-rwxrw-r–”. We will change file permission to “-rw-rw-r–” by running the following command:

$ chmod 664 SampleFile.txt

From the above image, the permission is changed to “-rw-rw-r–”, i.e., the execution permission for file owner (u) is revoked. Additionally, 664 is calculated by:

  • The first digit (6) is the sum of read (4) and write (2) permission for the user.
  • The second digit (6) is the sum of read (4) and write (2) permission for the users of the group.
  • The third digit (4) is the sum of read (4) permission for others.

How to Change File Ownership in Linux?

File ownership is modified using the chown command. The general syntax of chown is

$ sudo chown <<strong>user</strong>> <<strong>filename</strong>>

Where

  • user: the name of the user.
  • filename: The name of the file in String.

In our case, the ownership of a file “SampleFile.txt” will be changed. We can check the current ownership by running the following command:

$ ls -l

In the above image, we can see the file “SampleFile.txt” is owned by “linuxuser”. The file ownership can be changed from “linuxuser” to “root” user by following the command:

$ sudo chown root SampleFile.txt

The above image indicates that the owner of “SampleFile.txt” is modified from “linuxuser” to “root”

Conclusion

To change file permissions and ownership in Linux/Ubuntu, use the “chmod <permissions> <filename>” and “sudo chown <user> <filename>” commands. This article demonstrated two ways of changing permissions, i.e., by using symbolic mode and by using absolute mode. Additionally, changing the ownership of files on Linux/Ubuntu 22.04 LTS is also discussed.

 

Similar Posts