Permissions and owners

The chattr command

Changes special attributes of a file on ext2, ext3 and ext4 systems, such as making it immutable so that not even root can delete it. It does not change who can read or write: that is chmod's job.

Syntax

chattr [-R] [-V] [+-=]atributos archivo...Try it in the terminal →

Most useful options

+iImmutable: it cannot be written, renamed, linked or deleted, not even by root
+aAppend only: data can be added at the end, but earlier content cannot be rewritten or deleted. Ideal for logs
+ADoes not update the last access time, avoiding writes on slow disks
-RApplies the change to the folder and all its contents
-VShows each change as it makes it

Copy-ready examples

$ sudo chattr +i /etc/resolv.conflocks the file against changes, even by root. Use it with care.
$ lsattr /etc/resolv.confshows the attributes: the i appears when the file is locked.

Example output

----i---------e------- /etc/resolv.conf
$ sudo chattr -i /etc/resolv.confremoves immutability: without this step you cannot edit or delete the file.
$ sudo chattr +a registro.logthe file only accepts appends at the end. Useful for logs that must not be rewritten.
$ sudo chattr -R +A cache/the whole folder stops updating access times, saving writes.

You can try them in the browser terminal: it does not touch your computer.

Common errors

MessageCauseFix
chattr: Operation not permitted while setting flags on archivo.txtOnly root can set or clear i and a. Without privileges, chattr cannot touch the attribute.Use sudo chattr +i file. To edit an immutable file, first remove the i with sudo chattr -i.
rm: cannot remove 'archivo.txt': Operation not permittedThe file has the i attribute: nobody can delete or rename it, not even root.Check with lsattr. If the i is not wanted, remove it with sudo chattr -i and delete afterwards.

Typical mistakes

  • chattr is not chmod: it does not change who can read or write, but attributes of the file itself. A file with +i cannot be written, not even by root.
  • cp creates a new copy without these attributes: the copy does not inherit the lock.

Differences from similar commands

  • chmod: chmod controls who can read, write or execute; chattr adds protections that even root cannot bypass without removing them first.
  • lsattr: shows the file attributes. chattr changes them.

Security

chattr +i also locks the system out: if a program needs to write that file, it fails. Anyone with root can remove the attribute, so it is not a complete defense.

Related commands

FAQ

Why can I not delete a file even as root?

Because it has the i attribute. Check the attributes with lsattr and remove it with chattr -i.

What is the difference between i and a?

i locks everything: no writing, renaming or deleting. a only allows appending, useful for logs.

Learn to actually use it

The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.

Start the free course