Here’s how to Make Files and Directories Undeletable in Linux Even by Root User

Here’s how to Make Files and Directories Undeletable in Linux Even by Root User

There are certain files and directories that are of utmost important. Losing them can be catastrophic for a person or business. In Linux, root accounts have the capability to delete any file as per their preference. To safeguard the files or directories, its best to make it undeletable even by the root user.

That is possible by using the chattr command which changes the file attributes on a Linux. In this guide, learn more about preserving files and directories by making them undeletable.

Making Files Undeletable in Linux

As already mentioned, you’d have to use the chattr command to make files impossible to delete in Linux. It makes the /backups/passwd file immutable, which essentially means it cannot be modified or deleted by anyone. Its even not possible to create a link to it, nor write data to it.

To get started, you’d need to have superuser privileges. Then by using sudo command, execute this line:

Code:
$ sudo chattr +i /backups/passwd
OR

Code:
$ sudo chattr +i -V /backups/passwd
You will get the following response if everything goes correctly:

Code:
Chattr 1.42.13 (17-May-2015)
Flags of /backups/passwd set as ----I--------e--
You can view the attributes of any file using the lsattr command like the following:

Code:
$ lsattr /backups/passwd
Now, when you try to remove the file, you will get the following message:

Code:
$ sudo rm /backups/passwd
Code:
rm: cannot remove ‘/backups/passwd’ : Operation not permitted
Recursively Making Directories Undeletable in Linux

To safeguard directories and the files therein, you’d have to flag them with -R. It will change the necessary attributes and make them undeletable.

Here’s how to make directories undeletable:

Code:
$ sudo chattr +i -RV /backups/
If you want to make a file mutable again, employ the -i sign and remove the above attribute like this:

Code:
$ sudo chattr -i /backups/ passwd
So that’s how you make files and directories undeletable in Linux even by root users.
Author
kumkumsharma
Views
3,480
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from kumkumsharma

Top