Summery: In this article you can learn about Linux File Permissions .
Having a good understanding about Linux file permissions is a great importance to your Linux carrier. In this article we are going to have an in-depth explanation on Linux file permissions.
There are two authorization levels in Linux world.
- File Ownership
- File Permission
Linux File Ownership
Each and every file, directory in your Linux system have 3 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 you are working on project where a number of people required 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’s and permissions in the system. Let’s say these three started a group project. Now they need same file permissions and ownership’s for the project files. So instead of assigning these permissions manually one by one to the three users, it’s easy to create a group with these three users and give the required permissions to the group. 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 has neither created the file, nor they belong to any groups. This group is also known as “World”.
Linux File Permissions
Same as the file ownerships, each file or directory in your Linux system have three types of file permissions. They are:
Read permission give you the authority to open and read 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?
First bit denotes the file type of file. There are about 7 file types in Linux.
– = regular file
d = Directory
l = Link
c = Special file/Device file
p = Named pipe
b= block device
Next three bits denotes the permissions of the owner of the file. Next three bits which follows the first 4 bits denotes the permissions of the groups and the last three bits denotes 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 an example.
In the above image first bit is a “-“ it means sample is a regular file. Next three bits are “rw-“ It means the owner of the sample file have read and write permissions. But the owner does not have executable permissions. Next three bits are “rw-” which means groups have the similar permissions as the owner of the file. Last three bits are “r–” It means Others only has read permissions to the file.
Also Read: How To Configure VPN on Ubuntu with OpenVPN
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.
Owner need 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 sub directories in it. Use “-R” together with the chmod command. This will recursively apply permissions to all sub folders and files.