Linux File Permissions Explained

Linux is renowned for being a powerful and reliable operating system that offers unparalleled security, stability, and flexibility. In order to maintain the integrity and safety of the system, Linux implements specific settings in the form of “Permissions.” These permissions control who can access, modify, or execute the files and directories. Whether you are a new Linux user or a seasoned veteran, understanding Linux permissions is extremely important for a safe and secure system. In this article, we will dive deep into what Linux permissions are and how to keep your files and directories secured in the best way.

Understanding Linux Permissions

Understanding Linux Permissions

Linux file permissions are specific settings that allow users to control who can have access to their files. In Linux, each file or directory has a set of permissions that are distributed overthree levels of system users:

Each of these users hasthree types of file permissionsthat determine different types of actions that users can perform:

How to View File Permissions in Linux

How to View File Permissions in Linux

Now that you know what all file permissions exist in Linux, let us see how you can view the permissions granted to files. Viewing the file permissions is pretty easy, and you just need to use this command:

ls -l

Once you execute this command, you will see a list of all files and directories in the current location. Your output will look similar to the below screenshot:

Seems confusing? Let’s try to understand this with a sample example:

In the above example:

After simplifying the output ofls-lcommand, let’s expand on the permissions part with a sample permission set “rwxr-xr–“:

So, to sum up,the first 3 characterssignify the permissions for thefile owner; thesecond 3 characterssignify the permissions for the users in agroupand thelast 3 characterssignify the permissions forother users.

How to Change File Permissions in Linux

To change the file permission in Linux, we use the chmod command, which stands for “change mode”. The basic syntax to use the chmod command is:

chmod

Some of the command options that you can pair with chmod are:OptionDescription-vShows a diagnostic message for every file processed-cWorks like-vexcept it only shows a diagnostic message if any changes are made.-fUsed to suppress most error messages.-RUsed to change files and directories recursively.

For the part, you can use either of the two methods:

Absolute Mode in chmod

In this mode, the permissions are specified with a combination of 3-digit numbers from 1 to 7 (also known as octal numbers). Here, the first digit corresponds to the file owner, the second to the group users and the third corresponds to the other users. The basic syntax to specify permissions in absolute mode is:

chmod <permission_combination> <file_name>

The different combinations of numbers you can use for permissions are:Permission TypeNumberNo permission0Execute1Write2Write + Execute3 (i.e. 2+1)Read4Read + Execute5 (i.e. 4+1)Read + Write6 (i.e. 4+2)Read + Write + Execute7 (i.e. 4+2+1)

Let’s see an example to make this easier to understand. Say, you want to set read, write, and execute permissions for all users for the file “test.txt”, then use this command:

chmod -v 777 test.txt

This is a quite frequently used command in Linux systems, therefore, we have prepared an elaborate guide onwhat chmod 777 means in Linux.

  1. If you want to set read-only permission for the file owner and no permission for group and other users, you can use this command:

chmod -v 400 test.txt

Symbolic Mode in chmod

The main problem with the absolute mode is you always have to provide the permission set for all users even if you need to change for one user.

This is where the symbolic mode comes into play. The symbolic mode is the more commonly used as it uses alphabets instead of numbers, which most users find difficult to understand. In addition to being easy for users, you can also set permissions only for a specific user using the symbolic mode, unlike the absolute mode. The basic syntax to change file permission using symbolic mode in chmod is:

chmod <user_type><permission_set> <file_name>

In the above syntax:

In symbolic mode, you can specify the <user_types> as:

For the <permission_set> part, you can use either of the following combinations:Permission TypeSymbolNo permission—Execute-xWrite-w-Write + Execute-wxReadr-Read + Executer-xRead + Writerw-Read + Write + Executerwx

Let us now see some examples of how we can set permissions in Linux using symbolic mode. Suppose you want to set execute permission for the group, then use this command:

chmod -v g+x test.txt

You can even set multiple permissions for different user types. Say you want to remove execute permissions from the other user types and add read, write, and execute permissions for the file owner:

chmod o-x,u+rwx test.txt

Change File/Directory Owner and Group in Linux

Suppose you need to have all the permissions but do not want to share the permissions with the group users. In such a case you can use thechown(Change Owner) command to change the file owner. The syntax to change the owner is pretty straightforward:

chown <new_user_name> <filename_or_directory_name>

For example, if you want to change the owner to root for the file test.py, you can use this command:

sudo chown root test.py

Note: In order to change the file owner, you need to have root privilege. If you do not have root privilege then simply use the “sudo” command to get root permissions.

If you need to change the group for a file, use thechgrpcommand:

chgrp <new_group_name> <filename_or_directory_name>

For example, if you want to change the group to test, use the chgrp command:

sudo chgrp test test.txt

Note: No two groups can be the owners of the same file/directory. If you want to know how you can add a new user to a group, check out our guide onhow to add a new user to a group.

Managing file permissions is of utmost importance, especially in a multi-user environment. By regularly reviewing file permissions on your Linux system, you can ensure that your sensitive files stay safe and secure from prying eyes. We hope this article helps you understand this basic concept, and do let us know in the comments if you have any doubts.

Beebom Staff

Bringing the latest in technology, gaming, and entertainment is our superhero team of staff writers. They have a keen eye for latest stories, happenings, and even memes for tech enthusiasts.

Add new comment

Name

Email ID

Δ

01

02