Skip to content

Make OS file non-editable in Linux

The chattr command in Linux is a file system command which is used for changing the attributes of a file in a directory. The primary use of this command is to make OS files unable to alter for users other than the superuser.

Here is the basic syntax for using chattr:

chattr [options] [attributes] [file or directory]

The options that can be used with chattr include:

+: add an attribute
-: remove an attribute
=: set the specified attributes and remove any others

The attributes that can be used with chattr include:

a: append mode, which prevents data from being overwritten in the file
i: immutable, which prevents the file from being deleted or modified
d: no dump, which prevents the file from being included in backups
s: secure deletion, which overwrites the file’s data with zeroes when it is deleted

To check the immutable flag:

lsattr file.txt

To make a file immutable (non-editable), you can use the following command:

chattr +i file.txt

To remove the immutable flag:

chattr -i file.txt

To check the immutable flag:

lsattr file.txt

A file with the `i’ attribute:

  • Cannot be deleted or renamed.
  • No link can be created to this file.
  • No data can be written to the file.
  • Only the superuser or a process possessing the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.
  • Even file owner can not edit/move/delete/update file.

Note: Only the root/superuser can use chattr command to modify an immutable file.

Brijesh Gogia
Leave a Reply