5

The current file permissions look like this:

$ ls -l file
-rwxrwxr-x 1 chiranjitd chiranjitd 0 Oct 30 14:52 file

Now I try to give write permissions using chmod:

$ chmod +x+r+w file

After that, the file permissions still look like this:

$ ls -l file
-rwxrwxr-x 1 chiranjitd chiranjitd 0 Oct 30 14:52 file

The write permission is not given to the others. Why is this happening?

AdminBee
  • 22,803

1 Answers1

23

When you run chmod +x+r+w file, all the bits are set, except those masked by your umask. Your umask presumably masks (at least) other users’ write bit, so the +w part ends up being ignored for them.

Thus for a typical umask of 022, any chmod command which doesn’t specify who the permissions should be set for ends up ignoring any specified changes to the group’s and others’ write bits.

To set all the bits, ignoring umask, you need to specify who you want to set them for:

chmod a+rwx file

or more explicitly,

chmod ugo+rwx file

(or any subset of ugo, as appropriate, for the user, group, and/or others).

It’s also possible to specify numeric permissions instead of symbolic permissions; see Understanding UNIX permissions and file types for details.

Note that the umask masking behaviour also applies when clearing bits, which can produce surprising results: chmod -w file will only clear write bits which would be set by chmod +w file! Continuing with a typical umask of 022, this means that chmod -w won’t touch the group’s or others’ write bits, leaving them set if they already are. (GNU coreutilschmod helpfully warns about this.)

Stephen Kitt
  • 434,908
  • 7
    oh, that last part is indeed surprising. Looks like GNU coreutils' chmod warns about that: chmod 777 test.txt; chmod -w test.txt gives chmod: test.txt: new permissions are r-xrwxrwx, not r-xr-xr-x – ilkkachu Oct 30 '21 at 17:20
  • Let's not forget that once the file permission assignment is correct, the user must also have the ability to "see into" the directory (ie: ls returns the file listing) in addition to read/write the file itself. – Ian W Oct 31 '21 at 00:02
  • @IanW that's not exactly true, they only need the right to traverse the directory (ie. x), not to list its content (ie. r). – Ginnungagap Oct 31 '21 at 07:50
  • @ginnungagap, just saying it's not just the file perms, it's also got to do with the directory settings. The respondent should add that for a complete answer, per your details, yes. – Ian W Oct 31 '21 at 10:45
  • 1
    @IanW that aspect doesn’t come into play here, as indicated by the OP’s initial ls -l (which wouldn’t work correctly if the OP couldn’t read and traverse the current directory). I prefer to keep answers focused on the problem at hand, rather than attempting to address all possible areas of misunderstanding. – Stephen Kitt Oct 31 '21 at 20:18
  • Nitpick: that initial ls -l tells us nothing about what other users can see, since that was run by the owner. (You're right not to clutter the answer second-guessing that part, though - that's a totally different question!) – Toby Speight Nov 01 '21 at 13:47
  • @Toby not really a nitpick, thanks — I was reading the comments as determining whether the chmod would even be possible, not as being about the permissions required so that others could write to the file! But even for that, if one assumes the read permission was already effective, then everything is already OK. – Stephen Kitt Nov 01 '21 at 15:13