56

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?

9
  • 5
    take a look at help.ubuntu.com/community/FilePermissions May 27, 2014 at 8:43
  • 2
    Refer @AvinashRaj's link comment & apply it on to the instance. The question "Why?" depends on what instance it is & what purpose that it serves... :)
    – AzkerM
    May 27, 2014 at 8:58
  • 1
    Is he asking about the significance of the number 7 vs 5 or why they are numbers instead of, say, letters?
    – Braiam
    May 27, 2014 at 12:22
  • 4
    This is a great interview question; I may have to steal it. May 27, 2014 at 14:56
  • 5
    Strictly speaking, it's not 777, but 0777.
    – Ruslan
    May 30, 2014 at 8:36

6 Answers 6

132

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.)

9
  • 17
    Just for completeness sake, the format of the numbers are called octals which literally means base of 8 (since they start from 0 is 7+1=8).
    – Braiam
    May 27, 2014 at 14:35
  • 3
    davie: octal is easy because it actually groups three bits, just as hexadecimal groups four. Decimal is not a power of 2, so it doesn't group bits as nicely.
    – Konerak
    May 28, 2014 at 7:10
  • 2
    Well, you get the same number in either case. I think it's more likely that someone will understand a binary to decimal conversion than a binary to octal conversion. Also, I have added a section at the end to clarify that the numbers are really octal, not decimal.
    – daviewales
    May 28, 2014 at 9:17
  • 3
    7 is the same in decimal and octal - however, 777 is not. When you're talking about flipping bits, it makes a difference.
    – Sam Dufel
    May 30, 2014 at 22:13
  • 2
    Just for trivial sake 7 is referred to as God's Number and the root user, who's default permissions are 777 is sometimes referred to as God.
    – eyoung100
    May 31, 2014 at 0:04
17

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

2
  • What is group and what are those other permissions? Thanks for response.
    – hellodear
    May 27, 2014 at 9:09
  • Please refer this link for group and the other permissions, these will be enforced on anybody else on the server that is either not you or not in your group May 27, 2014 at 9:36
7

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)
2
  • 6
    This answer is utterly bad as it fails to recognise the binary and, more importantly, octal number system as source for Unix file permission bits. (Funnily enough, they're not 3 or even 4… they're 6, although the full 18 bit are not used currently.)
    – mirabilos
    May 28, 2014 at 18:05
  • 1
    The base is unimportant, as 111 is 7; whether I say that the permissions is 111 or 7 does not matter, because it is the same value. I used decimal for simplicity of example to show the math involved. I could have used binary, but the math would not have looked as straightforward. What does it matter when the bases can be translated between one another? May 28, 2014 at 19:23
4

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.

3

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".

3
  • I don't think this is a good explanation. UNIX designers could have decimal numbers and would still have ended up with 7 denoting maximum access, rather than 9 (as the maximum decimal digit). The 7 is not chosen "subtractively" based upon what numbers are possible starting at the highest possible single digit, but "additively" based upon the access allowed by the single binary digits starting at zero for no rights at all. Jun 1, 2014 at 12:59
  • Notice I wrote at the very beggining "for some reason"... - I did not want to go into deeper explanation about WHY they picked octal numbers, as that was not the OP question. The question was why 777 gives all permissions.
    – DejanLekic
    Jun 2, 2014 at 8:25
  • It is irrelevant here why they picked octal numbers. My point is that the reason that maximum rights are represented by the number 7 is not that 7 happens to be the maximum octal number. Jun 2, 2014 at 10:03
2

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.

2
  • first of all when linux was created (circa 1992) 8-bit 6-bit debate was WAY over. 2nd that's NOT the reason why this was done Jun 1, 2014 at 23:54
  • 1
    I provided the 8-bit/6-bit stuff for historical context. Linux was created long after that debate ended, it's true, but because it pulled so much of its inspiration from Unix, it was affected by that debate in similar ways. This is why I thought it would be a good idea to introduce the historical context. Jun 2, 2014 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.