Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Files and Directories

Linux chattr and lsattr commands

Reference for chattr and lsattr: list and change extended file attributes such as immutable (+i), append-only (+a) and no-atime (+A) on ext4 and similar file systems.

Beyond the permission bits shown by ls -l, Linux file systems such as ext2, ext3, ext4, XFS (a subset) and Btrfs keep attribute flags on every file. lsattr lists them and chattr changes them. The most famous are i (immutable: the file cannot be modified, renamed, deleted or linked to, even by root, until the flag is removed) and a (append-only: data can only be added at the end).

These flags are enforced by the kernel regardless of who runs the command, so they protect important files from accidents and from many kinds of tampering. Setting i and a requires root (the CAP_LINUX_IMMUTABLE capability); several other flags can be set by the file's owner.

Terminal
lsattr [-adR] [FILE]...
chattr [-RVf] [+-=]FLAGS FILE...

Try it

Setting i and a needs root, which our runner does not give you, so those commands are shown read-only first.

Terminal
$ sudo chattr +i /etc/important.conf
$ rm /etc/important.conf
rm: cannot remove '/etc/important.conf': Operation not permitted
$ sudo chattr -i /etc/important.conf
Bash List and set flags you may change

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Directories and what lsattr shows

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
lsattr [-adlpRv] [FILE]...
chattr [-RVf] [-v VERSION] [-p PROJECT] +FLAGS | -FLAGS | =FLAGS FILE...
Option Description
lsattr -a Include hidden entries in directory listings.
lsattr -d List directories as themselves, not their contents.
lsattr -R Recurse into directories.
chattr -R Recurse into directories.
chattr -f Suppress most error messages.
+FLAGS, -FLAGS, =FLAGS Add flags, remove flags, or set exactly this set.

Common flags (supported ones depend on the file system):

Flag Meaning Who may set it
i Immutable: no change, rename, delete or new links root
a Append-only root
A Do not update the access time owner
d Skip in dump backups owner
c Compress on write (rarely implemented) owner
S Synchronous updates owner
D Synchronous directory updates owner
u Keep content when deleted for undelete owner
e Uses extents (set by the file system) read-only
C No copy-on-write (Btrfs) owner
j Data journaling (ext3/4) root
T Top of a directory hierarchy for the Orlov allocator owner

Returns: 0 on success; 1 when a flag could not be read or set ("Operation not permitted", "Inappropriate ioctl for device" on file systems that do not support attributes).

Usage notes

  • i blocks even root. To change or remove such a file, first run chattr -i. That is the entire point: a script or intruder must change the flag first.
  • ls -l does not show attributes. If rm fails with "Operation not permitted" for a file you own and the parent directory allows deletion, check lsattr.
  • a for logs: an append-only log file cannot be truncated or rewritten, only added to, which makes tampering harder. Rotation tools must clear the flag first.
  • Not every file system understands every flag, and some (tmpfs, overlay, NFS, FAT) reject attributes entirely. Check the error message.
  • Backups: tar and cp do not preserve attributes by default. rsync -X/--fake-super or tar --xattrs handle different pieces; cp --preserve=all copies what it can.
  • Flags are separate from extended attributes (getfattr/setfattr) and from ACLs (getfacl). See ACLs and attributes.
  • Do not set i on files that programs must update, such as /etc/resolv.conf managed by a network service. They will fail in confusing ways. Use it on files you really mean to freeze.
  • e appears on ext4 files and is informational: it is not an error and cannot be removed.