Sep 21, 2023 3 min read

How to Use Chattr Command in Linux

Use Chattr command in Linux with our step-by-step tutorial. It is a file system command that changes the properties of a file in a directory.

Use Chattr Command in Linux
Table of Contents

Introduction

Before we begin talking about how to use chattr command, let's briefly understand - What is chattr?

In Linux chattr is a file system command that changes the properties of a file.

File attributes are meta-data properties that describe the behavior of a file under Linux. An attribute can, for example, indicate whether a file is compressed or if it can be deleted.

Some properties, such as immutability can be set or cleared, while others, such as encryption, are read-only. The file system being utilized determines whether certain properties are supported.

In this tutorial, you will use chattr command in Linux. We will also address a few FAQs on how to use chattr command in Linux.

chattr Syntax

The chattr command is written in the following format:

chattr [OPTIONS] [OPERATOR][ATTRIBUTES] FILE...

The [OPERATOR] part's value can be any of the following symbols:

  • + - The plus operator instructs the chattr to add new attributes to those already present.
  • - - The minus operator instructs chattr to remove given attributes from existing attributes.
  • = - The equal operator instructs the chattr to make certain attributes the only ones available.

One or more [ATTRIBUTES] flags that you want to add or remove from the file attributes are followed by the operator. Here's a rundown of some of the most popular qualities and their accompanying flags:

  • a - The file can only be opened in append mode for writing when this property is specified.
  • A - The atime record of a file with this attribute set is not updated when it is opened. The last time the file was accessed/opened by command or application is atime (access time).
  • e - This attribute indicates that the file's blocks on the disc are mapped using extents. With chattr, you can't change the e attribute.
  • i - This attribute denotes that the file is immutable, meaning it cannot be deleted or renamed.

In your terminal, type man chattr for a complete list of all file properties and flags. When using commands like cp or rsync to copy a file, attributes are not maintained by default.

chattr Example

Setting the immutable flag to a file or directory to prohibit users from removing or renaming the file is a typical usage of chattr.

With the lsattr command, you can see the file attributes:

lsattr todo.txt

Only the e flag is set, as shown in the output below:

Output

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

Add the i flag to the existing attributes with the + operator to make the file immutable:

sudo chattr +i todo.txt

We're using sudo since the immutable flag can only be changed by the root.

Verify that the attribute has been added:

lsattr todo.txt
Output

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

Use the - operator to undo the changes and remove the immutable flag:

sudo chattr -i todo.txt

You can add or remove several attributes at the same time using chattr. You would use the following command to make the file immutable and tell the kernel not to keep track of the last time it was accessed:

sudo chattr +iA todo.txt

The = operator is the final operator available to you. To set the e attribute the only attribute, use the following command:

sudo chattr "=e" todo.txt

To avoid shell interpretation of the + character, the operator, and flag are surrounded in quotes.

FAQs to Use Chattr Command in Linux

Can I recursively apply attributes to files and subdirectories using chattr?

Yes, you can recursively apply attributes by using the -R option. For example: sudo chattr -R +i directoryname

How can I make a directory read-only using chattr? 

Use the command: sudo chattr +a directoryname to make a directory read-only.

How can I list the attributes of a file or directory using chattr? 

Run: lsattr filename to list the attributes of a file or directory.

Can I make a file undeletable using chattr? 

Yes, you can make a file undeletable by running: sudo chattr +u filename

How can I prevent a file from being modified or renamed using chattr? 

Use the command: sudo chattr +u +i filename to prevent modifications and renaming.

How can I remove all attributes from a file or directory with chattr? 

Execute: sudo chattr -iaAcDsdR filename to remove all attributes from a file or directory.

Conclusion

We hope this detailed guide helped you understand how to use the chattr command. It is a command-line utility for modifying the attributes of files on a Linux file system.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.