Linux chattr Command
The Linux chattr command is used to change file attributes.
This command can change the attributes of files or directories on an ext2 file system, which include the following 8 modes:
a: Allow the file or directory to be used for append-only purposes.
b: Do not update the last access time of the file or directory.
c: Store the file or directory in compressed form.
d: Exclude the file or directory from dump operations.
i: Prevent the file or directory from being modified arbitrarily.
s: Securely delete the file or directory.
S: Synchronously update the file or directory.
u: Protect against accidental deletion.
Syntax
chattr [-RV][-v<version number>][+/-/=<attribute>][file or directory...]
Parameters
-R Recursively handle all files and subdirectories within the specified directory.
-v<version number> Set the version number of the file or directory.
-V Display the execution process of the command.
+<attribute> Enable the specified attribute for the file or directory.
-<attribute> Disable the specified attribute for the file or directory.
=<attribute> Specify the attribute for the file or directory.
Example
Use the chattr command to prevent a critical file from being modified:
chattr +i /etc/resolv.conf
lsattr /etc/resolv.conf
This will display the following attributes:
----i-------- /etc/resolv.conf
Allow a file to only have data appended to it, but not deleted, suitable for various log files:
chattr +a /var/log/messages