Best chattr command to change File Attributes – Making Important Files Immutable
Introduction
In this article we are going to discuss on how to use chattr command to change file attributes in linux and make the important files immutable. chattr stands for Change Attribute. chattr command is very nice and useful tool in linux is used to change file attributes and has the ability to make the file immutable. Immutable means once I set the attribute for some file by using chattr command then you will not be able to Move the file, Delete the file, create link of the file and edit the file even if you have full access for that file. root user also cannot do any changes to that file till attribute is applied. you can use chattr command to set and unset the attributes of the file. root user only has access to set or unset attributes of files and directories using chattr command.
So let’s have a look at some examples of chattr command to change File Attributes
Set “i” Attribute to a File
Suppose I have a file named elinuxbook.txt which is accessible to everyone, that means any user can come and delete, move or edit that file. Refer the output below.
[root@localhost ~]# ls -l elinuxbook.txt -rwxrwxrwx. 1 root root 0 Apr 24 03:59 elinuxbook.txt # Everyone has full access to "elinuxbook.txt" [root@localhost ~]# rm elinuxbook.txt # I am able to delete the file rm: remove regular empty file `elinuxbook.txt'? y
Now let’s set Attribute to elinuxbook.txt file using chattr command. To set attribute we have to use “+” sign and to unset attribute we have to use “–” sign.
[root@localhost ~]# chattr +i elinuxbook.txt # Setting Attribute to a file
Where :
i – Immutable
You can also use -V option to check the Verbose output while setting attribute to a file.
[root@localhost ~]# chattr -V +i elinuxbook.txt # Setting attribute to a file with -V
chattr 1.41.12 (17-May-2010)
Flags of elinuxbook.txt set as ----i--------e-
So we set attribute to the file elinuxbook.txt. to confirm the same you can use lsattr command. Refer the sample output below. You will notice a i (Highlighted in Red color) on permission section of the file.
[root@localhost ~]# lsattr elinuxbook.txt # confirm if attribute set or not ----i--------e- elinuxbook.txt
Now let’s try to Remove, delete, Move and change the permission of the file and I am sure you can’t do any one of that.
- Remove the File
[root@localhost ~]# rm elinuxbook.txt # Removing the File rm: remove regular empty file `elinuxbook.txt'? y rm: cannot remove `elinuxbook.txt': Operation not permitted # You can also try to remove the file forcefully. [root@localhost ~]# rm -rf elinuxbook.txt # Removing the file forcefully rm: cannot remove `elinuxbook.txt': Operation not permitted
- Move the File
[root@localhost ~]# mv elinuxbook.txt test.txt # Move the file
mv: cannot move `elinuxbook.txt' to `test.txt': Operation not permitted
- Edit the File
[root@localhost ~]# cat >> elinuxbook.txt bash: elinuxbook.txt: Permission denied
- Change Permission of the File
[root@localhost ~]# chmod 755 elinuxbook.txt chmod: changing permissions of `elinuxbook.txt': Operation not permitted
As you can see on all above examples we are unable to do any changes to that file.
To Remove “i” attribute use below command.
[root@localhost ~]# chattr -i elinuxbook.txt # Unset "i" attribute
After removing the attribute you will see the permission section will become blank.
[root@localhost ~]# lsattr elinuxbook.txt --------------- elinuxbook.txt
Now let’s try to Secure a directory by changing it’s attribute recursively using chattr command.
Here I have a directory named data and everyone have full access to that directory recursively. Refer the sample output below.
[root@localhost office]# mkdir data [root@localhost office]# chmod -R 777 data/ [root@localhost office]# ls -l total 4 drwxrwxrwx. 2 root root 4096 Apr 24 04:25 data
Now set attribute to that directory.
[root@localhost office]# chattr +i data/ [root@localhost office]# lsattr ----i--------e- ./data # You can also set attribute Recursively using -R option with chattr. [root@localhost office]# chattr -R +i data/
After setting the attribute to the directory now try to delete, move or create a file, I am sure you will not allowed to do any one of that. Refer the sample output below.
[root@localhost office]# rmdir data/ # Deleting the Directory rmdir: failed to remove `data/': Operation not permitted [root@localhost office]# rm -rf data/ # Deletiing the Directory Forcefully rm: cannot remove `data': Operation not permitted [root@localhost ~]# mv data/ mydata # Moving the Directory mv: cannot move `data/' to `mydata': Operation not permitted [root@localhost office]# cd data/ [root@localhost data]# cat > test.txt # Creating a File in the directory bash: test.txt: Permission denied
Also Read :
- MANAGING USERS AND GROUPS IN LINUX – A COMPLETE GUIDE FOR BEGINNERS
- BEST ZIP COMMAND WITH EXAMPLES IN LINUX
- BEST RPM COMMAND ( REDHAT PACKAGE MANAGER ) WITH EXAMPLES IN LINUX
Where we can actually make use of chattr command ?
Let’s take an Example : As a Linux administrator obviously you don’t want anyone to be access you configuration files, make changes on any files or remove any configuration files or do any misuse of it. It’s your responsibility to make it secure and keep safe from wrong hand who don’t have the authorise to access it. We can secure our all configuration stuff’s by using chattr command.
In Linux, all configuration files are stored in /etc directory. If we set attribute to /etc directory then no can able to access any of your configurations. So let’s do that.
[root@localhost ~]# chattr +i /etc/ # Setting attribute to /etc directory
Now let’s try to do some tasks :
Examples : 1 Create a Group
[root@localhost ~]# groupadd g5 groupadd: cannot lock /etc/group; try again later.
Example : 2 Set password for any User
[root@localhost ~]# passwd michelle Changing password for user michelle. New password: Retype new password: passwd: Authentication token manipulation error
Example : 3 Create a New User
[root@localhost ~]# useradd elinuxbook useradd: cannot lock /etc/passwd; try again later.
As you can see above we unable to do some tasks like create a new user, set password for any user, create a new group. we can’t do all this tasks because when we create a new user or set password for any user it updates the /etc/passwd file and /etc/shadow file which is not possible here as we set attribute for complete /etc directory.
Note : Here I set attributes to complete /etc directory to just explain you as an example. But you can set file attributes as per your need for example if you want to just control user and group management then you don’t need to set attribute for complete /etc directory you can set only for /etc/passwd and /etc/shadow and for groups set attribute for /etc/group. If you want to control Filesystem Table then set attribute for /etc/fstab and so on.
Now let’s take another example and unmount a filesystem. Refer the sample output below.
Example : 4 Unmount a File System
[root@localhost ~]# umount /media/ # Unmounting a File System
can't create lock file /etc/mtab~2762: Permission denied (use -n flag to override)
We are also unable to unmount a filesystem. To do all above tasks we have to unset attributes that we have applied for /etc directory.
Unset attribute by using chattr command
We can unset attribute by using chattr command with option -i
[root@localhost ~]# chattr -Vi /etc/ # Removing Attributes from directory
chattr 1.41.12 (17-May-2010)
Flags of /etc/ set as ----------I--e-
Allow to append a File using chattr command
You can allow a file to append data using chattr command with option +a. By applying this attribute you are only allowed to write data on that file and not allowed to delete and move.
Here I am allowing users to append data on elinuxbook.txt file.
[root@localhost ~]# chattr +a elinuxbook.txt # Setting +a Attribute
To check the applied attribute use below command. You will notice a at permission section.
[root@localhost ~]# lsattr elinuxbook.txt
-----a-------e- elinuxbook.txt
As you can see below we able to see the content of the file.
[root@localhost ~]# cat elinuxbook.txt Welcome to elinuxbook.com
Now let’s try to append some data in the file.
[root@localhost ~]# cat >> elinuxbook.txt # Writing some data Here you will get Linux Tutorials # Now confirm the same by using cat command [root@localhost ~]# cat elinuxbook.txt Welcome to elinuxbook.com Here you will get Linux Tutorials
So we can successfully append data in elinuxbook.txt. Now let’s try to delete the file.
[root@localhost ~]# rm elinuxbook.txt # Deleting the File rm: remove regular file `elinuxbook.txt'? y rm: cannot remove `elinuxbook.txt': Operation not permitted [root@localhost ~]# rm -rf elinuxbook.txt # Deleting the File Forcefully rm: cannot remove `elinuxbook.txt': Operation not permitted
For more information related chattr command you can use below commands on your linux system.
[root@localhost ~]# man chattr [root@localhost ~]# man lsattr
Have look at some useful chattr command Options :
- +i – A File with +i attribute cannot be delete, move, rename. in short cannot be modified.
- -i – This option allows to remove i attribute from the file.
- -V – To see the Verbose output
- -a – By using this attribute will only allow to append data on a file and cannot be delete or move.
If you found this article useful then Like Us, Subscribe Us or If you have something to say then feel free to comment on the comment box below.