Open In App

How to Make File and Directory Undeletable Even By Root in Linux?

In this article, we are going to see How to Make File and Directory Undeletable, Even By Root in Linux. So in order to make a file or directory undeletable by any user, we need to make the file or directory unmodifiable using the chattr command. This command changes the file attributes in our system.

So in order to make it Undeletable Syntax is:



Note: We need superuser user privileges to set or remove the attribute sudo Command

sudo chattr +i - v "file name"

Example:



sudo chattr +i -V manager

The Above command, created an undeletable file that is inside an example directory.You can see the below image.

Suppose if you want to view the attributes of a file, use the lsattr command as shown below 

lsattr manager 

You can see in the below image the attributes that are set/

Now if we try to remove the immutable file which we have made some changes above using the command like in below 

rm manager 
sudo rm manager

We can see the below image after trying to remove it as both normal and root user 

Next, if we want to make the Directory undeletable, then we should use -R Command to change the attributes and their files present inside.

sudo chattr +i -RV example1

To make a file mutable again we should use –i sign to remove all attributes as given below. Now you can see in the below image the example1 directory is now deletable because we have made it mutable using -i.

Article Tags :