Introduction
In this tutorial, we will look at chown “Operation not permitted” error in Linux. This is most common error seen whenever we do chown operation on a file of directory. We will look at the possible reason behind occurrence of this error. We will also discuss possible solutions which are used to fix this error in Linux. Let us first understand what this error really means in the next section.
What is “Operation not permitted” error?
The error “Operation not permitted” typically occurs when the chown
command is unable to change the ownership of a file due to permission restrictions. In Linux, chown
command is used to change the file’s ownership. If the user running this command does not have he necessary permissions, this error will occur. There are two possible reason for this error to occur. They are:
- User does not have necessary permissions
- File has immutable attributes
The immutable attribute prevents a file from being modified, renamed or deleted by both regular users and root user until the attribute is removed. We will cover two solution to fix the error in the next section. To reproduce the error you can use below command.
[stack@linuxnasa ~]$ ll total 0 -rw-r--r--. 1 root root 0 Sep 25 22:47 hello-world.py [stack@linuxnasa ~]$ chown secuser hello-world.py chown: changing ownership of ‘hello-world.py’: Operation not permitted
As you see, hello-world.py file is owned by root user. We are trying to change the ownership of this file as stack user which doen not have necessary permissions to execute the chown
command. Hence, it will throw the error.
[Solved]: chown “Operation not permitted” error
Also read: [SOLVED]: Configure error: no acceptable C compiler found in $PATH
Prerequisite
- Any user with sudo privilege/root user privilege required
- Basic understanding of Linux CLI commands
Solution-1: Login as sudo privilege/root user
In this step, switch to the user which has sudo privilege or as root user using below command.
To change ownership using root user
[stack@linuxnasa ~]$ su - root Password: Last login: Mon Sep 25 22:46:38 IST 2023 from 10.116.313.182 on pts/3 [root@linuxnasa~]#
Next, switch to the home directory to stack user where hello-world.py file is present and change the file permission using below command.
[root@linuxnasa ~]# cd /home/stack/ [root@linuxnasa stack]# chown secuser hello-world.py
Next, check if the file ownership is changed using below command. You will notice that file permission has been successfully changed.
[root@linuxnasa stack]# ll total 0 -rw-r--r--. 1 secuser root 0 Sep 25 22:47 hello-world.py
To change ownership using sudo user
[stack@linuxnasa]$ sudo chown secuser hello-world.py [sudo] password for stack: [stack@linuxnasa]$ ll total 0 -rw-r--r--. 1 secuser root 0 Sep 25 22:47 hello-world.py
Solution-2: Remove Immutable Attribute
In this step, check if the file has immutable attribute using below command. If you see ‘i’ added in the file that means immutable attribute has been added to the file.
[root@linuxnasa stack]# lsattr ----i--------e-- ./hello-world.py
Next, delete the immutable attribute from the file as shown below.
[root@linuxnasa stack]# chattr -i hello-world.py [root@linuxnasa stack]# lsattr -------------e-- ./hello-world.py
NOTE:
[root@linuxnasa stack]# chattr +i <filename>
Next, change the file ownership using below command.
[root@linuxnasa stack]# chown stack hello-world.py
You will now the the ownership has been changed successfully as we have removed the immutable attribute from the file.
Summary
If you want to learn more about the usage of chown command, please refer to chown man page where all the supported options of this command has been given and explained.