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

On Unix-like operating systems including Linux, root is the account or user name that by default can modify all directories and files on a system.

But have you ever wanted to protect your important files and directories from accidental deletion, even by the superuser or root user on your system?

In this article, we’ll walk you through the steps to make your files and directories undeletable, even by the most powerful root user on your Linux system.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.

How to Make File Undeletable in Linux (Even by Root)

To make a file undeletable by any system user, including root, you need to make it unmodifiable using the chattr command, which changes the attributes of a file or directory on a Linux file system.

Using the chattr Command

The command below makes /backups/passwd file immutable (or undeletable). This implies that the file can’t be modified in any way: it can’t be deleted or renamed. You can’t even create a link to it and no data can be written to the file as well.

Note that you need superuser privileges to set or remove this attribute, using the sudo command:

sudo chattr +i /backups/passwd
OR
sudo chattr +i -V /backups/passwd
Make File Undeletable in Linux
Make File Undeletable in Linux

To view attributes of a file, use the lsattr command as shown.

lsattr /backups/passwd 
View Linux File Attributes
View Linux File Attributes

Now try to remove the immutable file, both as a normal user and as a root using rm command as shown.

rm /backups/passwd
sudo rm /backups/passwd
Delete Immutable File in Linux
Delete Immutable File in Linux

How to Recursively Make Directory Undeletable in Linux

Using the -R flag, you can recursively change attributes of directories and their contents as follows.

sudo chattr +i -RV /backups/  
Make Directory Undeletable in Linux
Make Directory Undeletable in Linux

To make a file mutable again, use -i sign to remove the above attribute, as follows.

sudo chattr -i /backups/ passwd

For more information, read this article: 5 ‘chattr’ Commands to Make Important Files IMMUTABLE (Unchangeable) in Linux

You will find these related articles useful:

Conclusion

By using the methods outlined in this guide, you can protect your important files and directories from accidental deletion by the root user using the chattr command which adds an extra layer of security to safeguard your data.

If this article helped, with someone on your team.

TecMint Weekly Newsletter
Get the Learn Linux 7 Days Crash Course free when you join 34,000+ Linux professionals reading every Thursday.
Check your email for a magic link to get started.
Something went wrong. Please try again.
TecMint has been free for 14 years. Help keep it that way.
Google AI Overviews and tools like ChatGPT have cut into search traffic for independent tech sites like TecMint. Running this site costs over $2,000 every month for hosting, infrastructure, and paying authors to keep the content accurate and tested.

If this article helped you solve a problem, consider buying a coffee. It helps keep TecMint free, supports the authors, and keeps the project going.
☕ Buy Me a Coffee
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

2 Comments

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.

Free Course
Get a free Linux course before you go.
Subscribe to TecMint Weekly and get the Learn Linux 7 Days Crash Course free. Read by 34,000+ Linux professionals every Thursday.
Something went wrong. Please try again.
Check your email for a magic link to get started.