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. In this article, we’ll show how to make directories or files unremovable even by the root user in Linux.

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

How to Make File Undeletable in Linux

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
$ 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.

$ 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 article useful:

  1. How to Manage Users and Groups in Linux
  2. Managing Users & Groups and Enabling sudo Access on Users
  3. How to Find Files With SUID and SGID Permissions in Linux
  4. Translate rwx Permissions into Octal Format in Linux

That’s it! In this article, we showed how to make files unremovable even by the root user in Linux. You can ask any questions via the feedback form below.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.