If you’re working as Linux administrator or just started learning the basics of Linux, then it’s essential to understand how Linux file permissions work. In this article, I will detail everything you need to know about file permissions on Linux, including how to change file permissions and file ownership using the chmod and chown commands.

How do Linux file permissions work?On Linux, file permissions allow Linux admins to control the access level and ownership of files on the system. For this tutorial, I’ve been using a device running Ubuntu 22.04 LTS with sudo privileges.

The different permission types on LinuxUnderstanding how permissions groups work on Linux is important when you need to check the permissions of a file or a directory. To start this tutorial, create a directory named Documents in the home directory:

  • I’ll first login to my Ubuntu machine using my favorite SSH client, PuTTY.
  • In the home directory, create a directory named ‘Documents’ by running the command below.

mkdir Documents * After you’ve created the directory, run the following chmod command to change its access permissions. I’ll explain what the chmod 755 command does below

chmod 755 Documents/ * Next, you can list permissions for this directory and get other details about it using the ls -lh command.

ls -lh We get details about file permissions using the ls -lh command (Image credit: Petri/Sagar)Now, let’s analyze the attributes of the Documents directory (drwxr-xr-x)

  • The first character (d) is a file type (directory). However it can also have a ‘-‘ which represents a regular file, or a ‘c’ that represents a character device, or a ‘b’ that represents a block device.
  • After the directory (d), there are three triplets of permissions (rwx, r-x and r-x) which are associated to the owner of the file or directory, the group that has access to it, and the ‘others’ group, which designates everyone who isn’t the owner or a member of the group. Moreover, (-) indicates the absence of permissions.
  • Here, the owner of the file has read, write and execute permissions (‘rwx’), the group has read and execute (‘r-x’) permissions, and the ‘others’ group also has read and execute (‘r-x’) permissions.
  • ‘ec2-user’ is the owner of the documents directory, and the group name is also ec2-user, which is the primary group of the owner of the directory.

On Linux, the permissions for the owner, group, and ‘others’ can be constructed using r, w, x, or – where:

  • r indicates the read permission, and it means that the file/directory is readable.
  • w indicates the write permission, and it means that the file can be modified.
  • x indicates the execute permission, and it means that the file can be executed.
  • indicates the absence of permissions

Checking file permissions on Linux with the ls commandReading and understanding file permissions is important because if a file is assigned the wrong permissions, it may allow attackers to access it and potentially corrupt the entire operating system. In this section, I’ll create a new file in the home directory:

  • On the Ubuntu terminal in the home directory, run the command below to create and switch to the my_file directory.

mkdir my\_file * Next, create an empty text file named new_file using the touch command.

touch new\_file * Finally, run the ls -lh or ls -l command to check the permissions for the file that you’ve just created.

ls -lh There are three triplets of permissions for our file (Image credit: Petri/Sagar)Let’s analyze the results of this command:

  • Here, ‘new_file’ is the file name that exists under the home directory of the (ec2-user) user.
  • The first character (-) indicates that it’s a regular file.
  • After the first character (-), there are three triplets of permissions (‘rw-‘,’r–‘, and ‘r–‘) which belong to the owner, the group which has access to the file, and the ‘others’ group, respectively.
  • The owner has read and write permissions, while the group and ‘others’ are only allowed to read the file.
  • ‘ec2-user’ is the owner of the file, and the ec2-user is also the name of their primary group.
  • ‘0’ is the size of the file. The size is 0 because contents of the file is empty.

How to change file permissions on Linux with the chmod commandOn Linux, the chmod command can be used to change file permissions, and there are two different ways to do that: The symbolic (text) method and the numeric method. We’ve already used the symbolic method before, but let’s dive into details.

Symbolic (text) methodTo show you how to change file permissions with the symbolic method, let’s first create a file named file.txt in the home directory using the touch command.

touch file.txt * Next, we’ll review the permissions of the file.txt using the ls -lh command.

ls -lh We’re about to change permissions for this file with the chmod command (Image credit: Petri/Sagar)* Now, we’ll use the following chmod a-w command to remove the write permission from all the three permission groups (user, group and others).

chmod a-w file.txt * Now again, we’ll review the permissions of file.txt using the ls -lh command.

We’ve removed the write permission from all permission groups (Image credit: Petri/Sagar)This time, you can see that the write permission (w) has been removed from the user group, which is why we’re seeing ‘-r-‘ instead of ‘-rw’. The group and ‘others’ permission groups didn’t have the write permissions initially, so there’s no change on that front.

If we want to add the read, write and execute permissions to the user group, we can once again use the chmod command with ‘rwx’. The ‘u+’ in the command below indicates that we’re only changing permissions for the user group.

chmod u+rwx file.txt ls -lh Using the chmod command to add read, write and execute permissions (Image credit: Petri/Sagar)Similarly, If you need to subtract the read permission from the group that has access to the file, then consider running the command below where ‘g-‘ indicates that the permission is only removed from the group.

chmod g-r file.txtls -lh We used the chmod command to remove the read permission from the group (Image credit: Petri/Sagar)Numeric methodThe other way to use the chmod command to change file permissions is the numeric method. Here’s how the syntax works:

As I previously explained, ‘r’ represents the read permission and it has a 4 octal value. Moreover, ‘w’ stands for write and has a 2 octal value, while ‘e’ stands for execute and has a 1 octal value. So, if you use r+w+x in a command, the read, write, and execute permissions have an octal value of 7.

Let’s dive into an example and see how to change permissions for the file.txt file we’ve created earlier using the numeric method.

  • First, let’s verify again the permissions of the file.txt that we created earlier using the ls -lh command.

No changes to file permissions… yet (Image credit: Petri/Sagar)* Now, run the following chmod command will change the permission set of the file.

chmod 714 file.txtls -lh The chmod 714 command grants the read, write and execute permissions (7) to the owner of the file, the execute permission (1) to the group, and the ‘others’ group is granted both read and write access (4).

What are chmod 777, chmod 775 and chmod 755?As we learned earlier, using ‘7’ with the numeric method will assign all permissions (read, write, and execute) to a user group, whereas ‘5’ will only assign read and write permissions. The chmod 777, 775, and 755 commands are used quite often, and here’s what they do:

  • chmod 777 changes the permission of the file by granting the read, write, and execute permissions to the owner, group and others.
  • chmod 775 changes the permission of the file by granting the read, write, and execute permissions to the owner and the group that has access to the file, while the ‘others’ group is granted both read and write access.
  • chmod 755 changes the permission of the file by granting read, write and execute permissions to the owner, while the group and ‘others’ are granted both read and write access.

How to change file ownership in Linux with the chown commandIn the previous sections, we learned how to change the permissions of a file. However, if you need to change the ownership of a file or directory, then you should use the chown command.

To see how the chown command works, let’s run it on file.txt and follow up with the ls -lh command.

sudo chown -R root:root file.txt ls -lh Using the chown command to change ownership of a file (Image credit: Petri/Sagar).

After running this command, we can see that ‘root’ is the new owner of the file and the name of its primary group.

How to change groups ownership with the chgrp commandI just explained how to change the ownership of a file or directory using the chown command. However, if we want to just change the group membership of a file or directory, then we can use the chgrp command.

In the command below, we are changing the ownership of file.txt and transfer it from ‘root’ to ‘ec2-user’.

sudo chgrp ec2-user file.txt ls -lh We used the chrgp command to change the group ownership of the file (Image credit: Petri/Sagar)

Note: The chown and chgrp commands just change the permissions of files or directories, but they dont change any of the directory’s content.

ConclusionIn this tutorial, I explained everything you need to know about the different permission types on Linux. I also detailed how to use the chmod command to add and remove permissions from the different user groups, and how to use the chown and chgrp to change the group and ownership of files and directories. I hope that this knowledge will help you become a better Linux administrator.