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?
4 Answers
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
)
-
15Just to be clear -- the value you pass to the
chmod
command is always interpreted as octal. Usingchmod 888
will give an error.– mattdmDec 2, 2013 at 20:45 -
-
1A 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.)– alexisDec 2, 2013 at 23:35 -
So, in a C program, if you want to pass
1777
, you need to write01777
? Dec 3, 2013 at 17:36 -
1@Faheem, that's right. Octal
1777
is written01777
in C. (Or you could write1023
, which is the same number in decimal. But don't :-)– alexisDec 3, 2013 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.
-
3The 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.– jlliagreDec 2, 2013 at 21:16
-
-
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.– jlliagreDec 3, 2013 at 18:15
-
1
The interesting answer is:
- there is no difference between
0700
and700
, as explained in the other answers - but there is a difference between
00700
and0700
(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.
In Linux system there are two types permissions are available:
- File Permission
- 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.
mkdir()
. 0777 vs 777