What is the file permission in Linux & how to use it?

In this article, you will learn about what is the file permission in Linux & how to use it?

Many users can access the Linux and Unix-like operating systems simultaneously. if anyone can use the computer so he or she can see the files. It creates a security vulnerability. So if you want to remove this security concern, and make sure who can find and use that data, permissions come in the line. Consequently, The Linux operating system uses two kinds of authorization, number one ownership, and number two permission.

Ownership of a file or directory in Linux

There can be three types of ownership of a file or a directory. So, you can get a detailed explanation of them.

User

Whoever creates a file or directory, he/she becomes the owner of that file or directory. The owner is also known as a user.

Group

A group consists of adding more than two users in the same group. All users have the same access permission as the group. So the file access permission will be the same for every user in that group. If you want to give access to a file to many users, in that case, you can create a group of users. And assign the access permission to that group—no need to add one by one to all users.

Other

These are the users who are not creators (owners) nor group users, which means everybody else.


Permissions of a file or directory in Linux

In Linux, there are three types of permissions for a file or directory, which are based on the above three owners, i.e. user, group, and other.  So let’s discuss file permission in Linux.

Read permission

The read permission will give you to open and read the file. If you have read permission on any directory then you can list the content of that directory.

Write permission

The write permission allows the user to modify the contents of the file. If you have  ‘write‘ permission on a directory then you can add, remove and rename the files which are stored in that directory. For instance, you have a file on which you have ‘write‘ permission, but you do not have ‘write‘ permission on the directory in which that file located then you can only modify the contents of the file, but you can not rename, remove or move that file from that directory.

Execute permission

If you have the execute permission, then only you can execute the programs. For instance, you do not have the execute permission then you can only modify or look the program code because of ‘read‘ and write permission but you can not run that program.

Note: If you are the owner of the file or directory then you can allow the permissions to all, like a user, group, and other.

file permission in linux

In the above picture, you can see that I have created a file with name demofile using the touch command and also created a directory demodirectory using the mkdir commandand then I have listed them using ls -l command. 

The demodirectory is starting with drwxr-xr-x, where stands for the directory.

The demofile is starting with -rw-r–r–, where ‘-‘  (hyphen) stands for a file type.

And the other letters represent,

r = read permission

w = write permission

x = execute permission

– = no permission

file permission in linux

In the above picture, you can see that represents permission on the owner which is rw- means the owner has read and write permission. But, No execute permission.

The number represents permission on the group which is r– means the group has only read permission.

The number represents permission on the other which is r– means the other has also only read permission.

Changing the permission of file or directory using the chmod command

So, if you want to change the file or directory permissions then there is a command which is known as chmod command. The chmod is the short form of change mode. Using this command you can change the read, write, and execute permission for the owner, group, and others.

There are two modes to use the command to change the file permission in Linux.

  1. Symbolic mode
  2. Absolute mode

Symbolic mode

Symbolic mode represents changes to files’ permissions as operations on single-character symbols. They allow you to modify either all or selected parts of files’ permissions, optionally based on their previous values.

There are three symbols are used in symbolic mode,

  1. + (plus), we use a plus symbol to add the permission to the file or directory
  2. (minus), we use a minus symbol to remove the permission from the file or directory.
  3. = (assign), we use the equal symbol to set the new permission or override the permission.

The different users represent as below

u represent user or owner, g represents group, o represent other and a represent all. All means user, group, and others.

How to use symbolic permission

Example 1: We are going to give the execute permission to the owner (user) section, So for user and for execute permission then the command will look like the below.

chmod u+x demofile

So, here the demofile is the file name. If you long-list using ls -l command then you can see the permission on this demofile.

Adding the execute permission

Example 2: So, I’m going to give the full permission means to read, write, and execute to the group section. Then the command will be g for group and rwx for ‘read’, ‘write’, and ‘execute’.

chmod g+rwx demofile
file permission in linux
Giving the full permission to the group

Example 3: Now, I’m removing read permission from the other section. So, I have to write o for others and r for ‘read‘ and then the filename. You can see in the below command.

chmod o-r demofile
file permission in linux
Removing the only read permission from the other

Absolute mode or Numeric mode

In numeric mode, we use numbers to represent file permissions. When you change permissions by using the absolute mode, represent permissions for each triplet by an octal mode number. It is also known as clubbed permission or octal method. Generally, sysadmin prefers this method. The table below lists the octal values for setting the file permissions in the absolute mode.

Octal Value File Permissions Set Permissions Description 
0No permissions
1–xExecute permission only
2-w-Write permission only
3-wxWrite and execute permissions
4r–Read permission only
5r-xRead and execute permissions
6rw-Read and write permissions
7rwxRead, write, and execute permissions

r = read = 4
w = write = 2
x = execute = 1

For example: Suppose you want to give read and write permission in the user section and only execute permission in the group and other sections then you can write like below.

chmod 611 file_name

So, you can find the below description for this command.

  • user = read + write + no execute = 4 + 2 + 0 = 6
  • group = no read + no write + execute = 0 + 0 + 1 = 1
  • others = no read + no write + execute = 0 + 0 + 1 = 1

Changing the ownership or group

If you want to change the Ownership of the file or directory then you have to use below command,

chown owner_name:group_name file_name

I’m going to change the owner of the demofile from root to user sahil, so the command will look like below,

chown sahil:root demofile

As a result, you can look in the below picture.

file permission in linux
Changing the ownership of the file from root to user sahil

To change the owner of file and group also you can type like below,

chown sahil:sahil demofile

Here, the Owner will be sahil and the group name will be sahil. Also, you can create a new group and you can give permission for ownership to that group. So, all the members of that group can access the file according to the permission set for that group. Also, you can look at the below picture for better understanding.

Changing the owner and group.

Note: In case you want to change group-owner only, use the command.

chgrp group_name filename

chgrp stands for change group.

Conclusion

In conclusion, you learned about file permission in Linux. But, this is important to know about permission so make sure you do practical on this. I hope, you understand file permission in Linux but if you have any questions, you can ask in the comment section.

Also, you can read about

Leave a Reply