71

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?

2

4 Answers 4

102

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)

5
  • 15
    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, 2013 at 20:45
  • but each digit is octal, not the whole number itself. Dec 2, 2013 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, 2013 at 23:35
  • So, in a C program, if you want to pass 1777, you need to write 01777? Dec 3, 2013 at 17:36
  • 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, 2013 at 19:38
6

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.

6
  • 3
    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, 2013 at 21:16
  • You are right. Ergo, the "answer provided by alexis is wring. Dec 3, 2013 at 17:05
  • 2
    @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.". Dec 3, 2013 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, 2013 at 18:15
  • 1
    @jlliagre Ok, fair enough. Dec 3, 2013 at 19:39
2

The interesting answer is:

  • there is no difference between 0700 and 700, as explained in the other answers
  • but there is a difference between 00700 and 0700 (for directories)

At least that's the case for GNU coreutils' chmod version, which is the default on Linux.

See this example:

$ ls -ld mydir
drws--s--- 4 myuser mygroup 4096 Jul  8 09:27 mydir
$ chmod 0710 mydir ; ls -ld mydir                   # surprise ahead -- s-bits remain:
drws--s--- 4 myuser mygroup 4096 Jul  8 09:27 mydir
$ chmod 00710 mydir ; ls -ld mydir                  # _now_ they're gone:
drwx--x--- 4 myuser mygroup 4096 Jul  8 09:27 mydir

This is to avoid accidental deletion of setuid/setgid bits for directories. For details, see the chmod documentation.

1

In Linux system there are two types permissions are available:

  1. File Permission
  2. Special Permission

In File permission we set permission on files and folders:

The permissions are:

  • read(4)
  • write(2)
  • execute(1)

While in special permission three types of permission are:

  • SUID(4)
  • SGID(2)
  • Sticky Bit(1)

In your question you ask what is difference, so there is no any difference between chmod 777 and 0777 because there is no any octal value which show value of (0) zero.

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.