108

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.

2
  • 13
    chmod +x sets all executable flag without changing other permissions. chmod 755 sets rwxr-xr-x
    – ravery
    Jul 6, 2017 at 19:55
  • 5
    @ravery: Hi there! You have found the comments section, which is an area dedicated to critiquing and requesting clarification. To provide an answer/solution, you should use the "answer" section below (look for the big red "Post Your Answer" button). Hope that helps! Jul 7, 2017 at 13:56

7 Answers 7

221

Short version:

To be able to compare them, we should look at them from the same perspective, so:

  • chmod +x is equal to chmod ugo+x (Based on umask value)
  • chmod 755 is equal to chmod u=rwx,go=rx

Explanation:

Firstly you should know that:

  1. + means add this permission to the other permissions that the file already has.
  2. = 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.
  3. 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 or u+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 on umask value, it adds the bit to the ones umask allows. remember if you specify the target like o+r then umask 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.

enter image description here

3
  • 2
    So what's the difference between chmod a+x and chmod +x?
    – Ricardo
    Sep 13, 2018 at 16:46
  • 1
    @Ricardo here: What is the different between “chmod +x” and “chmod a+x”
    – Ravexina
    Sep 13, 2018 at 22:26
  • 1
    Spent about half a lecture on chmod a few years back in uni, didn't get a single question right on the exam about it. Now in the working world and having to use it, your post helped me learn it in <5 minutes. Very well explained, thanks!
    – plum 0
    Feb 21, 2020 at 16:49
41

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.

0
11

Another way to look at it (which I find easier to understand) is that chmod +x is setting the permissions relatively, whereas chmod 755 is setting them absolutely.

After chmod 755 is ran on a file, its permissions will be 755, or rwxr-xr-x.

chmod +x will just take the existing permissions, and add execute permissions to the file.

7

I recommend reviewing the chmod man page for full detail. You are just seeing two different operation modes available with the chmod command to accomplish the same task of changing permissions.

Octal mode is using numbers and sets the entire permissions of the file. Character mode is using the letters and is generally used to just modify existing permissions.

chmod 755 sets rwxr-xr-x while chmod +x adjusts permissions so that owner, group, and world all have executable permissions added. Assuming a default file permission of rwxr--r-- it would adjust it to the same permissions as 755 of rwxr-xr-x.

6

The difference is what permissions get set and which mode you use to set them.

With chmod +x you set the executable bit for all - the owner, the owner group, and the other users. This is known as symbolic mode. To quote the man chmod:

The operator + causes the selected file mode bits to be added to the existing file mode bits of each file; - causes them to be removed; and = causes them to be added and causes unmentioned bits to be removed except that a directory's unmentioned set user and group ID bits are not affected.

With chmod 755 you're using octal numbers, the binary representation of which is used to set specific bits of permissions. First (left) 3 bits correspond to owner permissions, middle 3 to the group permissions, and last (rightmost) correspond to permissions of all other users. The order of bits is always the same, read,write,execute or rwx Thus, exactly because the order is the same, individual number when converted to binary representation, will set the permission bits for which corresponding positional bit in the number is 1 and unset the one that is 0. Specifically:

  • Octal number 7 is 111 in binary, so you're setting all read,write, and execute bits for owner; rwx is set.
  • Octal number 5 is 101 in binary, so you're setting read and execute but disable write bits, and since it's 5 for group and other users, those two categories will have the same permissions. Thus r-x is set.

Here's the small demo:

    bash-4.3$ touch file1 file2
    bash-4.3$ chmod +x file1
    bash-4.3$ chmod 755 file2
    bash-4.3$ ls -l file1 file2
    -rwxrwxr-x 1 xieerqi xieerqi 0 7月   6 13:54 file1
    -rwxr-xr-x 1 xieerqi xieerqi 0 7月   6 13:54 file2
3

One important difference is that chmod + is subject to umask restrictions, and chmod <octal> is not.

Consider the following example:

$ ls -l foo bar
---------- 1 gowenfawr users 0 Jul  7 16:40 bar
---------- 1 gowenfawr users 0 Jul  7 16:39 foo
$ umask
0022
$ chmod +w bar
$ umask 0002
$ chmod +w foo
$ ls -l foo bar
--w------- 1 gowenfawr users 0 Jul  7 16:40 bar
--w--w---- 1 gowenfawr users 0 Jul  7 16:39 foo
$

So, if you want to make a delta change to permissions in a way that's appropriate to your umask settings, use the '+' syntax. But if you want to set it absolutely without regard to umask, use the <octal> format, and realize you have to specify all the bits and not just a delta.

2

In addition to these beautiful answers I want to mention a small but probably important difference. The command chmod 755 file is equivalent to chmod 0755 file. If we run this command on a file which has the SETUID-bit or SETGID-bit set, it will remove the SETUID/SETGID-bit. chmod +x file will leave the SETUID/SETGID-bit untouched. We can see this in the following example:

~$ touch test
~$ chmod u+s test
~$ ll test
-rwSrw-r-- 1 mook mook 0 Sep 14 00:49 test
~$ chmod +x test
~$ ll test
-rwsrwxr-x 1 mook mook 0 Sep 14 00:49 test
~$ chmod 755 test
~$ ll test
-rwxr-xr-x 1 mook mook 0 Sep 14 00:49 test

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .