Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I saw a code change at work, where the mode values were changed from 777 to 0777 to make nfs setattr work. What is the difference in the 2 values?

share|improve this question

2 Answers 2

up vote 35 down vote accepted

If you're passing them to chmod (the command-line program), there is no difference. But in a C program or similar, 0777 is octal (three sets of three 1 bits, which is what you intend), while 777 is decimal, and it's quite a different bit pattern. (chmod will interpret any numeric argument as octal, hence no leading zero is necessary.)

0777 (octal)    == binary 0b 111 111 111    == permissions rwxrwxrwx   (== decimal 511)

777 (decimal) == binary 0b 1 100 001 001 == permissions sr----x--x (== octal 1411)

share|improve this answer
    
Oh, makes sense. Thank you. –  user1071840 Dec 2 '13 at 20:41
5  
Just to be clear -- the value you pass to the chmod command is always interpreted as octal. Using chmod 888 will give an error. –  mattdm Dec 2 '13 at 20:45
    
but each digit is octal, not the whole number itself. –  Drake Clarris Dec 2 '13 at 21:17
1  
A string of octal digits is an octal number. chmod interprets a numeric argument as octal, regardless of prefix. (But really the number itself has no function; just the digits, or rather the bits.) –  alexis Dec 2 '13 at 23:35
1  
@Faheem, that's right. Octal 1777 is written 01777 in C. (Or you could write 1023, which is the same number in decimal. But don't :-) –  alexis Dec 3 '13 at 19:38

The first bit is used for the sticky bit http://en.wikipedia.org/wiki/Sticky_bit

If you set permission using 4 digits, the first will set or remove this bit.

share|improve this answer
2  
The sticky bit is not the first bit but the third one. The first is the suid bit and the second the sgid one. If you set permissions using three digits, the missing first digit will also affect (i.e. remove) these three bits. –  jlliagre Dec 2 '13 at 21:16
    
You are right. Ergo, the "answer provided by alexis is wring. –  alfredocambera Dec 3 '13 at 17:05
1  
@jlliagre From the chmod man page - "Omitted digits are assumed to be leading zeros.". Also, the first digit of 4 is the sticky bit. From the man page again: "The first digit selects the set user ID (4) and set group ID (2) and restricted deletion or sticky (1) attributes. The second digit selects permissions for the user who owns the file: read (4), write (2), and execute (1); the third selects permissions for other users in the file's group, with the same values; and the fourth for other users not in the file's group, with the same values.". –  Faheem Mitha Dec 3 '13 at 17:40
1  
@FaheemMitha, omitted digits are assumed to be leading zeros thus will remove the aforementioned bits. Also, you seem to confuse bits and digits, I was commenting about the first bit, not the first digit. –  jlliagre Dec 3 '13 at 18:15
1  
@jlliagre Ok, fair enough. –  Faheem Mitha Dec 3 '13 at 19:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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