Linux chattr Command

In this tutorial we are talking about linux chattr command. The purpose of chattr attribute is to stop accidentally delete of file/folder. Even if you have full permission on particular file and files is secured with chattr command, so you cannot remove this particular file. This command is very useful for system administrator. We can apply on important files which contains all user infos and passwords like shadow and passwd files.

Syntax for linux chattr command is

chattr [operator] [switch] [file name]

attribute -i    –    file cannot be modified, renamed and deleted.

Implementation

Login to root user, create a file and set full permission on below mentioned file.

cat > attributed-file
 This is my attributed test file.
chmod 777 attributed-file
ls -l attributed-file
 -rwxrwxrwx 1 root root 28 May 17 01:25 attributed-file

Now apply +i option on following file.

chattr +i attributed-file

After implement on this file, all other actions will be denied only we can read this file. We can implement all actions on perticular file with –i option.

ls -l attributed-file
 -rwxrwxrwx 1 root root 28 May 17 01:25 attributed-file
chattr +i attributed-file
lsattr attributed-file
 ----i--------e- attributed-file
cat attributed-file
 This is my attributed test file.
cat >> attributed-file
 -su: attributed-file: Permission denied
rm attributed-file
 rm: cannot remove `attributed-file': Operation not permitted
mv attributed-file /home
 mv: cannot move `attributed-file' to `/home/attributed-file': Operation not permitted
chattr -i attributed-file
lsattr attributed-file
 -------------e- attributed-file
cat >> attributed-file
 permission is removed.
rm attributed-file

Hope this tutorial will be helpful for you. Enjoy

Avatar photo

Asif Khan

Responsible and proactive professional with more than 13 years of experience in IT systems, open source software applications, DevOps, Linux systems, and cloud operations. My main goals are to automate things, keep them safe, and make sure they are strong. I am very good at planning and building the infrastructure for services that people really want. I was drawn to the fast-paced world of cloud computing because it has resources that can be scaled up or down as needed. One of my best skills is being able to use a lot of different DevOps tools to set up, release management, and microservices ecosystems, as well as for provisioning, orchestration, and configuration management.