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.confIn 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:
- i (Immutable): The file cannot be modified, deleted, renamed, or linked to. No data can be written to it, and the file cannot be truncated.
- a (Append-only): The file can only be opened in append mode for writing. It cannot be overwritten, truncated, or deleted. This is commonly applied to critical log files.
- e (Extent format): Indicates that the file uses extents for mapping blocks on the disk (standard for modern ext4 filesystems). This is set automatically by the kernel.
- d (No dump): The file is ignored by the
dumpbackup utility. - s (Secure deletion): When the file is deleted, its blocks are zeroed out and written back to the disk, preventing data recovery.
- A (No atime updates): The file's access time
(
atime) is not updated when accessed, reducing disk I/O operations. - c (Compressed): The file is automatically compressed on the disk by the kernel.
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/logRecursive Listing
To view extended attributes across an entire directory tree, the
-R flag recursively checks all subdirectories and their
contents:
lsattr -R /etcIncluding 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.txtSystem Administration and Security Use Cases
The lsattr command is vital for security auditing and
operational integrity. System administrators regularly use
lsattr to:
- Detect Unauthorized Changes: Malicious software or
intruders sometimes apply the immutable flag (
i) to malware files or modified system binaries to prevent administrators from editing or removing them. - Audit Critical Configurations: System files like
/etc/passwd,/etc/shadow, or/etc/resolv.confcan be audited withlsattrto ensure unauthorized modifications are blocked. - Verify Logging Integrity: Ensuring system log files
retain the append-only (
a) flag ensures that existing log history cannot be wiped or tampered with by unauthorized processes.