File Permissions aka Access Modes in Linux

Hey there ππ»! In this blog, we will deep dive into the Linux file permissions and how to change the mode of a file or directory using chmod command in Linux.
Sometimes, we may encounter some issues like when opening a file or a directory it says permission denied. This is because you don't have read access to those. Or sometimes we can't write to a file because we don't have write permission and if unable to execute a file like a script (.sh) file then we can say we don't have execute permission to that file.
To change file permission(or change mode) , you must be the owner of a file or directory.
Introduction
In Linux, we use ls -l to list all the information of files and directories in long list format i.e -l.
$ ls -l
-rw-r--r-- 1 <USER_NAME> 1857 Jun 11 02:22 test.txt
mode owner size date file/folder name
-rw-r--r-- is the file permission. Permission uses a total of 10 bits. The first bit tells about its kind. For example, if it is - then it's a normal file and if d then it's a directory. And rest bits are for read, write and execute permissions for different owners, each has 3 bits. Here, rw- for user, r-- for group and r-- for others (follow this sequence). Note, we first write read(r), write(w) and then execute(x).
File Ownership
We have three categories for owners of a file - User, Group, and Others.
User- Default owner.Group- Collection of users. A user belonging to a group would have the same permission as other members in the group.Others- Users that are not a part ofUserandGroupcome under this category.
File Permission or Access Mode
We already saw that there are three permissions for an owner - r, w and x.
r- Open and read file.w- Modify file.x- Run an executable file.
Ways to Change Mode using chmod
There are two ways to change modes/permissions:
1. Symbolic mode
Users, groups and others are represented by u, g and o respectively.
And we use +, - and = operators to modify permissions.
+ -> Adds permissions, - -> Removes permissions, = -> Sets permissions
Let's understand this with some examples.
Syntax - chmode <permission> <filename>
From the above example -rw-r--r--, let's say we want to add write w permission to group g owner.
chmod g+w test.txt
-rw-r--r-- β‘ -rw-rw-r--
To remove r read permission from others o.
chmod o-r test.txt
-rw-rw-r-- β‘ -rw-rw----
To add write w and execute x permissions to other o.
chmod o+wx test.txt
-rw-rw-r-- β‘ -rw-rw-rwx
To set only read r permission to g.
chmod g=wx test.txt
-rw-rw-rwx β‘ -rw-wx-rwx
We can also combine the commands in a single command -
chmod u+x,o+wx,g=wx test.txt
-rw-r--r-- β‘ -rwx-wxrwx
2. Numeric mode
In this mode, we represent permissions in numbers. 4, 2, 1 -> r, w, x respectively.
| read(r) | write(w) | execute(x) |
|---|---|---|
| 4 | 2 | 1 |
-rw-r---x, to represent this in numeric format, add 4 (r) and 2(w) for user, add 4 (r) for group, and only add 1 (x) for others, so 6->u, 4->g and 1->o. So, 641 is the numeric mode of the permission. We want to add write and execute permissions to group and execute permission to user. So we'll add 2 (for write) and 1 (for execute) to group i.e; (2+1) is added to 4 (current value for group) and add 1 (for execute) to user. So 771 is now the numeric mode of the permission.
To apply it, we use the same command.
chmod 771 test.txt
-rw-r---x β‘ -rwxrwx--x
Thanks for reading this articleπ. If you liked it, please do share!




