Script failing with “chmod: … Operation not permitted”

chmodpermissions

A script of mine works fine when I run it, but fails when run by a different user, with an error of the form

chmod: changing permissions of `/A/B/C/D/E': Operation not permitted

(Here /A/B/C/D/E is a directory. FWIW, the script resides in /A/B/C/D.)

In case it matters, the permissions structure of the directory in question and all its ancestors is as follows:

drwxrwsrwx kjo11 proj1 /A/B/C/D/E/
drwxrwsrwx cwr8  proj1 /A/B/C/D/
drwxrwsr-x root  proj1 /A/B/C/
drwxrwsr-x root  proj1 /A/B/
drwxr-xr-x root  root  /A/
drwxr-sr-x root  root  /

(In this listing, kjo11 is my $USER name. As it happens, cwr8 is the $USER name of the user for which the script fails. In any case, we are both in the proj1 group.)

The output of uname is Linux.

I'm stumped. The only detail that catches my attention is that the error is Operation not permitted, as opposed to Permission denied, but I can't make anything more out of this.

What conditions can cause such errors on running chmod?

Best Answer

Only the owner of a file, or the superuser, may alter the permissions on a file. This is true, even if the user is a member of the group that owns the file and the permissions of the file and parent directory would suggest setting permissions should be possible.

You can control the permissions of files and directories at creation time, by using the umask facility of your shell:

$ umask 002
$ mkdir -p targetdir
$ ls -ld targetdir
...
drwxrwxr-x  2 dan   wheel    2 19 Mar 15:13 targetdir

If you're doing this in a script, it's probably a good idea to save the original umask value so you can restore it upon successful creation of your directories.

Related Question