19

Below shows a file, /tmp/testfile, owned by user1 with group changed to wheel that also includes user2. The file has rw permission for the group. So shouldn't any member of the group be able to delete it? The example output below shows that user2 is not able to delete the file. Why?

[user2@files ~]$ ls -l /tmp/testfile
-rw-rw-r-- 1 user1 wheel 0 Jul 18 18:54 /tmp/testfile
[user2@files ~]$ groups
user2 wheel
[user2@files ~]$  rm /tmp/testfile
rm: cannot remove `/tmp/testfile': Operation not permitted
Cristian Ciupitu
  • 5,513
  • 2
  • 37
  • 47
user347765
  • 201
  • 1
  • 2
  • 4
  • 3
    possible duplicate of [Why can't I delete a file where I have group write permissions on?](http://superuser.com/questions/373115/why-cant-i-delete-a-file-where-i-have-group-write-permissions-on) – Hastur Jul 18 '14 at 22:52
  • Furthermore to the complete explanation by @grawity, a good solution is to create your 'own' directory under `tmp` and fix the sticky situation – fcm Oct 12 '17 at 17:28

1 Answers1

35

First, you're looking at the wrong permissions. When you move/rename/delete a file, you're only modifying the parent directory – the file's own permissions are not checked. You only remove an entry from the directory's list of files. Therefore you should check the permissions of the parent directory (in this case /tmp).

$ ls -ld /tmp
drwxrwxrwt 15 root root 460 Jul 19 15:18 /tmp/

Second, /tmp is special. On practically all systems, it's writable by anyone (ugo=rwx), so at first glance, it looks like anyone could rename or delete any file in it. This would of course make it easy (well, even easier) to create problems for other users, therefore /tmp always has the "sticky" aka "restricted deletion" mode set (o+t). With this mode set, only the file's owner can move or delete files in that directory, regardless of any permissions.

(On GNU coreutils, the chmod(1) manual page has a section about the "restricted deletion flag or sticky bit".)

Cristian Ciupitu
  • 5,513
  • 2
  • 37
  • 47
u1686_grawity
  • 426,297
  • 64
  • 894
  • 966