Linux – Is there a way to prevent/protect a file from being deleted even by root?

Yes, you can change the attributes of the file to read-only.

The command is:

chattr +i filename

And to disable it:

chattr -i filename

From man chattr:

A file with the i attribute cannot be modified: it cannot be deleted or renamed, no link can be created to this file and no data can be written to the file. Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.

Leave a Comment