chattr / lsattr

How do you make a file or folder undeletable or unchangeable even to the root user? Use the chattr command.

This would be useful if you have a that you don’t want to delete by accident or if you are investigating a user’s file and you do not want someone deleting the “evidence” while you investigate.

The command works by setting the immutable bit of the file. Note: This command must be run as root

How do you make a file immutable ?

chattr +i file_name

An example use would be:

chattr +i /var/log/messages

Now if I tried to delete the file I would get:

rm -f /var/log/messages
rm: cannot remove `messages’: Operation not permitted

Please remember that this was done as root !

If you want to unset or turn off the immutable bit (unchattr) you can use the command:

chattr -i file_name

If you want to check what are the attributes of a file you can do so using the command lsattr:

lsattr file_name

In our example we would be getting:

lsattr messages
—-i——– messages

notice the immutable bit? It’s that little letter ‘i’ in the files permissions settings.

g33kadmin

I am a g33k, Linux blogger, developer, student and Tech Writer for Liquidweb.com/kb. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Leave a Reply

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