Linux – Can’t change owner (user or group) of directory which I have all rights on


TL;DR: Why am I getting the Operation not permitted? And how can I resolve this?

I'm facing a problem which I can't resolve. I'm creating a directory as user a:group a), which I want to change to user b:group a. I don't understand why this operation is not permitted. This is what's happening:

user a@foo:~$ mkdir /home/user b/foo/test             
uber a@foo:~$ chmod 0777 /home/user b/foo/test
user a@foo:~$ ls -alF /home/user b/foo/ | grep test
drwxrwxrwx 2 user a            group a 4096 Jan  6 19:53 test/
user a@foo:~$ chown user b:group a /home/user b/foo/test
chown: changing ownership of `/home/user b/foo/test': Operation not permitted

(I changed the user and group names for simplicity's sake)

Other things that might be relevant:

  • User A is in Group A and Group B.
  • User B is in Group B.
  • Directory foo in /home/user b has 0750, and is owned to User B:Group A.

I'm eager to understand as why this operation is not permitted, and how I can resolve this (a solution without using sudo is a plus)?

Best Answer

You can only change ownership on a file if you're root (or have the CAP_CHOWN Posix capability). This is so because giving away files would trigger some security concerns (for example, if disks quotas were enabled you could then fill user b quota).

Use sudo chown if you're allowed to do so and it will work.

You can however change the owning group to a group you're a member of, so you should be able to chgrp "group b" "/home/user b/foo/test", which may be an alternative to share files with user b without becoming root, depending of what you're trying to achieve.

For more flexible permissions, you may want to look into ACLs.