Immutable attributes

This is part of the Semicolon&Sons Code Diary - consisting of lessons learned on the job. You're in the unix category.

Last Updated: 2022-05-26

Sometimes a file may be mysteriously read-only - even though you are root and you own the file. Yet you still cannot delete the file!

This is usually caused by an immutable flag being set.

On Ubuntu, you can determine this with the lsattr command:

$ lsattr my_file
----i---------e--- my_file

The i indicates immutable.

Note carefully that running ls -l will give you no useful info or clues about immutability.

Why might you want to make something immutable?

Perhaps as a quick hack to prevent other programs auto-managing files (e.g. as happens with /etc/resolv.conf and DNS settings).

If you use immutable attributes, document it since most programmers are not aware of it.

Incidentally, here is how to set a file to be immutable with chattr:

sudo chattr +i /backups/passwd

Resources