Why can't I delete a file where I have group write permissions on?

26

6

I have a file with the following permissions:

root:data, and chmod set to 775.

My normal user, let's call him boby, is in the data group.

Why can't I delete the file with the user boby?

 rwxrwxr-x 18 root data 4096 2011-12-30 22:02 storage
 my user is in the group data but can't write into storage

danidacar

Posted 2011-12-30T19:45:28.330

Reputation: 4 783

Answers

33

Because by deleting a file, you are not just modifying the file but also modifying its directory.

So if your file is:

rwxrwxr-x

You would be able to do:

cp /dev/null <filename>

But if your directory permissions are:

rwxr-xr-x  root  data  <directory name>

Then system will prevent you removing the file.

Karlson

Posted 2011-12-30T19:45:28.330

Reputation: 2 163

Isn't /dev/null read-only? How can you copy from it? – Aaron Franke – 2018-11-05T22:45:44.170

I have drwxrwxr-x on the directory, I think it has something to do with the d in front – danidacar – 2011-12-30T19:55:23.850

2@user56301 d just indicates that this file is a directory. What's the ownership of the directory? – Karlson – 2011-12-30T19:58:39.537

drwxrwxr-x 18 root data – danidacar – 2011-12-30T19:59:13.083

Try running as user boby the following: cd <directory> ; touch test_file ; rm test_file – Karlson – 2011-12-30T20:02:04.390

@user56301 can you create a file in that directory? if you can not, then you definitely can't delete a file there. – Rich Homolka – 2011-12-30T20:03:11.100

Permission denied, the user is in the group, the dir has write permission for the directory, but I can't create file. If I use 777 it works( as expected ) – danidacar – 2011-12-30T20:03:57.383

13

File deletion is based on directory perms, not file perms (*).

Do you have write permissions on the directory that contains the file?

(*) Caveat, you can have a directory where you enforce that only the owner of the file can delete it. This is useful for temp dirs.

Rich Homolka

Posted 2011-12-30T19:45:28.330

Reputation: 27 121

Also have a look here: https://superuser.com/questions/784952/linux-group-member-cannot-delete-file-with-rw-permission where the same is discussed.

– Meetai.com – 2015-05-27T02:49:27.017

1

If the containing directory does not permit the user boby or the data group to write to it, then that would explain this behavior.

Andrew Lambert

Posted 2011-12-30T19:45:28.330

Reputation: 7 136

2So the entire path needs group permission? It works like that. – danidacar – 2011-12-30T20:06:12.653

1@user: Not the entire path - just the file's immediate parent directory. You are only modifying the directory's contents. The higher parents do not matter at all. – user1686 – 2011-12-30T20:08:41.670

I update the answers – danidacar – 2011-12-30T20:09:38.850

1This is not exactly true. You only need write perms on the containing directory. The perms can be any of user, group, or other, it doesn't have to be group perms that allow you. – Rich Homolka – 2011-12-30T20:12:03.983

@Rich: AFAIK, only one set is checked. If you are the owner, the system will only check 'owner' perms, not 'group' nor 'others'. If you are in the group, the system won't check 'others' perms. (touch foo; chmod 6 foo; ls -l foo; cat foo) – user1686 – 2011-12-30T20:24:37.203

I have removed the "any parent of the containing directory" phrase from my answer. Not sure what I was thinking... – Andrew Lambert – 2011-12-30T21:26:38.393

Psst! The clue is in the final sentence of the question. Hint: Your answer mentions permissions but omits another relevant factor. – JdeBP – 2011-12-30T23:19:16.367

@grawity true, sorry that I was unclear, the answer originally said "it checks groups" I just wanted to say "it may check users, groups, or other as appropriate", not that it will check all 3. But, you know I can't edit comments on stackexchange sites. – Rich Homolka – 2011-12-31T04:36:22.837

1

I tried the same thing, and ran into the same problem.

Starting a new terminal session the problem. This can be achieved by:

  1. Logging out and logging back in
  2. Going to one of the 6 ttys (Ctrl+Alt+F1-6) (Note: Ctrl+Alt+F7 is your GUI session)
  3. using su boby to start a new session for user boby.

Cheers!

Here Be Wolves

Posted 2011-12-30T19:45:28.330

Reputation: 211

He mentions he is already logged in as boby – Canadian Luke – 2013-09-05T14:38:19.043

He needs to login again as boby - the old session seems to be stale and not reflect the group association changes. – Here Be Wolves – 2013-09-05T15:39:36.423

1

I bet the file you're trying to delete is in /tmp.

See Linux - group member cannot delete file with rw permission

/tmp usually 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.

Dagelf

Posted 2011-12-30T19:45:28.330

Reputation: 585