-->

How to Use chattr Command in Linux

Author Piyush Gupta

Chattr – command is useful to change file attributes on Linux second extended file system. It provides more security on your files from unwanted changes and deletes. There are many attributes available to do it.
To Add attributes on a file we use plus (+) and to remove attributes use minus(-) sign . Some impotent attributes are [ASacDdIijsTtu].

How to Use Chattr Command

Add Attribute on File

Create a new file to test this. As example I have crate a new file piyush.txt using touch command and assigned 777 permission on file.
# touch piyush.txt
# chmod 777 piyush.txt
# ls -l piyush.txt
-rwxrwxrwx 1 root root 0 Apr 19 15:10 piyush.txt
Now enable i attribute on file
# chattr +i piyush.txt
As you have enabled i attribute on file, let’s try to remove this file, you we get following error, even file has 777 permissions.
# rm -f piyush.txt
rm: cannot remove `piyush.txt': Operation not permitted

List Attributes of File

To List attributes on a file use lsattr command specified with file name.
# lsattr piyush.txt
----i-------- piyush.txt

Remove Attributes of File

To clear attribute on a file just use (-) sign with attached attributes. for example in piyush.txt.
# chattr -i piyush.txt

# lsattr piyush.txt
------------- piyush.txt

Attributes List in Chattr Command

Attribute [ i ]: A file with i attribute cannot be modified, it cannot be deleted or renamed by any user included root. Only root can clear this attribute. By this attribute you can keep safe your impotent files from other users or accidental delete.

No comments:

Post a Comment