DEV Community

Cover image for πŸ” Mastering chattr and lsattr in Linux – File Attribute Control Like a Pro
SAHIL
SAHIL

Posted on

πŸ” Mastering chattr and lsattr in Linux – File Attribute Control Like a Pro

When we think about file permissions in Linux, chmod, chown, and umask often come to mind.
But did you know there’s another layer of security and control?
That’s where file attributes come into play β€” managed with the chattr and lsattr commands.

πŸ“Œ What Are File Attributes?
File attributes define extra behaviours for files and directories, beyond standard read/write/execute permissions.

They can:

  • Make files immutable (cannot be modified or deleted).
  • Prevent accidental overwriting.
  • Ensure that changes are appended only.
  • Secure files from being renamed.

These attributes are supported on ext2, ext3, ext4 filesystems and some others.

πŸ›  chattr – Change File Attributes
The chattr command is used to set, add, or remove attributes on a file or directory.

chattr [operators][attributes] file_name
Enter fullscreen mode Exit fullscreen mode

Common Attributes:

Operator Meaning
+ Add the attribute
- Remove the attribute
= Set the attribute (replace all existing attributes)
Attribute Meaning
i Immutable – cannot modify, delete, or rename the file.
a Append-only – data can only be added, not removed.
A No access time update – prevents atime updates.
S Synchronous updates – changes are written to disk immediately.
d No dump – file won’t be backed up by dump command.
e Extents format – default for ext4 (don’t change this casually).
u Undeletable – allows recovery after deletion.
c Compress file on disk (needs filesystem support).
j Data journaling – data is written to journal before the file.

Examples
1️⃣ Make a file immutable:

sudo chattr +i important.txt
Enter fullscreen mode Exit fullscreen mode

Now:

You cannot modify, rename, delete, or create hard links to this file.

Even root must remove the attribute before editing.

sudo chattr -i important.txt

2️⃣ Append-only log file

sudo chattr +a logfile.log
Enter fullscreen mode Exit fullscreen mode

Now:
Data can only be added.

Useful for system logs, preventing tampering.

To remove:

sudo chattr -a logfile.log
Enter fullscreen mode Exit fullscreen mode

3️⃣ Replace all attributes

sudo chattr =a test.txt
Enter fullscreen mode Exit fullscreen mode

This removes all existing attributes and sets only append-only.

πŸ” lsattr – List File Attributes
The lsattr command displays current attributes of files and directories.

Syntax:

lsattr [options] [files...]
Enter fullscreen mode Exit fullscreen mode

Common Options:

Option Meaning
-a Show all files including hidden files.
-d List attributes of directories themselves, not their contents.
-R Recursively list attributes in directories.

Examples
1️⃣ Show attributes of a single file

lsattr file.txt
Enter fullscreen mode Exit fullscreen mode

Output:

----i--------e--- file.txt
Enter fullscreen mode Exit fullscreen mode
  • i β†’ Immutable
  • e β†’ Extents format (default for ext4)

2️⃣ Show all files including hidden ones

lsattr -a

Enter fullscreen mode Exit fullscreen mode

3️⃣ Show directory attributes only

lsattr -d mydir

Enter fullscreen mode Exit fullscreen mode

4️⃣ Recursively list attributes

lsattr -R /var/log
Enter fullscreen mode Exit fullscreen mode

⚠️ Common Pitfalls & Tips

Root privilege required for most attribute changes:

sudo chattr +i file
Enter fullscreen mode Exit fullscreen mode
  • Not all filesystems support attributes β€” mainly ext2/3/4.
  • Immutable files cannot even be deleted by root without removing i first.
  • Be careful with = operator β€” it overwrites all attributes.

🧠 Real-Life Use Cases

  • Prevent accidental deletion of config files:
sudo chattr +i /etc/fstab
Enter fullscreen mode Exit fullscreen mode
  • Secure logs from tampering:
sudo chattr +a /var/log/auth.log
Enter fullscreen mode Exit fullscreen mode

Freeze important scripts during deployment:

sudo chattr +i deploy.sh
Enter fullscreen mode Exit fullscreen mode

πŸ“Ž References

https://man7.org/linux/man-pages/man1/chattr.1.html

πŸ’¬ Have you used chattr and lsattr to protect your files? Share your experiences in the comments!

Top comments (0)