How to Use the chattr Command to Make a File Immutable in Linux

In the Linux operating system, security is paramount. Every single file on a server can be maliciously deleted or accidentally overwritten if it is left unprotected. The chattr (Change Attribute) command is one of the most powerful security tools available to a system administrator. Unlike standard file permissions, which simply dictate who can read or write a file, chattr allows you to make a file completely “immutable.” When a file is immutable, it is mathematically locked; absolutely no one—not even the absolute root administrator—can delete it, rename it, or modify its contents.

Why Use the Immutable Flag?

The immutable flag is primarily used to protect critical system configuration files (like /etc/resolv.conf or /etc/shadow) from being overwritten by aggressive automated scripts, rogue software updates, or malicious hackers who have gained root access.

How to Make a File Immutable

Because the immutable flag restricts the root user, only the root user (via sudo) has the authority to apply it.

  1. Open your Linux terminal.
  2. Identify the critical file you want to protect (e.g., a massive server database named database.sql).
  3. Type the following command, using the +i (plus i) flag to add the immutable attribute:

sudo chattr +i database.sql

  1. Press Enter.

The terminal will not print a confirmation message. However, the file is now permanently locked.

Testing the Lock

If you attempt to delete the file using standard methods:

rm database.sql

Linux will instantly reject the command, outputting an error: “Operation not permitted.” Even if you attempt to use sudo rm, the system will completely block you.

How to Unlock the File

If you need to update the database or edit the configuration file, you must deliberately strip the immutable attribute away before the system will allow any changes.

To remove the lock, you run the exact same command, but swap the plus sign for a minus sign (-i):

sudo chattr -i database.sql

Once you press Enter, the lock is instantly removed, and the file can be edited or deleted normally.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.