[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedAugust 14, 2023

How to use chattr command in Linux.

How To Use Chattr Command In Linux.

This article explains how to use the chattr command to change file attributes on Linux file systems.

In Linux, file attributes are meta-data properties that describe the file’s behavior. For example, an attribute can indicate whether a file is compressed or specify if the file can be deleted.

Some attributes like immutability can be set or cleared, while others like encryption are read-only and can only be viewed. The support for certain attributes depends on the filesystem being used.

 

chattr Syntax

The chattr command takes the following general form:

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

The value of the [OPERATOR] part can be one of the following symbols:

  • + – The plus operator tells the chattr to add specified attributes to the existing ones.

  • – The minus operator tells the chattr to remove specified attributes from the existing ones.

  • = – The equal operator tells the chattr to set specified attributes as the only attributes.

The operator is followed by one or more [ATTRIBUTES] flags that you want to add or remove from the file attributes. Below is a list of a few common attributes and associated flags:

  • a – When this attribute is set, the file can only be opened in append mode for writing.

  • A – When a file with this attribute set is open, its atime record is not changed. atime (access time) is the last time the file was accessed/opened by some command or application.

  • e – This attribute denotes that the file is using extents for mapping the blocks on disk. The e attribute cannot be modified with chattr.

  • i – This attribute indicates that the file is immutable, which means that the file cannot be deleted or renamed.

For a complete list of all file attributes and flags, type man chattr in your terminal.

By default, file attributes are not preserved when ing a file with commands like cp or rsync .

 

chattr Example

One of the common uses of chattr is to set the immutable flag to a file or directory to prevent users from deleting or renaming the file.

You can view the file attributes with the lsattr command:

lsattr file.txt

The output below shows that only the e flag is set:

-------------e-- file.txt

To make the file immutable, add the i flag with the + operator to the existing attributes:

sudo chattr +i file.txt

We’re using sudo because only root can change the immutable flag.

Confirm that the attribute is added:

lsattr file.txt
----i---------e----- file.txt

To revert the changes and remove the immutable flag, use the - operator:

sudo chattr +i file.txt

With chattr, you can add or remove multiple attributes at once. For example, to make the file immutable and tell the kernel not to track the last accessed time, you would use:

sudo chattr +iA file.txt

The last operator that you can use is the = operator. For example, to set the e attribute as the only attribute, you would run:

sudo chattr "=e" file.txt

Notice that the operator and flag are enclosed in quotes, to avoid shell interpretation of the + character.

 

Conclusion

chattr is a command-line tool for changing the file attributes on a Linux file system.

Keep reading
How to install Debian 12.
July 20, 2023

How to install Debian 12.

How to install Debian 12. Connect to java/html5 console, (here you can find an article that will show you how) How to access java/html5 console : Mount ISO: Virtual media > Connect Virtual Image > Map CD/DVD Click on: Connect Virtual Media. Click on: Map CD/DVD. Set users and password: Chose…

Read article
How to access your KVM Java Console from Client Area.
July 20, 2023

How to access your KVM Java Console from Client Area.

How to access your KVM Java Console from Client Area. After you place the order access Client Area from https://nexonhost.com/: Access services: Click on your product: You can access you KVm Java Console from the below buttons : How to Start, Stop, Reset, Kvm java console: Start the KVM Java…

Read article
How To Create A Password Generator In Bash.
July 19, 2023

How To Create A Password Generator In Bash.

How To Create A Password Generator In Bash. In this article, we will explore how to create a password generator using Bash scripting. Bash is a popular scripting language available in most Unix-like operating systems. Generate a random string of characters: To create a secure password, we need to…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, cloud hosting, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00