Having a good understanding of Linux file permissions is very important to your Linux carrier. In this article, we are going to have an in-depth explanation of Linux file permissions.
There are two authorization levels in the Linux world.
- File Ownership
- File Permission
Linux File Ownership
Every file, directory in your Linux system have three types of owners. They are:
User, also known as the owner of the is the one who creates the file or directory. Assume you created a text file called “sample” in your home directory. Then you will be the owner/user of that file.
In simple terms, a group is a collection of users. All users belonging to the same group will have the same access permissions to the file. These groups come in handy when working on project where a number of people require access to a file.
Let’s assume there are three users in a Linux system as alpha, beta, and gamma. These three of them have different file ownership and permissions in the system. Let’s say these three started a group project. Now they need the same file permissions and ownership for the project files. Instead of manually assigning these permissions one by one to the three users, it’s easy to create a group with these three users and give them the required permissions. so that every member inside that group will have the same access permissions to the project files.
Any other person except the owner and groups are categorized as Others. That means these guys have neither created the file, or they belong to any groups. This group is also known as “World.”
Also Read: How To Use grep Command In Linux With Regex
Linux File Permissions
Like the file ownership, each file or directory in your Linux system has three types of file permissions. They are:
Read permission give you the authority to open and read the file.
Write permission give you the authority to modify the content of a file.
Execute permission give you the authority to run a file as a program.
How to view the file permissions and ownership of a file or directory in Linux? You can view these by using the “ls” command together with the “-l” flag.
How to understand this weird code?
The first bit denotes the file type of file. There are about seven file types in Linux.
- – = regular file
- d = Directory
- l = Link
- c = Special file/Device file
- s= Socket
- p = Named pipe
- b= block device
Next three bits denote the permissions of the owner of the file. Next three bits that follows the first 4 bits denote the permissions of the groups and the last three bits denote the permissions related to others.
There are four possible characters in each triplet. They are:
- R = Read Permission
- W = Write Permission
- X = Execute Permission
- – = No Permission
Now let’s look at an example.
In the above image first bit is a “-“ it means the “sample” is a regular file. Next three bits are “rw-“ It means the owner of the sample file has read and write permissions. But the owner does not have executable permissions. Next three bits are “rw-” which means groups have similar permissions as the owner of the file. Last three bits are “r–” It means Others only has read permissions to the file.
Changing file/directory permissions with ‘chmod’ command
Let’s say you have a text file with all your social media credential saved in it. You do not want anyone to view this file. This can be achieved by changing file permissions.
Syntax of chmod command is:
chmod permissions <filename>
By the way, “chmod” stands for “Change Mode”.
So there are two ways to use chmod command.
- Absolute/Numeric mode
- Symbolic mode
Out of these two, Absolute/Numeric mode is the one that I personally confident and it is the mode that I use always. Here is how you use this mode. All the permissions are represented as a three-digit octal number.
So let’s say you have a file called “sample.txt.” You need to give read, write and execute permissions to the owner of the file, read permissions to the groups and no permissions to others. Now you need to do a little bit of math.
The owner needs to have read, write and executable permissions.
Read(4) + Write (2) + Execute (1) = 4+2+1= 7
Groups need read permissions
Read(4) + None(0) + None(0) = 4+0+0= 4
Others do not have any permissions
None(0) + None(0) + None(0) = 0+0+0= 0
So the file need to have “740” as it’s permission.
We can set using the following command
chmod 740 sample
If you are assigning permissions to a directory with subdirectories in it. Use “-R” together with the chmod command. This will recursively apply permissions to all subfolders and files.
Having a good understanding of Linux file permissions is very important when handling multi-user system for security. In this article I tried to explained file permission on Linux with examples so you guys can understand the concepts well. I hope you Hope you learned something new. Thank you for reading.