DEV Community

Rajat Kumar Nayak
Rajat Kumar Nayak

Posted on

Linux Permissions

What are Linux Permissions?

Linux permissions, as the name implies, are a technique of granting different permissions to a person or group of users.

We'll go over how permissions are defined, how people are assigned permissions, and how they're used.

How to check permissions?

Run Command:
ls -a -l
The output of the command will be

Image description

Here you can see we have a directory and a file.
we will now focus on the first column which has complete information on the permissions.

The permission column has values of each ten characters.

Image description

The first place can be either a 'd' or an empty value.

'd' represents that it is a directory.

Different types of users:

  1. User: We can say that 'User' refers to the owner of the file or directory who has created the file or directory.

  2. Groups: It refers to a group of users who are assigned permissions to execute.

In production circumstances, groups are primarily employed. When building a project, several groups are formed based on the use case.

For a better understanding let us consider working on a project on management systems. Then we categorize users based on their role and assign permissions.

Some groups such as developers have the privilege of editing the source code and making appropriate changes.

Some users entitled as contributors can have limited access to some of the directories which are made public by the owner for review and corresponding changes.

In such scenarios, the owner adds users to groups, and permissions are assigned to the groups so that all the users entitled to the group have the privilege of exercising based on the permission.

Others: This category consists of all other users available and it has maximum possible risk and the directories and files are vulnerable to security threats and attacks.

Types of Permissions

  1. Read: As its name suggests it allows the user or group to read the content of the directory or file.

  2. Write: This permission allows the users to change the contents of the file and make changes.

  3. Execute: This permission allows the users or group to execute the contents of the file. Generally bash scripts that are designed to solve specific purpose are executed.

By default, the owner of the file has read and write permissions enabled.

Why the owner doesn't have the execute permission by default?

When we have a closer look at the 1st set of permissions then we can notice that the owner lacks the execute permission.

This is because not every file is meant for execution at first. So to prevent any accidental execution that can lead to unwanted circumstances the file doesn't have execute permission.

Based on our use case we can give execute permission to the user using the command chmod. We will look into detail how we can assign permissions.

Ways to represent permissions
There are two ways to represent permissions. The first one is to represent with acronyms.
r -> read
w -> write
x -> execute

The other way to represent permission is with octal number. Each permission has a numeric value assigned to it:

r (read): 4
w (write): 2
x (execute): 1

Denoting a permission
Some examples of permissions sets:

Suppose we want to give read and write and permission:

  1. Either we can use 'r', 'w', 'x' : rw
  2. read -> 4 write -> 2 That eventually leads to 6

Use Case 1:

For example, a file might have read, write, and execute permissions for its owner, and only read permission for all other users. That looks like this:

Owner: rwx = 4+2+1 = 7
Group: r-- = 4+0+0 = 4
Others: r-- = 4+0+0 = 4

The chmod Command
The chmod command can be used to either assign permission or remove permission from the user or the group.

+ in the command denotes that the mentioned permission is granted to the group or user.
- represents that the mentioned permission being removed from the user or group.

Some examples of using chmod command:

  1. Assign read and write permissions to the owner:

chmod u+rw filename

  1. Assign execute permissions to the group:

chmod g+x filename

  1. Assign read,write,execute to owner and read,write permission to group.

Way 1:
chmod u=rwx,g=rw filename

Way 2:
read -> 4
write -> 2
execute -> 1

chmod 760 filename

So we have pretty much covered the topic of permissions in Linux. If you have any doubt I will be more than happy to help. You can mail your queries to 'rajatnayak1582002@gmail.com'.

Top comments (0)