chattr (change attribute) is the Linux command that sets filesystem-level file attributes, flags stored in the inode that change how the kernel treats a file regardless of its owner and mode bits. Its companion command lsattr lists those attributes. The best-known flags make a file immutable or append-only, so that even the root account cannot modify it until the flag is removed.
Within Linux permissions, chattr sits beside chmod, chown and Linux ACLs but works at a different layer. Ordinary permissions decide who may do what; attributes decide what may be done at all. That distinction makes chattr a useful hardening tool and, when forgotten, a frequent cause of baffling “Operation not permitted” errors.
How chattr attributes work in the kernel
Attributes are stored as a bit field in the inode of filesystems that support them, chiefly ext2, ext3, ext4, XFS, Btrfs and F2FS. chattr changes them through the FS_IOC_SETFLAGS ioctl, and lsattr reads them with FS_IOC_GETFLAGS. The kernel checks these flags before ordinary permission checks on write, rename, unlink and truncate operations.
Setting or clearing the immutable and append-only flags requires the CAP_LINUX_IMMUTABLE capability, which root normally holds. A process without it, including a root process inside a container that dropped the capability, cannot remove the protection. This is why chattr can stop accidental or scripted damage, yet it is not a defense against a full root compromise: an attacker with that capability can simply run chattr -i first.
The syntax uses an operator and a set of letters: chattr +i file adds a flag, chattr -i file removes it, and chattr =ai file sets exactly those flags. The -R option applies changes recursively to a directory tree, and -V prints each change.
The most important chattr attributes
Dozens of letters exist, but only a handful are used routinely and support varies by filesystem. The table below lists the attributes administrators encounter most often.
| Flag | Name | Effect | Filesystem support |
|---|---|---|---|
| i | Immutable | No write, rename, delete or link; metadata frozen | ext4, XFS, Btrfs, F2FS |
| a | Append-only | Data may only be added at the end | ext4, XFS, Btrfs, F2FS |
| A | No atime updates | Access time is not recorded | ext4, XFS, Btrfs |
| C | No copy on write | Disables CoW for new files | Btrfs |
| c | Compressed | Transparent compression | Btrfs, F2FS |
| d | No dump | Skipped by the dump backup tool | ext4, XFS, Btrfs |
| S | Synchronous updates | Writes reach disk immediately | ext4, Btrfs |
| e | Extents | File uses extent mapping; cannot be cleared | ext4 |
lsattr shows the flags as a string of letters and dashes, for example ----i---------e------- /etc/resolv.conf. The e flag appears on almost every ext4 file and is informational, so its presence is normal and not a sign of tampering.
chattr compared with other permission tools
Linux offers several overlapping mechanisms for controlling file access, and each answers a different question. Understanding where chattr fits prevents applying the wrong tool to a problem.
| Mechanism | Controls | Applies to root | Typical purpose |
|---|---|---|---|
| chmod | Read, write, execute bits | Largely bypassed | Basic owner, group and other access |
| chown | Owner and group | Not applicable | Assigning responsibility for files |
| Linux ACL | Per-user and per-group entries | Largely bypassed | Fine-grained sharing |
| chattr | Allowed operations on the inode | Yes, until flag removed | Preventing modification or deletion |
| Read-only mount | Entire filesystem | Yes, until remounted | Protecting whole volumes |
| SELinux or AppArmor | Process-to-object policy | Yes, by policy | Mandatory access control |
The decisive difference is that root bypasses mode bits and ACLs through CAP_DAC_OVERRIDE, but not attribute flags. Mandatory access control frameworks are stronger still because their policy can deny even the capability needed to clear the immutable flag.
Practical uses of immutable and append-only files
Attributes are most valuable where a file must stay stable despite automated tools or careless commands. Each common use case calls for a specific flag, as the following decision table shows.
| Situation | Command | Reason |
|---|---|---|
| Stop NetworkManager rewriting resolv.conf | chattr +i /etc/resolv.conf | Locks a file other services overwrite |
| Protect a log from truncation | chattr +a /var/log/audit.log | New entries allowed, history preserved |
| Store VM images on Btrfs | chattr +C on an empty directory | Avoids CoW fragmentation |
| Guard critical account files | chattr +i /etc/passwd | Blocks scripted account changes |
| Reduce writes on flash storage | chattr +A on busy directories | Skips access time updates |
The C flag must be set on an empty file or directory to take effect; applying it to a file that already contains data has no reliable result on Btrfs. Append-only logs work best when the logging daemon opens files with O_APPEND, otherwise writes fail.
Pitfalls and troubleshooting
Most problems with chattr come from forgotten flags rather than from the tool itself. Because ls does not display attributes, a protected file looks entirely ordinary until an operation fails.
- Package upgrades fail when a package manager cannot replace an immutable file such as
/etc/passwd; clear the flag before upgrading. - Running
rm -rfas root on an immutable file returns Operation not permitted, and many administrators first suspect permissions; run lsattr to check. - Attributes are lost when copying with tools that do not preserve them, and tar or rsync need explicit options to handle them.
- Network filesystems such as NFS and tmpfs generally do not support attributes, and chattr returns an error there.
- Attackers sometimes set +i on malware or altered binaries to hinder cleanup, so incident responders routinely run
lsattr -Ron suspicious paths.
The idea in brief
Normal Linux permissions say who is allowed to open or change a file. chattr adds a different kind of lock: it says what may happen to the file at all, no matter who asks.
With the immutable setting, a file is like a document sealed in a glass case. Even the administrator cannot edit or delete it until the case is deliberately unlocked. With append-only, the file behaves like a guest book where you can add a line but never erase one. The key point is that these locks are invisible to the usual listing commands, so lsattr is the way to see them.
A practical scenario
An administrator runs a small fleet of Linux servers that must use an internal DNS resolver. After every reboot, a network management service rewrites /etc/resolv.conf with the DHCP-provided resolver, and internal hostnames stop resolving. Instead of disabling the service entirely, the administrator writes the correct file and runs chattr +i /etc/resolv.conf through the configuration management system.
The service now logs a failed write but the correct resolver stays in place across reboots. Months later, a colleague cannot update the file and sees Operation not permitted despite being root. Running lsattr /etc/resolv.conf reveals the i flag, the colleague clears it with chattr -i, makes the change and sets the flag again. The team then documents every immutable file in the runbook, which prevents the same confusion during later upgrades.