How to Change Permissions of a File in Linux


Linux is a popular operating system that permits users for creating and modifying files. Regardless, all users do not have similar permission for these files. Linux uses a system of permissions to manage who can read, write, as well as execute a file or directory.

If you are a Linux user, you may have encountered situations where you need to change the permissions of a file or a directory. For example, you may want to make a script executable or restrict access to your personal files from other users.

This post will explain the for changing the permissions of a file in Linux:

  • Using Symbolic Mode
  • Using Numeric Mode

Let’s start by discussing the types of users in Linux

Type of Users in Linux

  • File permissions in Linux is a set of rules that determine how to read, write, and execute a file. Moreover, each file or directory has three types of users associated with it:
  • Owner: A user who creates the file, or who owns it.
  • Group: The user to which the owner links.
  • Others: All users of the operating system.

File Permissions in Linux

Each type of user can have one or more of the below permissions:

  • Read (r): The permission for reading the contents/data of a file.
  • Write (w): The permission for modifying or deleting a file.
  • Execute (x): The ability for running a file as a script, or to enter a directory.

File permissions are presented by three bundles of three characters. For example, -rwxr-xr– means that:

  • The file is not a directory (indicated by -).
  • The owner has r, w, and x permissions.
  • After that, the group has r and x permissions.
  • In the end, the others have r permission.

Note: All the implementation of this article is performed in the Debian 11 Linux-based operating system.

How to Display the Permissions in Linux?

To present the permissions of a file as well as a directory, utilize the “ls -l” script:

ls -l


According to the given output:

  • The first column shows the permissions of each file and directory. The nine characters present the access for the owner, group, and others. The dash (-) refers to the fact that no permission is granted.
  • The second and third columns show the owner and group of every file.
  • The fourth one presents the size of each file.
  • The fifth one displays the date and time of the recent changes to each file.
  • The sixth column specifies the name of each file or directory.

Method 1: Using Symbolic Mode

The symbolic mode uses symbols to represent the permissions and operators to modify them. The operators are + for adding permission, – for removing permission, and = for setting permission. Let us practice some examples:

Example 1: Add Execute Permission

To add executing permission for all users to a file called “script.sh”, the user can use this command:

chmod a+x script.sh

Example 2: Remove Write Permission for Others

To remove write permission for others from a directory called “docs”, execute this command:

sudo chmod o-w docs

Example 3: Set Read and Write Permission Only

Next, to set read and write permission only for the owner and group of a file called “work.txt”, use this command:

chmod ug=rw,o= work.txt

Example 4: Change the Owner and Group of a Particular File

For changing the owner and group of a file called “work.txt” to root, users can utilize this script:

sudo chown linux:root work.txt

Example 5: Change Only the Group of a Directory

To change only the group of a directory called “docs” to admin, execute the below command:

sudo chown :root docs

Example 6: Change Only the Owner of a File

To change only the owner of a file called “script.sh” to linux_user, you can use this command:

sudo chown linux_user script.sh

Method 2: Using Numeric Mode

The numeric mode utilizes numbers to identify permissions. Each permission has a corresponding value: four is for the reading task, two is for the writing purpose, and one is for execution. So, to get the numeric value of a set of permissions, you add up the values of each permission.

For example, read and write permission is 4 + 2 = 6, read and execute permission is 4 + 1 = 5, and all permissions are 4 + 2 + 1 = 7.

Here are some other examples of changing the permission of a file using numeric mode.

Example 1: Read and Write Permission Only for the Owner and Group

To specify the numeric value of all three sets of permissions, users concatenate them in order: owner-group-others. For instance, set read and write permission only for the group as well as the owner of a file called “work.txt”, and execute the below script:

chmod 660 work.txt


This is equivalent to using symbolic mode with “ug=rw,o=”.

Example 2: Give Read, Write, and Execute Permission to Everyone

To give reading, writing, and executing permission to all for a file named “script.sh”, execute the below script:

sudo chmod 777 script.sh


This changes the permission of “script.sh” to -rwxrwxrwx.

Example 3: Read as well as Execute Permission

For assigning, reading, and executing grants to the owner and group, use this command:

sudo chmod 550 work.txt


This changes the permission of work.txt to -r-xr-x—.

Example 4: Assign Multiple Permission

To grant the owner read and write permission, the group read and execute permission, and the rest of the users execute permission for a file named script.sh, use the command below:

sudo chmod 754 script.sh


This changes the permission of “script.sh” to -rwxr-xr–.

Example 5: Read and Write Permission to the Owner and Group

Now, give read and write permissions to the owner and group and no permission to others for a file named work.txt, and set the permissions. As a result, the new files created in the same directory will inherit the group ownership:

sudo chmod 2660 work.txt


This changes the permission of work.txt to -rw-rwS—.

Conclusion

For changing the owner or group of a file, utilize the symbolic mode and numeric mode. They permit users to handle who can access files and directories. More specifically, by using commands like “ls -l”, “chmod”, and “chown”, users can easily view and modify permission. This article has explained methods for changing the permission of a file.

Print Friendly, PDF & Email
Categories