The Wayback Machine - https://web.archive.org/web/20250622214756/https://www.geeksforgeeks.org/linux-unix/how-to-make-file-and-directory-undeletable-even-by-root-in-linux/
Open In App

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

Last Updated : 01 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we are going to see How to Make File and Directory Undeletable, Making a file or folder undeletable on Linux means setting it so no one—not even the root user (the all-powerful admin)—can delete, move, or change it. This is done using the chattr command, which tweaks special settings (called attributes) on files and folders. It is useful because it:

  • Protects Important Stuff: Keeps critical files (like system configs or family photos) safe from mistakes.
  • Boosts Security: Stops hackers or malware from deleting key files, even if they gain root access.
  • Saves Time: No need to restore deleted files or worry about data loss.
  • Great for Servers: Ensures Linux servers (e.g., on AWS or Azure) keep essential files intact.

How chattr Command Works

The chattr command (short for “change attributes”) is used to make something undeletable, you add the immutable attribute (marked with a +i), which blocks all changes, deletions, or moves. Only someone with superuser (admin) powers—using the sudo command—can set or remove this lock. You can check these settings with the lsattr command and remove the lock later if needed. We’ll guide you through this process step by step, making it as easy as locking a door.

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

Conclusion

Even root users usually have powers—but sometimes, you need stronger protection. Using the simple chattr +i command, you can make critical files and directories completely undeletable—even from root-level mistakes or malicious attacks. In real-world server environments like AWS, Azure, and private Linux hosting, system failures often happen because someone accidentally deletes important configuration files. Setting key files as immutable prevents this before they happen.


Next Article
Article Tags :

Similar Reads