root cannot delete, move or change a file – Operation not permitted or Permission denied – immutable attribute

If you are the root user and some file (files or directories) cannot be deleted, removed, renamed or changed you probably deal with the immutable attribute set on (by a colleague of yours – installation setups tend to not set such attributes).

Here is what it looks like to have such a file

root@srv.remote /etc/apache2/vhosts.d # mv /root/old/apache/
mv: cannot move `' to `/root/old/apache/': Operation not permitted
root@srv.remote /etc/apache2/vhosts.d # lsattr
root@srv.remote /etc/apache2/vhosts.d # rm
rm: cannot remove `': Operation not permitted
root@srv.remote /etc/apache2/vhosts.d # echo "teeest" >>
-bash: Permission denied

Here is how you can set the attribute off.

You need first to set off the file’s immutable attribute and then to do whatever you intended to do in the first place – delete, rename, change and so on. Y

chattr -i filename.txt

In continuation of our example above:

root@srv.remote /etc/apache2/vhosts.d # chattr -i
root@srv.remote /etc/apache2/vhosts.d # lsattr
root@srv.remote /etc/apache2/vhosts.d # mv /root/old/apache/
root@srv.remote /etc/apache2/vhosts.d #

As you can see no immutable attribute no problem to move the file!

And just not note you need to install a package with the name e2fsprogs (not always in the default installation) in your Linux distribution to have lsattr, chattr and more!