Using Linux lsattr to View Extended Attributes

The lsattr (list attribute) command in Linux is a specialized utility designed to display extended file attributes on supported filesystems, such as ext2, ext3, ext4, and XFS. While standard POSIX permissions viewed via ls -l manage standard read, write, and execute permissions for users and groups, extended attributes control lower-level filesystem behaviors, including file immutability, append-only states, and data journaling. This guide covers how Linux utilizes lsattr to inspect these advanced flags, how to interpret its output, and the essential command-line options system administrators use to audit file integrity.

Understanding Extended Attributes in Linux

In Linux, files have standard metadata stored in inodes, such as ownership, timestamps, and read/write permissions. Extended attributes provide an additional layer of control, modifying how the Linux kernel interacts with the file directly at the filesystem driver level.

These attributes are set using the chattr command and inspected using lsattr. Even if a user—including the root superuser—has full write permissions on a file, an extended attribute like immutability (i) prevents modification, renaming, linking, or deletion until the attribute is removed.

Basic Syntax and Output Interpretation

The basic syntax for the command is:

lsattr [options] [files...]

When run without flags on a standard file, lsattr displays a series of dashes and letters representing the active attributes, followed by the file path:

$ lsattr /etc/resolv.conf
----i---------e------- /etc/resolv.conf

In this output, each character position corresponds to a specific attribute flag. The presence of a dash (-) indicates that an attribute is unset, while a letter signifies an active attribute.

Common Attribute Flags

The most critical extended attribute flags displayed by lsattr include:

Essential lsattr Options

Linux provides several command-line flags to modify how lsattr traverses and lists directory contents:

Listing Directory Attributes

By default, running lsattr on a directory lists the attributes of the files inside it. To inspect the attributes of the directory itself, use the -d option:

lsattr -d /var/log

Recursive Listing

To view extended attributes across an entire directory tree, the -R flag recursively checks all subdirectories and their contents:

lsattr -R /etc

Including Hidden Files

Like the standard ls utility, lsattr ignores hidden files (those starting with a dot) by default. Use the -a flag to display all files:

lsattr -a ~

Displaying Long Format

To view extended attributes described in complete words rather than single-letter indicators, use the -l option:

lsattr -l filename.txt

System Administration and Security Use Cases

The lsattr command is vital for security auditing and operational integrity. System administrators regularly use lsattr to: