jayemef
6th October 2004, 05:58 AM
There are many questions on these forums relating to file permissions, which is what inspired me to write this up. The following is not crucial to know, nor is it a how-to by any stretch of the imagination. It's just something I personally find interesting, and you may too. Now, to go on...
Many of you have probably used the chmod command at some point in your linux experience. If you haven't and you're not familiar with it, it's a command used to change the permissions of a file or directory. When using it, one way to express the desired permissions is to enter a number, which the system then recognizes as read, write, and execute. Now, where do these numbers come from? Yes, they're in the chmod man pages, but why? Was 7 randomly selected to represent rwx? It is the lucky number afterall. But the answer is no.
A brief intro to binary
The answer is that the number entered is an octal representation of a series of binary numbers. If you don't know how binary works, it's not hard. It's actually essentually the same as the system we commonly use today - decimal. Number systems all have a base number, which is equal to the range of values you can represent. Decimal has a base of ten (range of 0-9, which is 10 values). When we use the number 254, we are actually interpreting it as (4x10)^0 + (5x10)^1 + (2x10)^2, or 4 ones plus 5 tens plus 2 hundreds. Well, binary is only different in that it is a base 2 system. The number 13 is actually (1x2)^0 + (0x2)^1 + (1x2)^2 + (1x2)^3, or 1 + 4 + 8. This may seem confusing, but it's actually pretty easy when you just think of what each place represents, ie, the first place is 1, second 2, third 4, fourth 8, etc.
Now this binary is all well and good, but what does it have to do with file permissions? Well, let me give an example. When setting up my fstab to mount network shares, I had made a credentials file to go with it that would contain my username and password so that, should someone look at my fstab, the information wouldn't be right there. Now, of course the path to that file IS there, so I'd have to change the permissions of that file to keep others from viewing it. So what I did was, after creating my credentials file, I used the command
chmod 600 auth.smb
Ah, now there's one of those numbers I was talking about. This particular command changes the file permissions to "-rw-------", meaning that only root can read or write to the file. But once again I ask, where does that 600 come from? Well, another good thing about binary is that it is perfect for representing true(1) and false(0). So let's think of permissions as true or false and take a second look. The only true cases here are for root, being read and write. So read and write can now be thought of as 1's. The remaining is executable, which is false, so that becomes 0. Put those together and we have 110, which, in binary, is (0x2)^0 + (1x2)^1 + (1x2)^2, or 6. Since all the rest of the permissions are false, or 0, they are all zero, and we get 600.
Now, going back to what I said before, this 600 isn't binary. After all, it has a six in it! It's the octal representation of binary. Octal is a number system with radix 3. The radix is the power at which 2 needs to be raised to get the base. The octal base is 8 (range of 0-7), so 2^3=8. This matters because when converting binary to another number system, you take x bits, or effectively digits, of the binary number at a time, where x is the radix. So to convert our binary permissions number, which is 110000000, we first group it into threes, since x in this case is 3. Thus we get 110 000 000. Now all we have to do is represent those numbers in their new form. Octal ranges from 0 to 7, so we can say 110 is 6 and 000 is just 0. Thus the binary 110000000 is 600 in octal. MUCH easier to represent!
So lets look at the form:
Octal: 0 6 4 0
Binary: 000 110 100 000
Symbolic: sst rwx rwx rwx (for special, user, group, other)
(or in this example, --- rw- r-- ---)
So there you have it. I hope this was enlightening and interesting as well. I certainly think it is. Now we know where those seemingly arbitrary numbers are coming from. As I said, you can get away without this knowledge, but don't you feel better knowing it?
:)
Many of you have probably used the chmod command at some point in your linux experience. If you haven't and you're not familiar with it, it's a command used to change the permissions of a file or directory. When using it, one way to express the desired permissions is to enter a number, which the system then recognizes as read, write, and execute. Now, where do these numbers come from? Yes, they're in the chmod man pages, but why? Was 7 randomly selected to represent rwx? It is the lucky number afterall. But the answer is no.
A brief intro to binary
The answer is that the number entered is an octal representation of a series of binary numbers. If you don't know how binary works, it's not hard. It's actually essentually the same as the system we commonly use today - decimal. Number systems all have a base number, which is equal to the range of values you can represent. Decimal has a base of ten (range of 0-9, which is 10 values). When we use the number 254, we are actually interpreting it as (4x10)^0 + (5x10)^1 + (2x10)^2, or 4 ones plus 5 tens plus 2 hundreds. Well, binary is only different in that it is a base 2 system. The number 13 is actually (1x2)^0 + (0x2)^1 + (1x2)^2 + (1x2)^3, or 1 + 4 + 8. This may seem confusing, but it's actually pretty easy when you just think of what each place represents, ie, the first place is 1, second 2, third 4, fourth 8, etc.
Now this binary is all well and good, but what does it have to do with file permissions? Well, let me give an example. When setting up my fstab to mount network shares, I had made a credentials file to go with it that would contain my username and password so that, should someone look at my fstab, the information wouldn't be right there. Now, of course the path to that file IS there, so I'd have to change the permissions of that file to keep others from viewing it. So what I did was, after creating my credentials file, I used the command
chmod 600 auth.smb
Ah, now there's one of those numbers I was talking about. This particular command changes the file permissions to "-rw-------", meaning that only root can read or write to the file. But once again I ask, where does that 600 come from? Well, another good thing about binary is that it is perfect for representing true(1) and false(0). So let's think of permissions as true or false and take a second look. The only true cases here are for root, being read and write. So read and write can now be thought of as 1's. The remaining is executable, which is false, so that becomes 0. Put those together and we have 110, which, in binary, is (0x2)^0 + (1x2)^1 + (1x2)^2, or 6. Since all the rest of the permissions are false, or 0, they are all zero, and we get 600.
Now, going back to what I said before, this 600 isn't binary. After all, it has a six in it! It's the octal representation of binary. Octal is a number system with radix 3. The radix is the power at which 2 needs to be raised to get the base. The octal base is 8 (range of 0-7), so 2^3=8. This matters because when converting binary to another number system, you take x bits, or effectively digits, of the binary number at a time, where x is the radix. So to convert our binary permissions number, which is 110000000, we first group it into threes, since x in this case is 3. Thus we get 110 000 000. Now all we have to do is represent those numbers in their new form. Octal ranges from 0 to 7, so we can say 110 is 6 and 000 is just 0. Thus the binary 110000000 is 600 in octal. MUCH easier to represent!
So lets look at the form:
Octal: 0 6 4 0
Binary: 000 110 100 000
Symbolic: sst rwx rwx rwx (for special, user, group, other)
(or in this example, --- rw- r-- ---)
So there you have it. I hope this was enlightening and interesting as well. I certainly think it is. Now we know where those seemingly arbitrary numbers are coming from. As I said, you can get away without this knowledge, but don't you feel better knowing it?
:)