Search posts

Linux file permission

In Linux the file permissions are divided to 3, read permission, write permission and execute permission, it is also divided to this 3 entity, the user that is owner of the file, the group owner of the file and other.
The permissions have:
1 is the execute permission – in binary 001
2 is the write permission – in binary 010
4 is the read permission – in binary 100

We can added the number to have more than one permission, for example if we want read and write permission, then we add 4 (read permission) and 2 (write permission) 4+2=6, and 6 is read and write permission together.
we can also combine the binary bit, so:

010
100
---
110

And 110 is 4 in binary.

Read and execute will be 1 (execute permission) and 4 (read permission), 1+4 =5 and so on.
If the number is zero then it mean that we have no permission.

The first number is the user permission, the second is the group permission, and the third is the other permission (every one who is not the user, or belong to the group of the file).

The first ‘- ‘ is for the file type, if it is ‘-‘ then it mean that it is a regular file.
‘d’ – mean directory.
‘l’ – mean soft link.

After the file type we have the permission, in the following order:
user [read, write, execute] group[read, write, execute] other[read, write, execute]
So the first triple is for the user permission, and we ‘rwx’. That mean that the user who own the file has all 3 permission. The same go for the group. The last is for other, and that is everyone that is not included who is the user or belong to the group. In the other triple we have ‘r-x’, so instead of the ‘w’, we have ‘-‘, and that mean that other has NO write permission. so when we have – in the triple of the permission, then that mean, that we don’t have that permission (the ‘we’ depend in the triple, first – user, second – group, third – other). If the permission exist then we have the letter that represent the permission:
r – for reading.
w – for writing.
x – for executing.

In order to change permission, we can use the chmod command.
The structure is the following:chmod <to-who u(user)|g(group)|o(other)> <action = + -> <permission wrx> <file name>
For example, say we have the following:

So the ‘o’ mean other, this is because we want to change permission for other.
The ‘-‘ mean to remove permission. The ‘rx’ mean read (r) and execute (x). and last the file name.
So it mean that we want to remove the r and execute permission from other.
And indeed, in the ls -l after the command, we see only ‘—‘ for the last triple, meaning other has no permission at all. Before they had read and execute permission.

If we want to add permission, then we need to use the ‘+’, which mean add permission.
For example, say we would like to give other all the permission, on the file from the last example:

And as we can see now the permission of other is only read and execute.

We can get the same effect with number.
The number for read is 4 and for execute is 1, together they are 5. So the number for read and execute is 5.
If we want to change the permission for all (user, group and other), to be only read and execute: