How to make a Linux File unchangeable ( unalterable ) so that no one can modify it

Posted on in Categories , , , , , , , last updated March 5, 2008

Q. How do I write protect file under Linux so that no one can modify it?

A. You need to use chattr command, which changes the file attributes on a Linux second extended file system. The chattr command supports various attributes.

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 in other words you make a file unchangeable or unalterable.

For example if you want file /data/financial.txt unchangeable by anyone on your system, type the following command (login as the root user):
# chattr +i /data/financial.txt
Now no one can delete or modify file /data/financial.txt. To reset back permission, type the following command:
# chattr -i /data/financial.txt
Use lsattr command to lists the file attributes on a second extended file system. It is use to see attributes set by chattr command.
# lsattr financial.txt
Output:

----i------------ financial.txt

See also:

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

Comments are closed.