Linux • User and File Management
Comments >
how to

Making a file unalterable with chattr

You can make a file unalterable so that it cannot be changed or deleted even by root:

$ chattr +i /path/to/file

After that the file becomes untouchable.

To update the file, remove the attribute using -i flag:

$ chattr -i /path/to/file

Use -R flag to unlock a directory.

$ chattr -R -i directory/

The Mac equivalent is:

#to "lock" the file $ chflags uchg /path/to/file #to "unlock" $ chflags nouchg /path/to/file

Comments

Is this file removable by a live disk?
@jmberd02a year ago
Is the order of arguments important?
@ritzbitz25a year ago
What's the purpose of making a file untouchable? i.e. A use case
@adamyalaa year ago
The order of args doesn't matter. Fix it ;)
@sergiohgza year ago
@adamyala It's useful for making sure you don't alter a file by mistake. Let's say you are often cleaning your /sandbox, but have some config files there you always want to keep. This way you won't delete them just because you forgot they're there.
@minglessa year ago
Can we lock a dir the same way? Or me files recursively in a dir?
@Mass.sow10 months ago
The "i" stands for immutable, which is the only way I remember this exists.
@Bharrison9 months ago
I agree with @Bharrison; the word "immutable" should appear somewhere in this slide.
@igal7 months ago

The 5-minute daily workout for your dev skills