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.

  • 9
    chmod +x sets all executable flag without changing other permissions. chmod 755 sets rwxr-xr-x – ravery Jul 6 '17 at 19:55
  • 2
    Possible duplicate of What does 'chmod +x <filename>' and how do I use it? – Julien Lopez Jul 7 '17 at 6:16
  • 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! – Lightness Races in Orbit Jul 7 '17 at 13:56
up vote 119 down vote accepted

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

  • 14
    a very important addition : when using chmod 755 you set those bits AND you also clear all the suid/sgid/sticky bits (that may have been there) (for exemple: NEVER chmod 755 /tmp ). 755 should always be thought as 0755, ie the first octal set of bits is also set to 0. – Olivier Dulac Jul 7 '17 at 15:01
  • 1
    almost perfect so a premature +1 (yay you got that badge from me : )): You forgot about umask ;) – Rinzwind Jul 7 '17 at 16:47
  • @OlivierDulac Thanks, I didn't want to get into too much details but I'll updated the answer... – Ravexina Jul 7 '17 at 17:22
  • 1
    I think I've just understood how permissions work, thanks ! – Fabich Jul 8 '17 at 10:28
  • 1

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.

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.

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.

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

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.

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

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.