Here is the video version, if you prefer it:
Let’s talk about how to change the permissions of a file. To do so, let’s first talk about octal and binary numbers. Before you get all whiny and say “Oh jeez, why binary? Why do I need to know binary?”. Well, you don’t really, but I think it greatly simplifies things, so that’s why.
Let’s look at user permissions (same applies for group and world permissions). If you have:
rw-
that could be represented in binary as:
110
since binary has only two digits – 0
and 1
– so this could be a valid choice. We have a 1
where we have the right to modify the file in a certain way “enabled” and 0
where we have the right to modify the file in a certain way disabled.
Now, how do we read 110
in binary? Let’s first see how do we read it in decimal.
What you actually do is this: 110 is 0 * 10^0 + 1 * 10^1 + 1 * 10^2
(looking the digits from right to left, where ^
is the exponentiation operator). That is, the rightmost digit is the least significant one and it gets multiplied by 10^0
. The digit to the left of it is multiplied by 10^1
and so on. The same is in binary, but instead of using base 10
, we use base 2
. So 110
will be:
0 * 2^0 + 1 * 2^1 + 1 * 2^2 = 6
Now, let’s pay attention to the highest possible number with 3 digits in binary, which is 111
. By following the same logic 111
in binary is 7
in octal (1 * 2^0 + 1 * 2^1 + 2 * 2^2 = 7
). Note that octal has 8 digits – 0
through 7
.
So now, the problem reduces to: Figure out what octal digit represents the file permissions we want. Let me give you a list:
0
in octal –000
in binary1
in octal –001
in binary2
in octal –010
in binary3
in octal –011
in binary4
in octal –100
in binary5
in octal –101
in binary6
in octal –110
in binary7
in octal –111
in binary
When you look at binary representations of the octal numbers, you can see the correspondence. For example, 5
in octal is 101
in binary, which would look like r-x
, meaning we are able to read and execute the file in question.
Now we can come to chmod
. chmod
is used to change file permissions. It is used as follows:
chmod <filePermissions> <file>
where <filePermissions>
are 3 octal numbers in a row, each representing permissions for the user, the group and the world, respectively. Only the file owner or the superuser can change file permissions. (Shotts, 2019)
Example chmod
command:
mislav@mislavovo-racunalo:~/Linux_folder$ chmod 600 aba.txt
You can also use symbolic notation to change permissions (you can check out the man pages for chmod
or you can Google how to use symbolic notation with chmod
), but I believe that octal is easier to use once you understand it. I do use chmod +x file
to add executable permissions to a file and that is a fast way to do so, but whenever I need anything more that that, I use binary file permissions.
Hope you learned something useful!
References
Shotts, W. (2019). The Linux Command Line, Fifth Internet Edition. Retrieved from http://linuxcommand.org/tlcl.php. Pages 118-122