Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

chattr

Set or remove file attributes, like immutable (no change at all) and append only.

Level
Everyday Linux
Group
Permissions
Needs root
Yes
Package
e2fsprogs

What it does

Attributes work below the normal permissions. A file with +i (immutable) cannot be changed, deleted, renamed or linked — not even by root — until the attribute is removed with chattr -i. With +a you can only add data at the end.

Only root (with the right capability) can set i and a. Support depends on the file system: ext4 knows many attributes; XFS supports a smaller set (like i, a, d, S, A, s… only some).

How to write it

chattr [-RVf] [-v VERSION] [-p PROJECT] [+-=ATTRIBUTES] FILE...

Main parts

+ - =
Add, remove, set exactly.
i
Immutable: no change, delete, rename, link.
a
Append only: only add data (good for logs).
A
Do not update the access time.
d
Skip the file in dump backups.
S
Write changes to disk at once (synchronous).
c
Compress (some file systems).
C
No copy-on-write (btrfs).
e
The file uses extents (ext4, cannot be removed).
u, s
Undeletable / secure delete (not used by ext4).

Options 5

Most used

-R
Change directories and their content recursively.
-V
Verbose; also show the version.
-f
Do not show most errors.
-v VERSION
Set the version/generation number.
-p PROJECT
Set the project number (for project quotas).

Examples 10

$ run as a normal user# run as root (or with sudo)

  1. 1Protect a file against every change

    chattr +i /etc/resolv.conf

    Stops tools (and people) from changing DNS settings.

  2. 2Show the attributes

    lsattr /etc/resolv.conf
    ----i----------------- /etc/resolv.conf

    The i is set.

  3. 3Even root cannot delete it

    rm -f /etc/resolv.conf
    rm: cannot remove '/etc/resolv.conf': Operation not permitted

    Remove the attribute first.

  4. 4Remove the protection

    chattr -i /etc/resolv.conf

    Now normal permissions count again.

  5. 5Make a log file append only

    chattr +a /var/log/app/audit.log

    An attacker cannot delete old lines (but can add new ones).

  6. 6Protect important files against ransomware or mistakes

    chattr -R +i /srv/archive/2025

    Old data that must never change.

  7. 7Find immutable files when a change fails

    lsattr -R /etc 2>/dev/null | grep -- '-i-'

    "Operation not permitted" as root often means i.

  8. 8Check for attacker tricks (security)

    lsattr /etc/passwd /etc/shadow /root/.ssh/authorized_keys

    Attackers set +i on their backdoor files so admins cannot remove them.

  9. 9No access time updates for a busy file

    chattr +A /srv/cache/data.db

    Fewer writes (most systems use relatime anyway).

  10. 10Set exactly these attributes

    chattr =a /var/log/app/audit.log

    Removes all others.

Watch out

  • An immutable file breaks updates: package updates and config tools fail on it. Document where you use it.
  • Not all file systems support all attributes ("Operation not supported").

Exit codes

0 :: Done. 1 :: Error.