LowEndBox - Cheap VPS, Hosting and Dedicated Server Deals

How to Use the Chattr Command in Linux

This article was contributed by Dustin Cisneros at RackNerd, a web hosting and infrastructure as a service provider which was recently voted #1 by the LowEndTalk community! If you are looking for a Linux VPS, Ryzen VPS, Windows VPS, or shared and reseller hosting services, be sure to take a look at RackNerd’s latest special offers here.


What is Chattr? Chattr, which stands for “change attribute” is a function in the command line of Linux that has the ability to set or unset certain attributes in the file and its basic purpose is to provide a certain level of security.


Generally, the outlook of chattr command is as follows:


The symbols below show the options for the value of [OPERATOR]:

  • = To have chattr set the setting of certain attributes as only attributes, the equal operator is used.
  • + To have chattr perform the addition of certain attributes to already present ones, plus operator is used.
  • For chattr to perform the removal of certain attributes to already present ones, the minus operator is used.

 One or more than one flags i.e [ATTRIBUTES] follow [OPERATOR]. These flags can be either removed from or added to the attributes. The following list gives us examples of a few attributes and operators:

  • A When a file having the A attribute is accessed, the a time record of the file is non-modifiable. The time also called access time is basically the last time of file opening by some particular command.
  • D When a file with the D attribute is changed, the modifications are written synchronously on the disk.
  • s When a command deletes a file having the s attribute, the blocks are zeroed, and then they are written back on the disk.
  • e When a file has an e attribute then it means that the file is using extents for mapping blocks on the disk.

Examples of how chattr is used

Making the files immutable is one of the most frequent uses of chattr, and this is made possible by the addition of an immutable flag to the file. This eliminates the chance for the deletion or renaming of the files.

File attributes can be viewed with the below command:

$ lsattr todo.txt

the + operator  along with the i flag can be added to the already present attributes of a file to make it immutable:

sudo chattr +i todo.txt

The following output represents that only e flag is used:

----i---------e----- todo.txt

Only root has the ability to change the immutable flag thus sudo is used. For the confirmation that attribute’s addition has been successful:

lsattr todo.txt

----i---------e----- todo.txt

For the reversal of the changes and removal of the flags, the minus operator can be used

sudo chattr +i todo.txt

Chattr makes it possible for the addition and removal of various attributes at one time:

sudo chattr +i todo.txt

The above code makes it possible to both command the kernel to avoid tracking of the last time and making the file immutable.

The equal operator can be used to set a particular attribute as the only attribute e.g. :

sudo chattr "=s" todo.txt

In the above example, the equal operator has been used to set the “s” attribute.

What do you use the Chattr command for in your environment? Let us know in the comments section below!


No Comments

    Leave a Reply

    Some notes on commenting on LowEndBox:

    • Do not use LowEndBox for support issues. Go to your hosting provider and issue a ticket there. Coming here saying "my VPS is down, what do I do?!" will only have your comments removed.
    • Akismet is used for spam detection. Some comments may be held temporarily for manual approval.
    • Use <pre>...</pre> to quote the output from your terminal/console, or consider using a pastebin service.

    Your email address will not be published. Required fields are marked *