Attributes of file or extend attributes of file are simple and easy to use. Chattr command uses to change file attributes on a Linux in ext2 or ext3 file system. After changing attributes no one can modify or delete a file even they have full access. Chattr keep safe from accidentally deleting files or directories.
Check File Attributes
We can check what attributes are set of a file as below:
# lsattr test.txt
- - - - - - - - - test.txt
Change Attributes of File
chattr command which changes the file attributes on a Linux, Let suppose if we will change a file attribute to '+i' attribute. 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, even root user can't modify this.
# chattr +i test.txt
# lsattr test.txt
- - - - i - - - - test.txt
If you want to changes in file you need to revert back attributes as below:
# chattr -i test.txt
if you want to allow everybody to just append data to a file and not change already entered data, you can set the append bit as follows:
# chattr +a
# lsattr +a test.txt
- - - - a - - - - test.txt
revert back as:
# chattr -a test.txt
There are more options that can use with chattr command
-R Modify directories and their contents recursively.
-V Print modes of attributes after changing them.
-v version Set the file's version.
+ Add attribute.
- Remove attribute.
= Assign attributes (removing unspecified attributes).
The operator `+' causes the selected attributes to be added to the existing attributes of the files; `-' causes them to be removed; and `=' causes them to be the only attributes that the files have.
A Don't update access time on modify.
a Append only for writing. Can be set or cleared only by a privileged user.
d No dump.
i Immutable. Can be set or cleared only by a privileged user.
j Journalled file. This is useful only in cases where you are using an ext3 filesystem mounted with the data="ordered" or data="writeback" attributes. The data="journalled" option for the filesystem causes this operation to be performed for all files in the system and makes this option irrelevant.
S Synchronous updates.
s Secure deletion. The contents are zeroed on deletion, and the file cannot be undeleted or recovered in any way.
u Undeletable. This causes a file to be saved even after it has been deleted, so that a user can undelete it later.