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.

Can someone explain why I get permission denied when running touch -m on this file even though it is group writable and I can write to the file fine.

~/test1-> id
uid=1000(plyons) gid=1000(plyons) groups=1000(plyons),4(adm),20(dialout),24(cdrom),46(plugdev),109(lpadmin),110(sambashare),111(admin),1002(webadmin)
~/test1-> ls -ld .; ls -l
drwxrwxr-x 2 plyons plyons 4096 Feb 14 21:20 .
total 4
-r--rw---- 1 www-data webadmin 24 Feb 14 21:29 foo
~/test1-> echo the file is writable >> foo
~/test1-> touch -m foo
touch: setting times of `foo': Operation not permitted
~/test1-> lsattr foo 
-------------e- foo
~/test1-> newgrp - webadmin 
~/test1-> id
uid=1000(plyons) gid=1002(webadmin) groups=1000(plyons),4(adm),20(dialout),24(cdrom),46(plugdev),109(lpadmin),110(sambashare),111(admin),1002(webadmin)
~/test1-> touch -m foo
touch: setting times of `foo': Operation not permitted
~/test1-> echo the file is writable >> foo
~/test1-> 
share|improve this question

2 Answers 2

up vote 5 down vote accepted

From man utime:

       The  utime()  system  call changes the access and modification times of
       the inode specified by filename to the actime  and  modtime  fields  of
       times respectively.

       If  times  is  NULL, then the access and modification times of the file
       are set to the current time.

       Changing timestamps is permitted when: either the process has appropri‐
       ate  privileges,  or  the  effective  user ID equals the user ID of the
       file, or times is NULL and the process has  write  permission  for  the
       file.

So, to change only the modification time for the file (touch -m foo), you'd need to either be root, or the owner of the file.

Being able to write to the file only gives you permission to update both the modified and access times to the current time; you can not update either separately, nor set them to a different time.

share|improve this answer
    
Any idea the rational behind this? It seems silly that I would be permitted to overwrite the files content completely yet denied permission to change the mtime to a specific value? –  Peter Lyons Feb 15 '13 at 14:42
1  
@PeterLyons For example, you can indirectly set the mtime to the current date, but you can't force an mtime in the past, so you can't hide that the file has been modified recently. –  Gilles Feb 15 '13 at 22:20

It seems like file owner web-data have only read permission that's why you are getting error.

Try to change the permission like

chmod 750 foo

then su web-data and then run touch -m command.

or run touch -m command as a root.

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.