close
» » How to disallow changing a file in Linux

How to disallow changing a file in Linux

Section: System | Actuality: Linux Mint 19.1 Tessa
How to disallow changing a file in Linux
If for some reason you need to prevent the modification of any file in your system, this article is for you.
The reasons are different, I have heard that sometimes the system (or some other program) change some sort of config file and you have it each time to recover. Or you are afraid that your child will change some important file, such as the config of your favorite game (CS:GO)… There are a lot of application options.
Today we will talk about the chattr command. Yes, similar functionality can be achieved in the usual chmod and chown, but still with the good old chattr prohibit any file to be subjected to any changes can be more effective. And no matter who tries — you, the administrator, or the system itself — no one can affect the contents of this file!
Chattr itself is very versatile. It is designed to manage the attributes of the file, and has a fairly wide range of tools, but today we are interested in how to prevent the file from changing.
Let’s create a file! Open the terminal in the home directory and execute:
touch unchangeable.txt
How to disallow changing a file in Linux
This will create a text file named «unchangeble.txt». We’ll need it for experiments.
The following command propose to add some text. For example, «MintGuide.org — Hello from Mr. Shekin». Do this in a text editor or run a command like this:
echo 'MintGuide.org - Hello from Mr. Shekin' > ~/unchangeable.txt
How to disallow changing a file in Linux
Let’s forbid anyone to change it!
sudo chattr +i ~/unchangeable.txt
How to disallow changing a file in Linux
Now you can try to delete it. Not deleted! Even through sudo! Even from root! You also won’t be able to rename the file or make changes to its contents.
If you want to make the file editable again, just run:
sudo chattr +i ~/unchangeable.txt
Then you can perform the actions on the file again. Access rights the file will be the same as before the protection from modification.
May the Force be with you,
Good Luck!!!
Chattr

Print version

    Your working environment Other polls