20

Out of the two options to change permissions:

  • chmod 777 file.txt
  • chmod a+rwx file.txt

I am writing a document that details that users need to change the file permissions of a certain file. I want to detail it as the most common way of changing file permissions.

Currently is says:

- Set permissions on file.txt as per the example below:
    - chmod 777 /tmp/file.txt

This is just an example, and won't change files to have full permissions for everyone.

12
  • 6
    first solution is shorter and more widely used, I never seen the second.
    – Archemar
    Commented Sep 3, 2014 at 14:21
  • 2
    setting read, write AND execute permissions for everyone is really no good idea in terms of security. You should use chmod 773 instead. Commented Sep 3, 2014 at 19:34
  • 1
    @MartinErhardt I would normally just use 775 or 755 for executable files only.
    – Kevdog777
    Commented Sep 4, 2014 at 7:49
  • 3
    chmod 777 is like chmod a=rwx, not chmod a+rwx. Commented Sep 4, 2014 at 10:32
  • 1
    777 or 666 is simple and much more memorizable than ugo+rwx, a+rwx or ugo+rw which is very confusing as o is ambiguous and can mean for you either owner or others, you choose. So instead making the big mistake, you keep checking man to verify which one is which one, or just use the numbers.
    – kenorb
    Commented Sep 15, 2014 at 15:18

4 Answers 4

30

Google gives:

chmod 777 is about 3 times more popular.

That said, I prefer using long options in documentation and scripts, because they are self-documenting. If you are following up your instructions with "Run ls -l | grep file.txt and verify permissions", you may want to use chmod a+rwx because that's how ls will display the permissions.

7
  • Thanks for this. I guess that by writing it out in full (alphanumeric) will prevent people from asking what does the 7 mean, etc.
    – Kevdog777
    Commented Sep 3, 2014 at 14:54
  • 2
    I think the reason chmod 777 is more popular (as conjetured before seeing your numbers) is simply that you type 2 characters less :)
    – Ángel
    Commented Sep 3, 2014 at 15:07
  • If you're into adding tips in your documentation, you can add a tip that chmod 777 is a handy shortcut to achieve the same effect. Your readers would appreciate the educational element in your documentation. 777 is more popular because it's a lot easier and faster to hit 7 three times, but when a new user sees 777 for the first time they may not know what exactly it means. That's why it's a bad choice as primary documentation. But it's definitely worth mentioning as a tip.
    – ADTC
    Commented Sep 3, 2014 at 23:54
  • As a tangent, my brother and his friends would use this technique for allowing Scrabble words. They had some threshold for number of Google hits before a word was considered 'real' Commented Sep 4, 2014 at 11:09
  • I think it is popular because it is older (symbolic notation was added later), so when any one chooses, they search on google or what ever the equivalent of the time was and go with the crowd. I think it is easier to use symbolic, because by the time I have worked out the numbers I could have typed the symbols, I also tend to get it right first time. Commented Jul 18, 2016 at 18:15
26

[I edit to add best practice, following Dotancohen suggestion in his answer. I hope it doesn't make it less clear, and that the good habit is taken]

Important additional information: They are not equivalent.

chmod a+rwx : set the last 3 octals to 777, so it ensure that Owner, Group and Users have the "rwx" set. If there are additional bits in the first octal (setuid, setgid, and/or Sticky bit) it leaves them untouched. Think about it as a binary "or 00777".

chmod 777 : set the rights to 00777, so it ensure Owner, Group and Users have "rwx" set, AND NOTHING MORE. It also make sure the additional bits (setuid, setgid, and/or Sticky bit) are set to 0.

So use the first form, if you just want to make sure to grant access to everyone (and please make double, triple sure that it is required... it opens the door to all sort of security problems, some quite unexpectedly broad in what they allow a malicious user to do)

Use the 777 form if you also want to make sure to reset any setuid/setgid/sticky bit, ie if the files needs to be "00777", which is probably more likely in your case (the file's right is known, and should be : 00777). Here also, make triple sure that it is really needed...

Usually it's best to keep access to owner(and sometimes group) : then use the groups to grant access to some specific users to the file/directory. a+rwx is both easy and usually the wrong way to grant access (of course there are very rare cases when it is the only way...)

http://en.wikipedia.org/wiki/Chmod is a good read as it explains what each number or letter represents (including setuid/setgid/sticky)

9
  • 1
    In addition, chmod a+w (which a+rwx includes) on a suid file is very dangerous. Do I need to elaborate on chmod a+w *?
    – Joshua
    Commented Sep 3, 2014 at 19:15
  • What is the point in setting the others permission? If you are logged into the machine as the user, then surely you can just use the users permission?
    – Kevdog777
    Commented Sep 4, 2014 at 7:52
  • @Kevdog777: Others is for people not the owner nor in the group, aka everyone else on the system Commented Sep 4, 2014 at 7:59
  • 1
    @Kevdog777: if only root and the user need access : chmod 00600 (if no need to execute the file) or chmod 00700 (if needs to execute the file -or- it's a directory). or chmod 00400 to add a (tiny, easily overridden) protection against modifying/writing to the file (00500 for a directory). Then others (and people in the group but not the user) can't access the file/dir. (following dotandcohen note, I add the 4th octal in there + a "0" before it: makes no difference for the shell, but helps if you set via perl/C/etc and the 4th octal is a kind of reminder that it exists, and that it is set too) Commented Sep 4, 2014 at 8:04
  • 4
    chmod 777 is like chmod a=rwx. Commented Sep 4, 2014 at 10:32
2

I usually think of the difference being that setting the permissions to 0777 explicitly sets them to 0777. As mentioned earlier, the leading 0 will be inferred if you just type 777. Whereas a+rwx adds read/ write/ execute leaving the setuid/ sticky bit untouched.

Suppose you just want to be sure that a file is executeable, you might use a+x so that you can modify the execution privilege without worrying about or modifying the other permissions. If you used the octal representation, you would need to know what the permissions are currently set to to be sure that you do not modify the permissions in some other way besides what you intended.

1

I would say that giving the numerical values is far more common in "simple peoples guides" such as those produced for users of budget web hosting. However, take care to specify them as octal, not decimal, values:

$ chmod 0777 some_dir

Note the leading 0 in 0777. Though Bash and other CLI environments do the right thing with using the unpadded 777, many programming languages such as PHP and Perl have similar functions that do require the leading 0. So I recommend its use in Bash as well, to remember that it is supposed to be there.

Note to downvoters: As clarified in the comments, the chmod command does in fact know to pass the directive as octal even if decimal notation is specified. However, not all environments support this, thus it is best practice to get used to specifying it explicitly.

10
  • 10
    chmod does never treat the number as decimal. chmod 777 file and chmod 0777 file are completely equivalent.
    – celtschk
    Commented Sep 3, 2014 at 14:51
  • 3
    @Kevdog777: "octal" just means base-8. But you don't need to care about it, as far as usage of the chmod shell command is concerned; you can just think of the three digits as independent from each other, each one telling you one set of permissions.
    – celtschk
    Commented Sep 3, 2014 at 14:54
  • 3
    To chmod, the 0 in 0777 actually means “clear the setuid, setgid, and sticky bits”. This is equivalent to 777, as the argument is always padded with leading zeros to four digits. For the same reason, chmod 77 is equivalent to chmod 077. Commented Sep 3, 2014 at 15:22
  • 1
    The chmod command interprets its mode argument in octal. You need to pay attention if you're using most programming languages (C, Perl, …), but in a shell script it's octal for chmod. Commented Sep 3, 2014 at 21:24
  • 1
    +1 for your note, as it is indeed good to always prepend a 0 (even if setting all 4 octals: 01777 for example) as it makes no difference to the shell, but ensure C, perl and others interpret it as octal Commented Sep 4, 2014 at 7:54

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