Site icon TechStrot

How to Change File Permissions in Linux?

how-to-change-file-permissions-in-linux

Linux is a multi-operating kernel supporting multiple users at a time. That’s why many users can share the same resource simultaneously on the same system. At the same time, you can control this sharing and access in Linux through permissions and ownership. There are three types of owners in Linux:

  1. User: The user is considered the creator and default owner of the file. That’s why the user is also called the owner of the file.
  2. Group: All members and users of a Linux group have the same Linux group permissions to access any folder or file.
  3. Others: The user not part of the above two categories comes in this category, i.e., the others.

The Linux file permissions are mainly categorized into three forms: read, write, and execute. You can also change these permissions as per your requirements. However, many beginners always look for these methods and end up getting errors. So in this blog, we will demonstrate how to change the file permissions in Linux correctly! So, off we go.

How to Change File Permissions in Linux?

Before changing the file permission in Linux, you should know about the current permission of the file. You can check the permissions of files and folders from a long list by running the following command in the terminal:

ls -l

The above output shows that the first character starts with “d,” representing the directory. If it begins with “-,” it refers to a file. The subsequent characters indicate the permissions of the file or directory.

These 9 characters are divided into users, groups, and others, each representing its permissions. Now you can change the permissions of the files according to the above output. In Linux, you can change the permissions in some simple ways, so let’s get a brief overview of them all.

Method 1 – The CLI Method

You can use the ‘chmod’ command to change the Linux file permissions, and here is the general syntax of this command:

chmod permissions < filename >

Note: You must be the owner of the directory or file to change its permissions.

You can also apply these permissions to subdirectories or within a directory with the -R flag:

chmod -R permissions < filename >

The permissions can be readable, writable, executable, or a combination of any of these. Now there are two modes of chmod to change Linux file permissions; absolute mode and symbolic mode. Let’s know each of them:

1. Absolute Mode

In the absolute mode, the numbers represent the permissions, which the mathematical operators modify. Under this mode, file permissions are displayed as three-digit octal numbers, not as characters. Here each digit represents permission. You can understand this better from the following table:

Number Symbol Permission Type
0 No permission
1 -x- Executable permission
2 -w- Writable permission
3 -wx Write + Execute
4 r– Readable permission
5 r-x Read + Execute
6 rw- Read + Write
7 rwx Read + Write + Execute

From the above table, we concluded that read, write, and execute permissions are represented by 4, 2, and 1, respectively. You can change Linux file permissions in absolute mode by following the below table:

Permissions  Number Representation Provide Permission Remove Permission
Read 4 +4 -4
Write 2 +2 -2
Execute 1 +1 -1

Let’s take an example where  we will assign read, write and execute all three permissions to the user, write and execute permissions to the group, and provide read permission to others:

Owner Read Write Execute Sum Total
User +4 +2 +1 7 731
Group +2 +1 3
Others +1 1

According to the above table, you have to perform calculations like this, and your chmod command will be as follows:

chmod 731 < filename>

Here, 731 is a three-digit octal number. Where, 

7 → Read + Write + Execute (for users)

3 → Write + Execute (for Groups)

1 → Execute (for others)

Similarly, you can also remove these permissions, and here is a basic example:

Permissions  Number Representation Remove Permission
Read 4 -4
Write 2 -2
Execute 1 -1

From the above example, we will remove the write permission from users and read permission from others. Its calculation table will be as follows:

Owner Read Write Execute Sum Total
User 4 2 – 2 1 5 530
Group 2 1 3
Others 1 – 1 0

To apply the above conditions, run the following chmod command in the terminal:

chmod 530 < filename >

2. Symbolic Mode

In this mode, we use the mathematical operators to assign or remove the Linux file permissions:

Let’s take an example where the current permission for Linux files is as follows:

-rw-r—-x

You will understand the above file condition more clearly using the following table:

Regular file type.
rw- The user has read and write permissions.
r– Group has only read permission.
–x Others have only execute permission.

Now we add write permission to the group, read permission to others, and remove write permission to users. So we change the file permission using the below command:

chmod u-w g+w o+r < filename >

In the above command, u, g, and o represent the users, groups, and others, in the respective order.

Special Permissions

‘Sticky bit’ and ‘Setuid and setgid’ are two special permissions related to Linux files, which are indicated by ‘t’ and ‘s,’ respectively.

Sticky bit Special Permissions

In a shared environment, you can apply sticky bit permissions to any file to set permissions for the file owner only to delete and rename files. Run the following command in the terminal to set this permission on any file:

chmod+t < filename >

Setuid and setgid Special Permissions

When you execute any program with the SUID bit enabled, you inherit the permissions of the owner of that program. Programs that do not have this SUID bit set to run with the permissions of the user who started the program. The same goes for SGID. Most programs are executed with group permissions, but this permission turns the program into a group owner.

Setuid and setgid permissions are used for assigning the system to run an executable as the owner with the owner’s permissions. To set the setuid and setgid permissions, use the below command:

chmod g+s < filename >

Note: Be careful using the above permissions because if they are wrongly assigned, it leaves your system vulnerable.

Method 2 – The GUI Way

You can easily check and change the Linux file permissions using the GUI, i.e., the graphical user interface method. Follow the below steps to check and change permissions in Linux:

1. First, go to the file whose Linux permissions you want to check and right-click on it, then select Properties.

2. On doing so, it opens a new window that contains basic information related to the file. Navigate to the Permissions tab, and you will get the following information:

3. You will see that the permission of the particular file is contained in three categories: user, group, and owner.

4. Here the owner can restrict or give access to users according to the different categories.

5. In the same way, you can change the permissions of subfolders directly from here. For this, click on ‘Change Permissions for Enclosed Files.’

6. After clicking on this, you can see the details below, and you can change all these permissions accordingly.

Bonus Tip

In addition to changing file permissions, you may also encounter a situation where you need to change user file ownership or group ownership. Through the ‘chown‘ command, you can also change the Linux file ownership in Linux. The general syntax to change the Linux file ownership is as follows:

chown user < filename >

Condition 1: Change User Ownership

Suppose the owner of a file is <xyz>, and we want to change its owner to <abc>. So we run the below chown command in the terminal:

chown abc < filename >

Condition 2: Change Group and User Ownership Simultaneously

To change group and user ownership simultaneously, you have to use the chown command followed by a colon before the group name like this:

chown user:group < filename >

Note that the owner’s name must come before the name of the group’s owner and must be separated by a colon.

Condition 3: Change Directory Ownership

Similarly, you can recursively change ownership for the contents in a directory. For this, run the following command in the terminal:

chown -R admin /opt/script

Condition 4: Change Group Owner

If you want to change the group owner, use the following version of the chown command:

chown :group name < filename >

Using a colon (:) before the group name indicates that it is the group’s owner. You can also use another command to change the group owner, which is:

chgrp group_name filename

Know how to create a shell script in Linux!

Conclusion

As a multi-user operating system, Linux sets permissions based on ownership to ensure the security of the directories and files of users. In Linux, you can modify and change the owner and permissions according to yourself. This blog shows how to change file permissions in Linux with the help of both GUI and terminal.

Exit mobile version