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:

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

Youc an view attributes of the file using this command:

1
lsattr /home/Scripts/Admin.sh

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

1
2
$ 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

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

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

1
2
3
4
# PROTECT
sudo chattr +i -RV /home/Scripts
#REMOEV PROTECTION
sudo chattr -i -RV /home/Scripts

Leave a Reply