In Linux, there are many files and directories that belong to different users and different groups, but how to check permissions for those files? So, for this, there is a command that lists down the files and directories with their permissions and owners.
The command is -
ls -l
For example -
🤔 But again, there's a question what is written there before any filename or directory name?
The answer is that it denotes the type (is it file or directory) with their permissions such as users, groups and others
Let's break it down -
drwxr-xr-x 1 root root 4096 Apr 11 08:31 apt
First letter d
means it is a directory and if it is file then it will be denoted with -
sign
For example -
-rw-r--r-- 1 root root 98 Nov 22 03:45 shells.state
Now, the next 9 letters are divided into three categories
Owners, Groups and Others and that rwx
letters indicates
r - read
w - write
x - execute
For example -
Now, all permissions, such as read, write, and execute have their own Octal Coding System. It means they have assigned with octal codes
Permission Symbol Value
Read r 4
Write w 2
Execute x 1
If we are assigning the permissions to users, groups or others with the help of octal values then we have to sum up their values.
For example, I want to assign full permission to users, groups and others then, the values will be -
4 (Read) + 2 (Write) + 1 (Execute) = 7 For Users
4 (Read) + 2 (Write) + 1 (Execute) = 7 For Groups
4 (Read) + 2 (Write) + 1 (Execute) = 7 For Others
Then, the final code will be 777.
2nd Example - I only want to assign read permission to users and groups then, the values will be -
4 (Read) + 0 (Write) + 0 (Execute) = 4 For Users
4 (Read) + 0 (Write) + 0 (Execute) = 4 For Groups
0 (Read) + 0 (Write) + 0 (Execute) = 0 For Others
Then, the final code will be 440
3rd Example - I want to assign execute permission to users and read permission to groups then, the values will be -
0 (Read) + 0 (Write) + 1 (Execute) = 1 For Users
4 (Read) + 0 (Write) + 0 (Execute) = 4 For Groups
0 (Read) + 0 (Write) + 0 (Execute) = 0 For Others
Then, the final code will be 140
Now, how to change the file permissions?
For example- The File shown below has some permission
-rw-r--r-- 1 root root 0 Apr 11 14:00 sample
I want to change the file permission and want to assign execute permission to groups and others
So, the Octal code will be -
0 (Read) + 0 (Write) + 0 (Execute) = 0 For Users
0 (Read) + 0 (Write) + 1 (Execute) = 1 For Groups
0 (Read) + 0 (Write) + 1 (Execute) = 1 For Others
The final code will be 011
Command to change file permissions -
chmod <octal_code> <filename/dir. name>
chmod 011 sample
After giving the command, the permissions have changed
------x--x 1 root root 0 Apr 11 14:00 sample