Skip to content

chattr Linux Command Guide

The chattr command in Linux allows users to change file attributes such as immutable, append-only, no-dump, and more to enhance security and control access. It can be used to protect important system files and directories from accidental deletion or modification. By setting specific attributes, users can control how files are accessed and modified, providing an extra layer of security for critical data.

Terminal window
chattr [options] [parameters] file/path
OptionDescription
+aAppend only
+cCompress file in the file system
+dDo not back up in dump
+iImmutable
+eEncryption
-eDisable encryption
+jData journaling
-jDisable data journaling
+SSecure deletion
+sSecure deletion and copying on rolls
+TNo tail-merging
+uAdd a file to the dump
-uRemove a file from the dump
ParameterDescription
file/pathSpecify the file or path
Terminal window
sudo chattr +i file.txt

This command sets the immutable attribute on the file.txt file, preventing any modifications to its contents.

Terminal window
sudo chattr +u file.txt

This command sets the undeletable attribute on the file.txt file, allowing it to be moved or renamed but not deleted.

Terminal window
lsattr file.txt

This command displays the attributes of the file.txt file, showing which special attributes are set.

Terminal window
sudo chattr +a file.txt

This command sets the append-only attribute on the file.txt file, allowing new data to be appended but not overwritten.

Remove the Immutable Attribute from a File

Section titled “Remove the Immutable Attribute from a File”
Terminal window
sudo chattr -i file.txt

This command removes the immutable attribute from the file.txt file, allowing modifications to its contents.

Terminal window
sudo chattr +a file.txt

This command sets the immutable attribute on the file.txt file, preventing it from being deleted.

Terminal window
sudo chattr +aiu file.txt

This command sets multiple attributes (immutable, append-only, undeletable) on the file.txt file, providing comprehensive protection.

To use the chattr command in Linux, execute the following command:

Terminal window
chattr +i filename.txt

What is the purpose of the chattr command in Linux?

Section titled “What is the purpose of the chattr command in Linux?”

The chattr command is used to change the file attributes of a file in Linux, such as making a file immutable or appending data to it.

How can I make a file immutable using chattr?

Section titled “How can I make a file immutable using chattr?”

You can make a file immutable using chattr by executing the following command:

Terminal window
chattr +i filename.txt

How do I check the attributes of a file using chattr?

Section titled “How do I check the attributes of a file using chattr?”

To check the attributes of a file using chattr, run the following command:

Terminal window
lsattr filename.txt

Can I recursively change file attributes with chattr in Linux?

Section titled “Can I recursively change file attributes with chattr in Linux?”

Yes, you can recursively change file attributes with chattr by using the -R option. For example:

Terminal window
chattr -R +i directory/

How can I remove a specific attribute from a file using chattr?

Section titled “How can I remove a specific attribute from a file using chattr?”

To remove a specific attribute from a file using chattr, run the following command:

Terminal window
chattr -<attribute> filename.txt
  • Making files immutable
  • Preventing accidental deletion of files
  • Preventing files from being renamed
  • Preventing files from being modified
  • Allowing only appending to files
  • Making files undeletable even by root user
  • Protecting critical system files