chattr

file attributeslinux
The chattr command is one of the most frequently used commands in Linux/Unix-like operating systems. chattr Change file attributes on a Linux file system

Quick Reference

Command Name:

chattr

Category:

file attributes

Platform:

linux

Basic Usage:

chattr [options] [arguments]

Common Use Cases

  • 1

    File attribute management

    Set special attributes on files and directories

  • 2

    Security hardening

    Protect files from accidental deletion or modification

  • 3

    System administration

    Manage file attributes for system security

  • 4

    Data protection

    Prevent unauthorized changes to critical files

Syntax

chattr [OPTION]... [+-=][ATTRIBUTES]... FILE...

Options

Option Description
-R, --recursive Recursively change attributes of directories and their contents
-V, --version Display version information and exit
-f, --silent, --quiet Suppress most error messages
-v, --verbose Output a diagnostic for every file processed
--help Display help and exit
--inode X Only affect files with inode number X

Available Attributes:

Attribute Description
a append only - file can only be opened in append mode for writing
A no atime updates - do not update atime (access time) when file is accessed
c compressed - file is compressed on the fly by the kernel
C no copy on write - file is not subject to copy-on-write updates (for filesystems like Btrfs)
d no dump - file is not candidate for backup with dump utility
D synchronous directory updates - directory updates are done synchronously
e extent format - file uses extents for mapping blocks on disk
i immutable - file cannot be modified, deleted, renamed, or linked
j data journaling - file data is journaled
P project hierarchy - inherit project ID from parent directory
s secure deletion - file is securely deleted (zeros written over data)
S synchronous updates - file changes are written synchronously to disk
t no tail-merging - file with this attribute will not have tail of partial blocks merged
T top of directory hierarchy - directory is the top of the directory hierarchy
u undeletable - file contents are saved when deleted (allows recovery)

Examples

How to Use These Examples

The examples below show common ways to use the chattr command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Make a file immutable (can't be modified, deleted, or renamed)
chattr +i important_file.txt
Remove the immutable attribute
chattr -i important_file.txt
Make a file append-only (can only be opened in append mode)
chattr +a log_file.txt
Set a file to be compressed by the kernel on write and decompressed on read
chattr +c large_file.dat
Prevent a file from being backed up by dump utility
chattr +d backup_excluded.txt

Advanced Examples:

Set multiple attributes at once (immutable and append-only)
chattr +ia critical_log.txt
Remove multiple attributes at once
chattr -ia critical_log.txt
Apply recursively to directories and files
chattr -R +i /path/to/important/directory
Set exact attributes (equals replaces all current attributes)
chattr =i confidential_file.txt
Apply to a specific file version using inode number
chattr +i --inode 12345 /path/to/file
Apply to all files in a directory but not the directory itself
find /path/to/directory -type f -exec chattr +i {} \;
Display attributes of all files in a directory
lsattr /path/to/directory/

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Features:

The chattr command allows system administrators to set special attributes on files:

  • File protection against accidental deletion or modification
  • Control over how the kernel handles file data
  • Ability to set attributes that affect backup and journaling behavior
  • File system-level security enhancements
  • Performance optimization options

Filesystem Support:

Not all attributes are supported on all filesystems:

  • Most attributes work on ext2, ext3, and ext4 filesystems
  • XFS supports a subset of attributes (a, A, d, i, S)
  • Btrfs supports: a, c, C, d, D, i, P, S
  • JFS supports limited attributes (a, d, i)
  • ReiserFS has limited support for some attributes

Common Use Cases:

  • Immutable flag (+i): Protect critical system files from accidental modification
  • Append-only (+a): Ideal for log files where data can be added but not modified or deleted
  • No atime updates (+A): Improve performance on frequently accessed files
  • Synchronous updates (+S): Ensure important data is immediately written to disk
  • Compression (+c): Save disk space on large, infrequently accessed files

Important Notes:

  • Root privileges are required to change most attributes
  • The immutable attribute (i) cannot be removed if the secure-bit is set in kernel
  • To view file attributes, use the companion command 'lsattr'
  • Changes to attributes are immediate and don't require system restart
  • Attributes are stored in the file's inode, not in the file content
  • Setting attributes on symbolic links affects the link itself, not the target file

Security Implications:

  • Even root users cannot modify immutable files without first removing the i attribute
  • The immutable flag provides protection against malware and intrusions
  • System configuration files can be protected from unauthorized changes
  • Append-only logs can preserve audit trails even if the system is compromised
  • These attributes are not substitutes for proper access controls and permissions

Performance Considerations:

  • The 'A' attribute (no atime updates) can significantly improve performance for frequently accessed files
  • Synchronous attributes (S, D) ensure data integrity but may decrease performance
  • The compressed attribute (c) trades CPU usage for reduced disk space
  • For large directories, recursive operations with -R may take considerable time

Common Use Cases

File attribute management

Set special attributes on files and directories

Security hardening

Protect files from accidental deletion or modification

System administration

Manage file attributes for system security

Data protection

Prevent unauthorized changes to critical files

File system optimization

Optimize file system behavior with attributes

Related Commands

These commands are frequently used alongside chattr or serve similar purposes:

Use Cases

1

File attribute management

Set special attributes on files and directories

2

Security hardening

Protect files from accidental deletion or modification

3

System administration

Manage file attributes for system security

4

Data protection

Prevent unauthorized changes to critical files

5

File system optimization

Optimize file system behavior with attributes

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the chattr command works in different scenarios.

$ chattr
View All Commands