Understanding and Resolving File Ownership Issues on Linux: “Operation not permitted”

When attempting to change the ownership of a file on a Linux system, you may encounter the Operation not permitted error if the action fails. This generic error does not give us a lot of insight into what the problem could be, so we must do a little digging to figure out why the error is occurring. In this tutorial, we will go through some troubleshooting steps to determine why this error occurs while trying to change file ownership with the chown Linux command.

In this tutorial you will learn:

  • How to change ownership of a file using sudo
  • How to mount a file system with ownership settings
  • How to change the immutable flag in the extended attributes for a file
Understanding and Resolving File Ownership Issues on Linux: "Operation not permitted"
Understanding and Resolving File Ownership Issues on Linux: “Operation not permitted”
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System Any Linux distro
Software chown
Other Privileged access to your Linux system as root or via the sudo command.
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Troubleshooting the “Operation not permitted” Error in Linux




The Operation not permitted error can appear under a myriad of circumstances on a Linux system. But we are specifically looking into why this error occurs whenever trying to change the ownership of a file with the chown command.

For example:

$ chown linuxconfig: example_file.txt 
chown: changing ownership of 'example_file.txt': Operation not permitted
DID YOU KNOW?
The Operation not permitted error sounds similar to the Permission Denied error, but the two errors are actually caused by different things. The permission denied error usually indicates a lack of necessary file permissions, while the former means that an operation was simply not able to be performed.

Let’s look at a few different reasons below for why the Operation not permitted error might appear when using the chown Linux command:

Insufficient Permissions

The most common cause for this error is that your user account probably does not have sufficient permissions to be delegating ownership of the file in question. If you want to change the ownership for a file, then your user account needs to already be the owner. Otherwise, only the root account can change the ownership of the file.

In this scenario, we can simply preface our chown command with sudo, and the ownership of the file should be successfully changed:

$ sudo chown linuxconfig: example_file.txt 

Still not working? Try one of the other solutions below.

File System Does Not Support Linux Permissions

If you are working on a file system that does not support Linux file permissions (file systems other than ext3, ext4, etc), then you will encounter the Operation not permitted error when trying to change file ownership – even when using the root account. File systems like exFAT do not support file permissions by default.

In this case, the solution would be to mount the file system by using the uid=1000,gid=1000 options, so that all files on the file system will be owned by the current user account and group.

$ sudo mount -t exfat -o uid=1000,gid=1000 /dev/sdX1 /mount/point




You will need to adapt the above command to fit your needs (put in the proper file system type, device path, and mount point), but the idea is that using the -o uid=1000,gid=1000 option in your mount command will allow you to take ownership of all files on the file system once it is mounted.

The File Has the Immutable Flag Set in the Extended Attributes

Another scenario in which the pesky Operation not permitted error may appear is when the immutable flag is set within the files extended attributes. If this flag is set, then you will not be able to change ownership of the file with the chown command.

Follow along with the steps below to change the immutable flag on a file:

  1. First, for the sake of example, let’s see how to turn ON the immutable flag:
    $ sudo chattr +i example_file.txt
    
  2. We can examine the extended attributes of the file by using the lsattr command on it:
    $ lsattr example_file.txt
    ----i--------e-- example_file.txt
    

    The i in the output above indicates that the immutable flag is turned on, which means it will be impossible to change the owner of the file, even with the root account. Let’s check to make sure:

    $ sudo chown linuxconfig:linuxconfig example_file.txt
    chown: changing ownership of 'example_file.txt': Operation not permitted
    
  3. To fix this, we can disable the immutable flag by executing the following command:
    $ sudo chattr -i example_file.txt
    
  4. Let’s once again check the extended attributes of the file:
    $  lsattr example_file.txt
    -------------e-- example_file.txt
    
  5. Good to go. We can now successfully change ownership of the file with the chown command:
    $ sudo chown linuxconfig:linuxconfig example_file.txt
    

Closing Thoughts




In this tutorial, we saw how to resolve the Operation not permitted error on a Linux system. We went through various scenarios in which this error can occur while trying to use the chown command to change the ownership of a file in Linux. This is not meant to be an exhaustive list, especially since the error is rather generic, but the issues above are usually the main causes for the error to occur. Hopefully this has helped you in remedying the error.



Comments and Discussions
Linux Forum