Chattr Command

Linux chattr Usage (Make Files Untouchable)

Especially in open source scripts such as WordPress or Joomla, you have seen hackers can infiltrate all files by plugins or themes. Hackers can also add a malware code on every file which is really hard to find and prevent. But how you can prevent that kind of attacks and secure your necessary files from modification?

Here at DataKeepers, we recommend you use the chattr command to increase the security of your content management system. You can use the chattr command if you want to prevent modification of your critical and risky files.

Chattr command basically isolates the file from any modification. When you use the chattr command in a file, after that time that file is no longer able to be touched, deleted, or changed. Even if you are login as root user, any attempts will be unsuccessful. That is the most precise method of safety.

To use this command, you must have a server with a virtual or physical Linux operating system. You can use chattr by accessing your Linux server via SSH.

As usage of wordpress and attacts on this cms very much, in this tutorial we like to show you some examples over WordPress.

How to use?

For using chattr command you don’t need to install anything. So it works as soon as you entered the command. Let’s do chattr on the file wp-settings.php and do it untouched;

chattr +i wp-settings.php

After now, it should be untouchable. Let’s try to delete the file named wp-settings.php;

rm -rf wp-settings.php

The file can not be deleted as seen below;

Sample Trial Delete File with Chattr

In this condition, the file mustn’t be edited also, so we are opening the same file with nano editor and trying to add code or text inside of it.

nano wp-settings.php

You can try to record it with the following keyboard key combinations ;

ctrl+x+y+enter

You will see that it will still fail and will not be able to write;

Nano EditorNow as you can see our file became untouchable. As you can guess, applying this code one by one to hundreds of files and making them all untouchable; will takes a lot of time. So if you want to apply chattr to all the files in the entire folder/directory, it is enough to add the R parameter inside the code, you can try the example command below;

If you apply chattr to the wp-admin/ directory, no files under this directory can be changed or deleted.

chattr -R +i wp-admin

After a some time passed you may forgot that which files you applied chattr in past, so you can use the lsattr command to list the directories and files that chattr is applied to. You can determine the applied files and folders with “i” mark. These are untouchables;

lsattr

Lasttr command show immune files

If you want to undo the operation and remove immunity from chattr applied files and folders – the parameter is sufficient (the following becomes now writable);

chattr -i wp-settings.php

Remove chattr from the directory:

chattr -R -i wp-admin

As you can see with a simple command we can secure some of the important areas of wordpress. To be more secure you can also use chattr command to template files, wordpress config file and .htaccess files. Just you need to be careful not to apply this code any cache or upload folder. Also don’t forget that it isn’t a total security option for your files or scripts but with applying this command you are minimizing the risks.

You can use chattr and create untouched files on VPS and Cloud servers with Linux operating systems.

Leave A Comment?