Using chattr to Make Files Immutable in Linux

This article provides an overview of the chattr command in the Linux operating system, focusing on its ability to make files immutable. You will learn what the immutable attribute does, why system administrators use it to protect critical files from unauthorized modification or accidental deletion, and the exact commands required to apply, verify, and remove this attribute.

What is the chattr Command?

The chattr (change attribute) command is a Linux utility used to modify file attributes on filesystems such as ext2, ext3, ext4, and XFS. Unlike standard file permissions managed by chmod (read, write, execute), file attributes define low-level behavioral rules for files and directories at the filesystem level.

The Purpose of the Immutable Flag (+i)

The primary purpose of applying the immutable flag using chattr is to completely lock a file against any changes. When a file is marked as immutable:

This behavior makes the immutable attribute an essential security and administrative tool. Standard permissions (chmod 777 or chmod 000) still allow the superuser (root) or a user with sudo privileges to delete or alter files. The immutable attribute acts as an explicit safeguard that halts any write or delete operations regardless of user privilege level.

Key Use Cases

How to Use chattr

1. Making a File Immutable

To set the immutable attribute on a file, use the +i operator with root privileges:

sudo chattr +i /path/to/important_file.txt

Once executed, any attempt to remove or modify this file—even by running rm -f as root—will fail with an "Operation not permitted" error.

2. Checking the Attribute

Standard commands like ls -l do not show filesystem attributes. To verify that a file is immutable, use the lsattr command:

lsattr /path/to/important_file.txt

If the file is immutable, the letter i will appear in the output flags:

----i---------e---- /path/to/important_file.txt

3. Removing the Immutable Attribute

To modify or delete the file again, remove the immutable flag using the -i operator:

sudo chattr -i /path/to/important_file.txt

After removing the attribute, standard file permissions and user privileges dictate how the file can be accessed, modified, or deleted.