Protect Important Files with Immutable Attribute
In this final step, we will learn how to use the immutable attribute to protect important files from being accidentally or maliciously modified or deleted.
Let's start by creating an important file that we want to protect:
echo "This is an important file" > ~/project/important.txt
Now, let's set the immutable attribute on the file:
sudo chattr +i ~/project/important.txt
Example output:
$ sudo chattr +i ~/project/important.txt
With the immutable attribute set, the file cannot be deleted, renamed, or modified, even by the root user. Let's try to delete the file:
rm ~/project/important.txt
Example output:
$ rm ~/project/important.txt
rm: cannot remove '~/project/important.txt': Operation not permitted
As you can see, the rm
command failed to delete the file due to the immutable attribute.
Next, let's try to modify the file:
echo "Trying to modify the file" > ~/project/important.txt
Example output:
$ echo "Trying to modify the file" > ~/project/important.txt
-bash: ~/project/important.txt: Operation not permitted
Again, the operation is not permitted because the file has the immutable attribute set.
To remove the immutable attribute and allow modifications, you can use the following command:
sudo chattr -i ~/project/important.txt
Example output:
$ sudo chattr -i ~/project/important.txt
Now, you can freely modify or delete the file.
The immutable attribute is a powerful tool for protecting important files and directories from accidental or malicious changes. By setting this attribute, you can ensure that critical system files, configuration settings, or sensitive data remain intact, even in the face of user errors or security breaches.