A Linux system administrator needs to know some Linux tricks. In this article, you will learn how to Make File and directory undeletable Linux (Even By Root).
As you know, root is the account or user name that by default can modify all directories and files on a system, on Unix-like operating systems including Linux.
In this tutorial, you will see how to make directories or files unremovable even by the root user in Linux.
How to Make File and directory undeletable Linux
Among the Linux tips, learning to make files and directories undeletable would be so useful for you, as you can apply this tip on all important configuration files so that no one accidentally or intentionally deletes it.
To make files unmodifiable, you can use the following command which makes /backups/passwd file immutable (or undeletable). After that, you will not be able to create a link to it and no data can be written to the file.
Also, please be aware that to set or remove this attribute, you need superuser privileges. So use sudo commands.
sudo chattr +i /backups/passwd OR sudo chattr +i -V /backups/passwd
Also, use the lsattr command, to view attributes of a file
$ lsattr /backups/passwd
Now let’s try to remove the immutable file, both as a normal user and as a root.
rm /backups/passwd sudo rm /backups/passwd
How to recursively make directory undeletable in Linux
Here we have the -R flag, to recursively change attributes of directories and their contents.
$ sudo chattr +i -RV /backups/
Then, use -i sign to remove the above attribute and making a file mutable again.
sudo chattr -i /backups/ passwd
Good job! Now you know how to make File and directory undetectable, even by root in Linux.
Dear user, we hope you would enjoy this tutorial, you can ask questions about this training in the comments section, or to solve other problems in the field of Eldernode training, refer to the Ask page section and raise your problems in it.