How to Use Chattr Command

Add Attribute on File:-
Create a new file to test this. As example I have crate a new file testfile.txt using touch command and assigned 777 permission on file.

# touch testfile.txt
# chmod 777 testfile.txt
# ls -l testfile.txt
-rwxrwxrwx 1 root root 0 Apr 10 13:10 testfile.txt

Now enable i attribute on file:-

# sudo chattr +i testfile.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 testfile.txt
rm: cannot remove `testfile.txt’: Operation not permitted

List Attributes of File:-
To List attributes on a file use lsattr command specified with file name.

# lsattr testfile.txt
—-i——– testfile.txt

Remove Attributes of File:-
To clear attribute on a file just use (-) sign with attached attributes. for example in testfile.txt.

# sudo chattr -i testfile.txt

# lsattr testfile.txt
————- testfile.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 including root. Only root can clear this attribute. By this attribute you can keep safe your impotent files from other users or accidental delete.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s