Why is 777 assigned to chmod to permit everything on a file?
I was asked in an interview why it is that 777 is assigned to take all permissions for a file. Why not 555? He said that there is reason for everything. So, what is the reason for 777? Why not any other number? Is there any significance in this number?
Solution 1:
I'll try to address the underlying reason why it is 777, rather than aaa, or 999.
Remember that permissions come in the following format:
u g o
rwx rwx rwx
where u=user, g=group, o=other.
Now, imagine you are representing each of these groups as binary. 1 is true, 0 is false.
If you want to give full access to everyone, you would assign the following permissions in binary:
u g o
rwx rwx rwx
111 111 111
Now, if you know binary, you will realise that when you convert 111
from binary to decimal, you get 7
.
Thus, you can represent full access as 777
.
Note: We are really converting from binary to octal. See the edit below.
This works for all the other access modes as well.
For instance, we can easily work out what 555
means by converting each 5
to binary, and writing it in the above format. 5
in binary is 101
, so we have the following permissions:
u g o
r-x r-x r-x
101 101 101
5 5 5
Similarly, if we want to give all permissions to the user, but only allow other people to read, we can find a number representation.
u g o
rwx r-- r--
111 100 100
7 4 4
Now, we know that 111
in binary is 7
in decimal, and 100
in binary is 4
in decimal. Thus, the permissions will be 744
.
Edit:
Technically, as highlighted by @LưuVĩnhPhúc and @Braiam, we are converting from binary to octal, as described below. However, the decimal and octal representations of numbers < 8 are the same, so for binary numbers with 3 digits or less, both decimal and octal representations are the same.
When represented as octal numbers, rather than splitting into groups of three, and doing binary to decimal conversion on each group, you can actually take all three groups together as a single binary number, and convert to octal.
For example, here are some binary to octal conversions:
0b111111111 == 0o777
0b101101101 == 0o555
0b111100100 == 0o744
Note that I am prepending "0b" and "0o" to distinguish between binary and octal numbers.
If you want to play around with this, open a terminal, run python
and then play around with the following commands:
oct(0b111111111)
bin(0o555)
Remember to prepend "0b" or "0o" to the numbers to let the computer know what base you are interested in. (If you don't, it will assume base 10.)
Solution 2:
Reading file permission means 4
, writing file permission means 2
and executing file permission means 1
.
So the total of this is 7
.
Now what is 777: first 7
is for file owner that means file owner have read, right and execute permission.
2nd 7
is for the group to which the file belongs, it means group also have all the read, write and execute permission.
And 3rd 7
is for others permission
If you give the file permission 555
then the file owner, group and others
have only read
and execute
permission not write
permission because read permission means 4 and execute means 1 so total gets 5
Solution 3:
In not so many words as the top answer:
Each file has 3 permissions options: read, write, and execute. You can choose none of these, one of these, two of these, or all of these:
C(3,0) + C(3,1) + C(3,2) + C(3,3) = 8
1 + 3 + 3 + 1 = 8
So, altogether, there are 8 combinations; 8 options for permissions. Counting from 0, the last number is 7 (from 0 to 7). So, represented by numbers, here are all the options:
0 - nothing
1 - execute
2 - write
4 - read
3 - execute + write (1 + 2)
5 - execute + read (1 + 4)
6 - write + read (2 + 4)
7 - execute + write + read (1 + 2 + 4)
There are three numbers because the order goes [user permissions][group permissions][others permissions]
So, 777 means that all three groups have read, write, and execute permissions.
Also (indirectly related so you don't necessarily have to read this part), because I think its relevance is important: Why is
read
number 4 instead of number 3?
0 - nothing
1 - execute
2 - write
3 - read
4 - execute + read (1 + 3)
5 - write + read (2 + 3)
6 - execute + write + read (3 + 2 + 1)
7 - ????? no way to get this with the 3 basic options (and we are missing execute + write)
The only way to get unique combinations for all of the possibilities is to go by powers of 2 for the basic options. 20 = 1 (execute), 21 = 2 (write), 22 = 4 (read), and if there were a 4th basic option it would be numbered 23 = 8. Note that write
is not listed until all combinations of previous options have been listed (which is just one option, since it is just execute
). read
is not listed until all combinations of previous options have been listed (again one, since there is only one combination with two options - execute
+ write
). execute
+ write
+ read
is not listed until all previous combinations have been listed, which is 3, since there are now two choices from three permissions. The list would continue in this manner, no matter how many basic options there were. For the sake of example, with 4 basic options (note that we also know there will be 16 combinations total since there are 4 options and 24 = 16):
0 - nothing
1 - execute
2 - write
4 - read
3 - execute + write (1 + 2)
5 - execute + read (1 + 4)
6 - write + read (2 + 4)
7 - execute + write + read (1 + 2 + 4)
8 - love
9 - execute + love (1 + 8)
10 - write + love (2 + 8)
11 - execute + write + love (1 + 2 + 8)
12 - read + love (4 + 8)
13 - execute + read + love (1 + 4 + 8)
14 - write + read + love (2 + 4 + 8)
15 - execute + write + read + love (1 + 2 + 4 + 8)