Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

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

  • Last Updated : 11 Feb, 2021

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.



How to Make File and Directory Undeletable

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/

How to Make File and Directory Undeletable

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 

How to Make File and Directory Undeletable

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

How to Make File and Directory Undeletable

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.

How to Make File and Directory Undeletable

My Personal Notes arrow_drop_up
Recommended Articles
Page :