Friday, 3 January 2014

Immutable files in Linux

Chattr

chattr is a command in the Linux operating system that allows a user to set certain attributes on a file residing on many Linux filesystems.

touch filetest.txt

To display the attributes
lsattr filetest.txt
------------- filetest.txt

To set file attributes
chattr +i filetest.txt

lsattr filetest.txt
----i-------- filetest.txt

To test the functionality
mv filetest.txt file.txt
mv: cannot move `filetest.txt' to `file.txt': Operation not permitted

rm -rf filetest.txt
rm: cannot remove `filetest.txt': Operation not permitted

echo "HI" > filetest.txt
-bash: filetest.txt: Permission denied

To remove the immutable attribute
chattr -i filetest.txt
lsattr filetest.txt
------------- filetest.txt

To append operations
chattr +a filetest.txt
lsattr filetest.txt
-----a------- filetest.txt

test the functionality
rm -rf filetest.txt
rm: cannot remove `filetest.txt': Operation not permitted
mv filetest.txt file.txt
mv: cannot move `filetest.txt' to `file.txt': Operation not permitted
echo "replacing content" > filetest.txt
-bash: filetest.txt: Operation not permitted
echo "appending new content" >> filetest.txt
appending new content

To remove the immutable attribute
lsattr filetest.txt
------------- filetest.txt

How to Recursively change attributes of directories and their contents.

chattr -R +i test
lsattr test/filetest.txt
----i-------- test/filetest.txt

chattr -R -i test
lsattr test/filetest.txt
------------- test/filetest.txt

chattr -R +a test
lsattr test/filetest.txt
-----a------- test/filetest.txt
chattr -R -a test

Ref:- http://www.adminarticles.com/file-attributes/

No comments:

Post a comment