e Learning

chown operation not permitted when changing the file owner in Linux

As a Linux user, you can change the permissions of a file or directory you own. But can you change the ownership of a file or directory that you own?

The answer is No. A Linux user cannot change the ownership of a file even if the user owns the file. Only the root user can change the file owner or you need  have sudo privileges as a regular user.

Problem: chown operation not permitted

The way Linux file permissions work, you cannot give away the ownership of a file or folder you own.

For example, I have logged into my Ubuntu Linux system as the user ben. In the home directory I have a text file called file1.txt which is owned by the user ben. Now I will run the chown command and try to change the file owner to root.

chown root file1.txt

Instead of changing the owner, chown command output the following error:

chown: changing ownership of ‘file1.txt’: Operation not permitted

Solution

The bottom line is that regular Linux users don’t have  permissions to change the file owner even as the owner of the file. Only the root user or a user with root privileges may change the owner of the file.

So you have to run the chown command as the root user or you can run the chown command with sudo.

sudo chown root file1.txt

Note that to run Linux commands with sudo privileges, you need to be a member of the sudo admin group.

Changing the Group Owner

A user can change the group owner of a file if the user owns the file, but only if the user is a member of the target group.

Which means, A Linux user may change the group owner of a file if following two conditions are satisfied:

  1. The user must own the File or the Directory.
  2. The user must be a member of the new group.

For Example, let’s think user Ben wants to change the group owner of the file1.txt. The file1.txt is owned by the user Ben and Ben’s primary user group. Now Ben wants to change the group owner to admin user group.

To be able to change the group owner to admin, Ben must be a member of the admin user group.

chmod :admin file1.txt

If Ben is not a member of the admin group, chmod command will give the same error message “chown operation not permitted”.

chown: changing group of ‘file1.txt’: Operation not permitted

These restrictions do not apply to the root user or when you execute the chown command with sudo privileges.