Linux offers write protection by allowing you to set files as immutable, to do this you will need to set set a special bit. Once this bit is set no one can delete or modify file, this includes the root user. The only user that can set and clear immutable bit is the root user.
List File attributes
To list what bits are set use
No bits set
Immutable bit set (see schg flags are set)
Set file immutable bit
chattr +i testfile.txt
Clear file immutable bit
chattr -i testfile.txt
chattr also supports a number of other attributes
- a - Append-only: appends are permitted to this file, but it cannot otherwise be edited. Root only.
- A - Accesses not timestamped: accesses to this file don’t update its access timestamp (atime).
- c - Compressed: data is transparently compressed on writes and uncompressed on reads.
- d - Don’t dump: tell the dump program to ignore this file when making backups (see Backups and Remote Storage).
- i - Immutable: file cannot be changed or deleted (root only).
- j - Journaled data (ext3 filesystems only).
- s - Secure deletion: if deleted, this file’s data is overwritten with zeroes.
- S - Synchronous update: changes are written to disk immediately, as if you had typed sync after saving (see Disks and Filesystems).
- u - Undeletable: file cannot be deleted (undeletable).
- -R Recursively process directories.
For more information on the usage of chattr and the attributes that can be set see the man page at https://man7.org/linux/man-pages/man1/chattr.1.html