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?
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
Thus, you can represent full access as
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
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:
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.)
Reading file permission means
4, writing file permission means
2 and executing file permission means
So the total of this is
Now what is 777: first
7 is for file owner that means file owner have read, right and execute permission.
7 is for the group to which the file belongs, it means group also have all the read, write and execute permission.
7 is for others permission
If you give the file permission
555 then the file
owner, group and others have only
not write permission because read permission means 4 and execute means 1 so total gets
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
readnumber 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
read is not listed until all combinations of previous options have been listed (again one, since there is only one combination with two options -
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)
For some reason, UNIX designers decided to use OCTAL numbers for file permissions. As you know, a maximum value for one-digit octal number is 7. It turned out that one octal digit for user access, one for the group access and one for the world access is enough for nearly everything. Maximum 3digit octal number is 777, and it only makes sense that it denotes "access to everybody/everything".
Nowadays, we all know that a byte is 8 bits: this has been universally agreed upon for decades. But that wasn't always the case, and Unix (which inspired Linux in many ways) was written during a time when this was still being debated. In particular, it needed to be portable to systems that used 6-bit bytes or 8-bit bytes. Some of the people who wrote it were on one side of the debate, and others were on the other side.
On a related note, Base-2 (binary) is not a very convenient notation for writing out values. Nowadays, most programmers write out a more compact notation that uses Base-16 (hexadecimal) instead. 16 is just big enough of a base that you could exactly pack four bits into one hex-digit: for example, "0000" in binary is 0x0 in hexadecimal (that "0x" is a common way to note that you're about to write a hexadecimal number), while "1111" is 0xF (or 15 in decimal). You can actually write any possible combination of four bits using a single hex digit, just by counting in binary, and because of the way positional arithmetic works, you can stack it up: two hex digits can encode any possible combination of eight bits, just by counting, and so on. So the 8-bit folks loved this.
The 6-bit folks had their own way of doing this, but instead of using Base-16, they used Base-8 (octal). It has similar advantages to hexadecimal: you can store any position of three bits in an octal-digit, and you can stack digits up in a similar way. So just like the 8-bit folks used two hex-digits for a byte, the 6-bit folks used two octal-digits for a byte. You don't see octal very much anymore, but it was typically noted with a leading zero: for example, "111" is 07 in octal.
Now, what does all of this have to do with Unix permissions? As far as Unix was concerned, there are three things you could do with a file: you could read it, you could write to it, or you could execute it as a program. If you're going to restrict that with permissions, then you need a bit for each of them: turn it on for things that someone is allowed to do, and leave it off for things that someone is not allowed to do. Since there are three things being tracked, you need three bits, and since Unix tracks it along three lines (owner, group, and everyone) you need nine bits total.
At some point along the line, someone -probably in the 6-bit camp- said "Hey, we can use octal-digits for this". And this turned out to be a very convenient notation: three octal-digits is just enough to encode every possible combination of the bit fields. Once they decided to do this, 777's fate (and 000's) were sealed, because those numbers would be the same no matter how they arranged the bits, but order mattered for all of the other numbers, so they set about doing this.
They arranged the permissions into three-bit fields: read at the start, write in the middle, and execute at the end. Then they arranged the fields themselves: owner at the start, group in the middle, and others at the end. Once they'd done this, all they had to do to assign the rest of the numbers was count.
Because they're 3-bit fields, you can say that each octal-digit controls one of the fields: the first digit controls owner permissions, the second digit controls group permissions, and the third digit controls other permissions. Thus, 777 (111 111 111) is all permissions for everyone, while 700 (111 000 000) is all permissions only for the owner. Other combinations are also common: 666 (110 110 110) is read/write for everyone but not execute), while 555 (101 101 101) is read/execute for everyone but not write, and 400 (100 000 000) is read-only for the owner and no access for anyone else.
And that's why 777 means all permissions. These days, it's probably the most popular reason for people to use octal at all, though Unix and its descendants still have a few other vestiges of it. For example,
od, or Octal Dump, is a way to get binary dumps of a file in octal form (it has a hexadecimal cousin, called
xxd, but that's not as well-known and isn't available everywhere). It's also why you need to be careful with leading zeroes in some programming languages, because they might think that you intend to write numbers in octal when that's not really your intent.