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.
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.
$ 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
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.
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
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
iblocks even root. To change or remove such a file, first runchattr -i. That is the entire point: a script or intruder must change the flag first.ls -ldoes not show attributes. Ifrmfails with "Operation not permitted" for a file you own and the parent directory allows deletion, checklsattr.afor 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:
tarandcpdo not preserve attributes by default.rsync -X/--fake-superortar --xattrshandle different pieces;cp --preserve=allcopies what it can. - Flags are separate from extended attributes (
getfattr/setfattr) and from ACLs (getfacl). See ACLs and attributes. - Do not set
ion files that programs must update, such as/etc/resolv.confmanaged by a network service. They will fail in confusing ways. Use it on files you really mean to freeze. eappears on ext4 files and is informational: it is not an error and cannot be removed.