How to prevent OpenVZ containers from rewriting files like /etc/resolv.conf or /etc/hosts

OpenVZ is known to replace the content in files like /etc/resolv.conf and /etc/hosts with a template but there is a way to prevent this from happening!

The only thing you have to do is to set the immutable-bit inside your container for each file you want to keep as it is.

For example: chattr +i /etc/hosts

That's it! Now, after each reboot of the container, these files won't be updated anymore.

But please keep in mind that you'd have to remove the immutable-bit before you want to change these files (like for touch, rm, edit, etc.).

For example: chattr -i /etc/hosts

Add Feedback