To set the sticky bit on a directory, why do the commands chmod 1777 and chmod 3777 both work?
2 Answers
1 1 1 1 1 1 1 1 1 1 1 1
___________ __________ __________ ___ ___ ___ ___ ___ ___ ___ ___ ___
setUID bit setGID bit sticky bit user group others
Each number (also referred to as an octal because it is base8) in that grouping represents 3 bits. If you turn it into binary it makes it a lot easier.
1 = 0 0 1
3 = 0 1 1
5 = 1 0 1
7 = 1 1 1
So if you did 1777, 3777, 5777, or 7777 you would set the sticky bit because the third column would be a 1. However, with 3777, 5777, and 7777 you are additionally setting other bits (SUID for the first column, and SGID for the second column).
Conversely, any other number in that spot (up to the maximum of 7) would not set the sticky bit because the last column wouldn't be a 1 or "on."
2 = 0 1 0
4 = 1 0 0
6 = 1 1 0
-
3+1 for a nice description of how octal numbers work and how it applies to the file permission bits.user– user2013-02-08 14:44:23 +00:00Commented Feb 8, 2013 at 14:44
-
1It's called "bitmask", and +1 also for explaining/showing how it can set and
clearthe Owner Group & Other columns.Krista K– Krista K2013-12-30 21:07:35 +00:00Commented Dec 30, 2013 at 21:07
The permissions passed as an argument to chmod are specified as an octal value. Each numeral in the value represents three bits. If three numerals are given, you're setting the read, write and execute bits for the file's owner, group and others (everyone else). If four numerals are given, the leftmost number sets the setuid, setgid and sticky bits. Octal 1 sets the sticky bit. Octal 2 sets the setgid bit. Octal 2 + octal 1 is octal 3 which sets both the setgid bit and the sticky bit.
-
1Isn't it octal 2 | octal 1 rather than octal 2 + octal 1? The operations happen to have the same result in this case, but in general it's a bitwise or that matters, isn't it?gerrit– gerrit2013-02-08 09:06:53 +00:00Commented Feb 8, 2013 at 9:06
-
1@gerrit Yes, in the general case you should be looking at the
binary oroperator. However, as you point out, in this case it works out to the same result, and plenty more people are familiar with addition.user– user2013-02-08 14:44:01 +00:00Commented Feb 8, 2013 at 14:44
passwdbinary were world-writable, you wouldn't be able to get root access by modifying it, as you say, but you could replace it with some other binary that everyone would run thereafter, thinking it waspasswd.