chattr

Also known as: lsattr

On this page 7

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.

FlagNameEffectFilesystem support
iImmutableNo write, rename, delete or link; metadata frozenext4, XFS, Btrfs, F2FS
aAppend-onlyData may only be added at the endext4, XFS, Btrfs, F2FS
ANo atime updatesAccess time is not recordedext4, XFS, Btrfs
CNo copy on writeDisables CoW for new filesBtrfs
cCompressedTransparent compressionBtrfs, F2FS
dNo dumpSkipped by the dump backup toolext4, XFS, Btrfs
SSynchronous updatesWrites reach disk immediatelyext4, Btrfs
eExtentsFile uses extent mapping; cannot be clearedext4
Common attributes set with chattr

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.

MechanismControlsApplies to rootTypical purpose
chmodRead, write, execute bitsLargely bypassedBasic owner, group and other access
chownOwner and groupNot applicableAssigning responsibility for files
Linux ACLPer-user and per-group entriesLargely bypassedFine-grained sharing
chattrAllowed operations on the inodeYes, until flag removedPreventing modification or deletion
Read-only mountEntire filesystemYes, until remountedProtecting whole volumes
SELinux or AppArmorProcess-to-object policyYes, by policyMandatory access control
chattr and related access-control mechanisms

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.

SituationCommandReason
Stop NetworkManager rewriting resolv.confchattr +i /etc/resolv.confLocks a file other services overwrite
Protect a log from truncationchattr +a /var/log/audit.logNew entries allowed, history preserved
Store VM images on Btrfschattr +C on an empty directoryAvoids CoW fragmentation
Guard critical account fileschattr +i /etc/passwdBlocks scripted account changes
Reduce writes on flash storagechattr +A on busy directoriesSkips access time updates
When to use chattr flags

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 -rf as 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 -R on 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.