What is the difference between "chmod +x" and "chmod 755"?
When it comes to making a file executable what is the difference between chmod 755
and chmod +x
and when would I use which? I so far have only used chmod +x
and I just read something and it used chmod 755
and I could not tell whether or not it was better to use chmod 755
or chmod +x
.
Short version:
To be able to compare them, we should look at them from the same perspective, so:
-
chmod +x
is equal tochmod ugo+x
(Based onumask
value) -
chmod 755
is equal tochmod u=rwx,go=rx
Explanation:
Firstly you should know that:
-
+
means add this permission to the other permissions that the file already has. -
=
means ignore all permissions, set them exactly as I provide.- So all of the "read, write, execute, sticky bit, suid and guid" will be ignored and only the ones provided will be set.
-
read = 4, write = 2, execute = 1
-
Here is the binary logic behind it (if you're interested):
Symbolic: r-- -w- --x | 421 Binary: 100 010 001 | ------- Decimal: 4 2 1 | 000 = 0 | 001 = 1 Symbolic: rwx r-x r-x | 010 = 2 Binary: 111 101 101 | 011 = 3 Decimal: 7 5 5 | 100 = 4 / / / | 101 = 5 Owner ---/ / / | 110 = 6 Group ------/ / | 111 = 7 Others ---------/ | Binary to Octal chart
-
Using +x
you are telling to add (+
) the executable bit (x
) to the owner, group and others.
- it's equal to
ugo+x
oru+x,g+x,o+x
- When you don't specify which one of the owner, group or others is your target, in case of
x
it will considers all of them. And as @Rinzwind pointed out, it's based onumask
value, it adds the bit to the onesumask
allows. remember if you specify the target likeo+r
thenumask
doesn't have any effect anymore. - It doesn't touch the other mods (permissions).
- You could also use
u+x
to only add executable bit to the owner.
Using 755
you are specifying:
- 7 -->
u=rwx
(4+2+1 for owner) - 5 -->
g=rx
(4+1 for group) - 5 -->
o=rx
(4+1 for others)
So chmod 755
is like: chmod u=rwx,g=rx,o=rx
or chmod u=rwx,go=rx
.
chmod +x
adds the execute permission for all users to the existing permissions.
chmod 755
sets the 755
permission for a file.
755
means full permissions for the owner and read and execute permission for others.