About chattr & lsattr
The chattr (change attribute) and lsattr (list attributes) commands are complementary tools for managing extended file attributes on Linux filesystems (ext2, ext3, ext4, and others). These attributes provide an additional layer of protection beyond standard Unix permissions.
Why Use File Attributes?
- chattr: Sets or modifies file attributes (requires root for most attributes)
- lsattr: Displays current file attributes (can be run by any user)
- Immutable Flag (i): Makes files unchangeable, even by root - critical for security
- Append-Only (a): Allows only appending to files - perfect for log files
- Works at Filesystem Level: Protection survives chmod, chown, and even root access
Common Use Cases:
- Protecting critical configuration files from accidental modification
- Securing system binaries against tampering
- Preventing deletion of important files during maintenance
- Creating append-only log files that can't be truncated
- Hardening servers against rootkit installation
- Compliance requirements for immutable audit logs
| Attribute | Symbol | Description |
|---|---|---|
| immutable SECURITY | i |
File cannot be modified, deleted, renamed, or linked. Even root cannot change it without removing attribute first. |
| append-only SECURITY | a |
File can only be opened in append mode for writing. Cannot be deleted, truncated, or modified. |
| no-dump | d |
File will not be backed up by the dump program. |
| synchronized | S |
Changes are written synchronously to disk (like mount with sync option). |
| no-atime | A |
Access time is not updated when file is accessed. |
| compressed | c |
File is automatically compressed on disk (filesystem dependent). |
| undeletable | u |
When deleted, contents are saved for later undeletion (not widely implemented). |
| secure-deletion | s |
File is securely deleted (overwritten with zeros) when removed. |
| journaled | j |
Data is written to journal before being written to file (ext3/4 only). |
| Operator | Description |
|---|---|
+ |
Add specified attributes |
- |
Remove specified attributes |
= |
Set attributes to exactly this value (clear others) |
-R |
Recursive - apply to directories and their contents |
-V |
Verbose - display attribute changes |
-v version |
Set file version/generation number |
Example 1Make File Immutable (Cannot Be Changed)
Protect a critical configuration file from any modification:
Explanation:
chattr +i: Adds immutable attributelsattr: Shows 'i' flag is set- File cannot be modified, even with sudo/root
- File cannot be deleted, renamed, or linked
- Directory entry cannot be modified
- Must remove 'i' attribute before making any changes
Example 2Remove Immutable Flag to Allow Changes
Temporarily remove protection to update the file:
Explanation:
chattr -i: Removes immutable attribute- File becomes modifiable again
- Make your changes
chattr +i: Restore protection- Best practice: Remove protection only when needed
- Document when and why protection was removed
sudo chattr -i file; sudo vi file; sudo chattr +i file. This ensures protection is restored even if you forget.
Example 3Append-Only Flag for Log Files
Create log file that can only be appended to, never truncated:
Explanation:
chattr +a: Sets append-only attribute- Appending works:
>>operator succeeds - Overwriting fails:
>operator denied - Deletion fails: Cannot remove file
- Perfect for audit logs and compliance
- Prevents log tampering by attackers
Example 4List All Attributes in Directory
View attributes for multiple files at once:
Explanation:
lsattrwithout args shows attributes for specified files- Shows all flags: 'i' for immutable, 'a' for append-only
-d: Show directory attributes, not contents- The 'e' flag is extent format (ext4 default, not settable)
- Dashes indicate attributes that are not set
- Useful for auditing protected files
lsattr -R /etc /usr/bin /usr/sbin | grep "i\|a" > /var/log/protected-files.log to track which files have special attributes.
Example 5Recursive Protection of Directory
Protect entire directory tree from modification:
Explanation:
-R: Recursive flag applies to all files- Both files and directory itself get immutable flag
lsattr -R: Recursively list attributes- Even
rm -rfcannot delete protected files - Must remove attributes from all files individually
- Or use:
sudo chattr -R -i /etc/critical-configs/
Example 6Protect System Binaries from Tampering
Harden critical system executables against rootkit installation:
Explanation:
- Protects critical binaries from replacement
- Common rootkit technique: replace ls, ps, netstat
- Immutable flag prevents binary substitution
- Even root cannot overwrite without removing attribute
- Critical for intrusion detection and prevention
- Should be part of system hardening checklist
Example 7Combine Multiple Attributes
Set multiple attributes simultaneously:
Explanation:
+ai: Sets both append-only and immutable- With 'i': File cannot be deleted or renamed
- With 'a': File can only be appended to
- Combined: Maximum protection for log files
- Both attributes shown in lsattr output
- Must remove both to modify:
chattr -ai
+ai together. This ensures logs cannot be deleted (i) or have entries removed by truncation (a), providing complete tamper protection.
Example 8No-Dump Attribute for Temporary Files
Exclude files from backup operations:
Explanation:
+d: Sets no-dump attribute- File excluded from
dumpbackups - Useful for cache files and temporary data
- Reduces backup size and time
- File still accessible and writable normally
- Does not affect tar, rsync, or other backup tools
dump utility, which is less commonly used today. Modern backup tools (rsync, tar) typically ignore this attribute. Use exclude lists in those tools instead.
Example 9Check Attributes Before and After Operations
Monitor attribute changes during system maintenance:
Explanation:
- Capture attributes before maintenance window
- Perform system updates or changes
- Compare attributes afterward
- Diff shows immutable flag was removed from hosts
- Indicates file was modified during upgrade
- May need to restore protection afterward
sudo lsattr -R /etc > /var/log/attrs-$(date +%Y%m%d-%H%M).txt. Track attribute changes as part of change management process.
Example 10Automated Protection Script
Create a script to protect critical system files:
Explanation:
- Script automates protection of critical files
- Array holds list of files to protect
- Loop applies immutable attribute to each
- Verification shows current protection status
- Can be run after system installation
- Create complementary unprotect script for maintenance
protect-critical-files.sh and unprotect-critical-files.sh. Run unprotect before maintenance, protect afterward. Log all executions for audit trail.
System updates fail when trying to modify protected config files.
Solution: Before system updates: sudo chattr -R -i /etc. After updates: sudo chattr -R +i /etc/critical-files/. Better: use apt hooks or package manager triggers to automate this.
rm -rf directory/ fails even with root when files inside are immutable.
Solution: Must remove attributes first: sudo chattr -R -i directory/, then delete. Or use script: find directory/ -type f -exec chattr -i {} \; && rm -rf directory/
Backup software fails when trying to update files with append-only attribute.
Solution: Either configure backup software to handle attribute errors gracefully, or temporarily remove append-only flag during backup window: chattr -a logfile; backup; chattr +a logfile
NFS, FAT32, and some network filesystems don't support extended attributes.
Solution: Check filesystem support: mount | grep $(df /path | tail -1 | awk '{print $1}'). Attributes only work on ext2/3/4, XFS, Btrfs, ReiserFS. Not on NFS, CIFS, FAT.
Security modules may conflict with file attributes or add confusion.
Solution: File attributes and MAC (SELinux/AppArmor) work on different layers and generally don't conflict. Use both for defense in depth. Check logs when troubleshooting: ausearch -m AVC for SELinux.
Maintain a list of files with special attributes:
Update documentation when adding/removing protection. Helps troubleshooting and knowledge transfer.
Meet regulatory requirements for immutable logs:
Document this in compliance reports. Demonstrate technical controls for log integrity.
Create audit rules to alert when attributes are modified:
Alerts you when someone removes protection from files - potential security incident.
Prevent accidental corruption of package databases:
Remove protection before package operations, restore after. Prevents database corruption.
Before depending on immutable protection, test recovery:
Ensure you can recover if attributes prevent legitimate system maintenance.
For maximum security, combine with read-only root filesystem:
Enterprise security: read-only root + immutable binaries + SELinux = defense in depth.
| Task | Command |
|---|---|
| Make file immutable | sudo chattr +i file |
| Remove immutable flag | sudo chattr -i file |
| Make file append-only | sudo chattr +a file |
| List file attributes | lsattr file |
| Recursive protection | sudo chattr -R +i directory/ |
| List directory attributes | lsattr -d directory/ |
| Multiple attributes | sudo chattr +ai file |
| Remove all protection | sudo chattr -R -ai directory/ |
| No-dump flag | chattr +d file |
| List recursively | lsattr -R directory/ |
Files to Consider Protecting
| File/Directory | Attribute | Reason |
|---|---|---|
| /etc/passwd, /etc/shadow, /etc/group | +i |
Prevent unauthorized user account modifications |
| /etc/sudoers, /etc/sudoers.d/* | +i |
Protect privilege escalation configuration |
| /etc/ssh/sshd_config | +i |
Prevent SSH configuration tampering |
| /boot/grub/grub.cfg | +i |
Protect bootloader configuration |
| /bin/*, /sbin/*, /usr/bin/*, /usr/sbin/* | +i |
Prevent rootkit binary replacement |
| /var/log/audit/audit.log | +a |
Ensure audit log integrity |
| /var/log/secure, /var/log/auth.log | +a |
Protect authentication logs |
| /etc/fstab | +i |
Prevent mount table manipulation |
| /etc/hosts, /etc/resolv.conf | +i |
Prevent DNS hijacking |