Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
What is the chmod command the script is trying to run? I.e. what is it attempting to change the permissions to? –  depquid Mar 19 '13 at 14:34
    
@depquid: basically the script always runs something like mkdir -p blah && chmod 775 blah, to ensure that directory blah exists and has the right permissions... –  kjo Mar 19 '13 at 15:05
2  
@kjo Aha! You can use the umask utility to ensure the directory is created with appropriate permissions. –  D_Bye Mar 19 '13 at 15:08
    
@D_Bye: that's great advice. Please add it to your answer. –  kjo Mar 19 '13 at 15:10

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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