Linux security

How to Make File and Directory Undeletable in Linux

There may be situation that you want to make sure your file or directory cannot be DELETED by accident or intentionally.
In my case I like to protect this way my special directory containing my special and very important scripts. This is very easy to setup.

You need to obviously elevate your rights to do this task:

sudo chattr +i /home/Scripts/Admin.sh

Youc an view attributes of the file using this command:

lsattr /home/Scripts/Admin.sh

If you try to DELETE it, you will get error message

$ sudo rm /home/Scripts/Admin.sh
rm: cannot remove '/home/Scripts/Admin.sh': Operation not permitted

Now, it you want to revert and make the file again as before so you can delete, you just need to use this switch (-i) in the command

sudo chattr -i /home/Scripts/Admin.sh

The same you can do for DIRECTORY and here is the command:

# PROTECT
sudo chattr +i -RV /home/Scripts
#REMOEV PROTECTION
sudo chattr -i -RV /home/Scripts

Leave a Reply

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