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:
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:
Now, if you know binary, you will realise that when you convert
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
Similarly, if we want to give all permissions to the user, but only allow other people to read, we can find a number representation.
Now, we know that
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:
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
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
So the total of this is
Now what is 777: first
If you give the file permission
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:
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
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
I'm surprised so many similar answers that completely miss the mark.
After determining the 3 classes that were needed read, write and execute. They went with octal (3 bits) to minimize the space needed for managing the file system.
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,
Others have given very good technical explanations of what does the number mean to the computer, just to gloss over them, 7 = 4(read) + 2(write) + 1 (execute) and the three digits mean this : 7(owner)7(group)7(guest)
But if that was the only condition, then the number could have easily been 3(0+1+2), or 6(1+2+3).
I have a very strong feeling that the choice of 777 is a very well hidden joke put in by the people who set it as a convention, as 777 is also known as the "god's number" (the same way 666 is "the devil's number"). It supposedly represents the triumph of god's number (7) over the devil's number (666).
And only god can protect a file which has 777 as its permissions.
Reference : http://en.wikipedia.org/wiki/777_(number)