Home·Terminal commands

chattr

Permissions

Usage

sudo chattr +i [file]

Sets filesystem-level attributes such as immutable; with +i even root must clear the flag before editing, and it works only on Linux filesystems like ext4, xfs and btrfs.

Common flags

FlagMeaning
+iImmutable: not even root may change or delete it until -i
-iClear the immutable flag so the file can be edited again
+aAppend only, which suits a log file
+ADo not update the access time on reads
-RApply through a whole tree
lsattrThe companion command that shows which flags are set

Examples

sudo chattr +i /etc/resolv.conf

Stops anything from overwriting the file.

lsattr /etc/resolv.conf

Shows the i in the flag list.

sudo chattr -i /etc/resolv.conf

Removes it again so you can edit.

Numeric and symbolic modes name the same thing: 755 and u=rwx,go=rx are the same permission.

How to read this

  • Square brackets [ ] mark a part you may leave out.
  • An ellipsis … means you can list more than one.
  • Flags are case-sensitive — in some commands -r and -R do different things.

Questions

Q. What does chattr do?

Sets filesystem-level attributes such as immutable; with +i even root must clear the flag before editing, and it works only on Linux filesystems like ext4, xfs and btrfs.

Q. How do I type it?

sudo chattr +i [file] — square brackets mark the parts you can leave out.

Q. How many flags are worth knowing?

6 are listed here; the full set is in man chattr. This command sits under Permissions.

Related commands

man page: man chattr