How to Use the ‘chattr’ Command to Make Files Immutable in Linux

The Limitation of Standard Permissions

Linux security relies heavily on standard file permissions (Read, Write, Execute). If you own a file, or if you have root (administrator) privileges, you can modify or delete that file.

However, there are scenarios where even the root user should be prevented from accidentally modifying a file. For example, you might have a core DNS configuration file (resolv.conf) that a rogue DHCP script keeps automatically overwriting. Or you might have a massive forensic log file that must be preserved exactly as it is for legal compliance.

To completely lock down a file so that absolutely no one—not even the superuser—can delete, rename, or modify it, you must use the chattr (Change Attribute) command.

The Immutable Flag (+i)

The chattr command interacts directly with the ext2/ext3/ext4 and XFS file systems, applying hidden attributes that sit beneath the standard permission layer.

The most powerful attribute is the Immutable flag (i).

How to Lock a File

Suppose you have a file named critical_config.cfg. To make it immutable, you must use sudo:

sudo chattr +i critical_config.cfg

Once this command is executed, the file is frozen in time. If you try to delete it using rm, or edit it using nano, or rename it using mv, Linux will immediately reject the action with an “Operation not permitted” error. Even if you are logged in as root.

How to Unlock a File

To modify the file again, you must explicitly remove the immutable attribute by swapping the plus (+) for a minus (-):

sudo chattr -i critical_config.cfg

The Append-Only Flag (+a)

Sometimes, locking a file completely is too restrictive. Consider a security audit log (/var/log/auth.log). You want the system to be able to write new log entries to the bottom of the file, but you absolutely want to prevent a hacker from opening the file and deleting the evidence of their intrusion.

For this, you use the Append-Only flag (a).

sudo chattr +a /var/log/auth.log

With this flag set, programs can use the >> operator to add new lines to the end of the file. However, any attempt to overwrite the file, open it in a text editor to change past entries, or delete the file entirely will be blocked by the kernel.

To remove the append-only restriction:

sudo chattr -a /var/log/auth.log

Verifying Attributes with lsattr

Because these attributes operate beneath the standard permission layer, running a standard ls -l command will not show them. If a file is mysteriously undeletable despite you having full read/write permissions, an attribute is likely the culprit.

To view the hidden attributes of a file, use the lsattr (List Attributes) command:

lsattr critical_config.cfg

The output will look something like this:

----i---------e------- critical_config.cfg

The presence of the i confirms the file is currently immutable.

Conclusion

The chattr command is the ultimate safety net in the Linux filesystem. By applying immutable and append-only flags, administrators can protect mission-critical configurations and forensic logs from rogue scripts, human error, and malicious tampering.

RELATED POSTS

  • How to Use the find Command to Locate Files Modified in the Last 24 Hours in Linux
  • How to Verify File Integrity Using the md5sum Command in Linux
  • How to Use the Linux lspci Command to List Peripheral Hardware
  • How to Use the Linux hostnamectl Command to Set the System Hostname
  • How to Use the Linux cmp Command to Compare Two Files Byte by Byte
  • Get the best tech tips delivered straight to your inbox.

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