11

I am trying to execute chown on a directory that has the following permissions and owners:

drwxrwxr-x 2 justin devs  4096 Jan  1 20:42 test

I am trying to simply execute the following as the justin user:

chown justin:nginx test

So basically just change the group owner to nginx, but I am getting:

chown: changing ownership of `test/': Operation not permitted

Any ideas?

mdpc
  • 11,698
  • 28
  • 51
  • 65
Justin
  • 5,008
  • 19
  • 58
  • 82

2 Answers2

17

First, use the chgrp command instead of chown and that will work.

In the case of using chown, for security reasons in most Linux contexts, any ownership change is restricted to the root user even though you are marked as the owner of the file, directory, etc or not. In one case, this is to prevent users from evading quotas by setting the file permission bits to 777 and changing the ownership of a file to some other unknown user and eating up their quota.

So using the chown command in a user context, especially where the ownership is not changing should not be used just to change the group of a file.

NOTE: You must be a member of the group you are attempting to change the file to. This can be verified by id -a. If you are not in the group, you will get this message eventhough you are the owner of the file.

mdpc
  • 11,698
  • 28
  • 51
  • 65
  • 1
    Same thing with `chgrp nginx test`, Operation not permitted. – Justin Jan 02 '13 at 05:12
  • If you are running the chgrp command as the owner of the inode, this should work. Is your filesystem mounted READ ONLY? – mdpc Jan 02 '13 at 05:16
  • Nope does not work. Here is a test case: `$ mkdir test $ chgrp nginx test/ chgrp: changing group of test/: Operation not permitted $ ll total 4 drwxrwxr-x 2 justin justin 4096 Jan 1 21:18 test` – Justin Jan 02 '13 at 05:20
  • Please identify the Linux distribution and the level of the operating system? – mdpc Jan 02 '13 at 05:23
  • Have you contacted the systems administrator for this system? – mdpc Jan 02 '13 at 05:24
  • :) LOL I am the sysadmin. Running CentOS 6.3 x64 `uname -a` Linux web2.mydomain.com 2.6.32-279.19.1.el6.x86_64 #1 SMP Wed Dec 19 07:05:20 UTC 2012 x86_64 x86_64 x86_64 GNU/Linux – Justin Jan 02 '13 at 05:26
  • 1
    Got it....you cannot change ownership to a group you are not allowed to be in. Check `id -a` to verify that the group you are trying to change the file to is available to you. – mdpc Jan 02 '13 at 05:31
  • Ok, so I have to add `justin` into the `nginx` group? – Justin Jan 02 '13 at 05:33
  • 1
    yes, that is the solution. – mdpc Jan 02 '13 at 05:34
4

You have to be part of the group in order to be able to change the current group ownership to . You can edit /etc/groups file as root to make sure that user justin is a part of nginx group. After making changes for the group, you need to relogin to the system to make it in affect or to change to the group without a restart/logout, you can use cmd newgrp nginx.

Now you should be able to change the group of the file or folder with cmd chgrp nginx test as user justin.

Cloudmeteor
  • 449
  • 2
  • 7