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:
OR
You will get the following response if everything goes correctly:
You can view the attributes of any file using the lsattr command like the following:
Now, when you try to remove the file, you will get the following message:
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:
If you want to make a file mutable again, employ the -i sign and remove the above attribute like this:
So that’s how you make files and directories undeletable in Linux even by root users.
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
Code:
$ sudo chattr +i -V /backups/passwd
Code:
Chattr 1.42.13 (17-May-2015)
Flags of /backups/passwd set as ----I--------e--
Code:
$ lsattr /backups/passwd
Code:
$ sudo rm /backups/passwd
Code:
rm: cannot remove ‘/backups/passwd’ : Operation not permitted
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/
Code:
$ sudo chattr -i /backups/ passwd