chattr changes Linux filesystem attribute flags that sit outside ordinary chmod permissions and ACLs. It is the companion command to lsattr: use lsattr to inspect attributes, then use chattr only when you understand which flag needs to change.
This lesson follows Linux Command #54 – lsattr, Linux Command #52 – getfacl, and Linux Command #50 – chmod. Together, these tools help separate filesystem attributes from ACLs and ordinary Unix mode bits.
What You’ll Learn
- Explain what
chattrchanges. - Use
+iand-ito set and clear the immutable attribute. - Use
+aand-ato control append-only behavior. - Understand the difference between
+,-, and=. - Verify every change with
lsattr. - Recognize filesystem and privilege limitations.
- Troubleshoot files that cannot be modified, renamed, or deleted.
Server Gyan demonstrates changing Linux file attributes with chattr, including common attribute flags and practical command syntax.
What chattr Changes
The current chattr(1) manual defines chattr as the command for changing file attributes on a Linux filesystem. Its symbolic mode uses three operators: + adds selected attributes, - removes selected attributes, and = replaces the current set with exactly the attributes you specify.
The general form is chattr MODE FILE.... In day-to-day administration, the most important modes are usually +i, -i, +a, and -a.
Important: attribute support varies by filesystem. The e2fsprogs manual explicitly warns that not every flag is supported or used by every filesystem. When behavior is unexpected, identify the filesystem first with a command such as findmnt -T FILE and then consult that filesystem’s documentation.
Make a File Immutable
Use sudo chattr +i FILE to add the immutable attribute when the filesystem supports it. For example:
sudo chattr +i important.conf
lsattr important.conf
With the i flag set, the file cannot normally be modified, renamed, deleted, or linked. The current manual states that only the superuser or a process with the CAP_LINUX_IMMUTABLE capability can set or clear this attribute.
A LinuxQuestions discussion shows a practical reason administrators use chattr +i: normal read-only permissions do not necessarily stop a file from being unlinked when the directory permissions allow deletion.
Remove the Immutable Attribute
When a legitimate change is required, first confirm why the immutable flag exists. Then remove it with:
sudo chattr -i important.conf
lsattr important.conf
After verification, make the intended change. If the file is supposed to remain protected, restore the immutable attribute with sudo chattr +i important.conf and verify again with lsattr.
Use Append-Only for Logs and Records
The a attribute is append only. A file carrying this flag can be opened for writing only in append mode. Existing contents cannot normally be overwritten or truncated while the flag remains set.
sudo chattr +a audit.log
lsattr audit.log
printf '%s\n' 'new record' | sudo tee -a audit.log
sudo chattr -a audit.log
This can be useful for specific logging or record-keeping workflows, but it is not a substitute for a complete logging, backup, or security architecture.
A LinuxQuestions thread highlights an important distinction: append-only behavior is a filesystem attribute, not one of the normal read/write/execute permission bits.
Understand +, -, and =
chattr +i FILE— add immutable while preserving other changeable attributes.chattr -i FILE— remove immutable while preserving other attributes.chattr +a FILE— add append-only.chattr -a FILE— remove append-only.chattr =a FILE— replace the changeable attribute set with append-only. Use=carefully because it is not merely additive.
For routine administration, + and - are usually safer because they change only the named attribute. Do not use = unless you deliberately intend to replace the existing changeable attribute set.
Directories Behave Differently Than Files
Making a directory immutable does not automatically apply the i flag to every child file. Instead, the directory itself cannot be modified, which prevents operations such as creating, removing, or renaming directory entries. Existing child files may still be editable if their own attributes and permissions allow it.
If you truly intend to change attributes recursively, chattr provides -R. Treat recursive attribute changes as high impact. Inspect the target path first, use a disposable lab when learning, and avoid broad commands against system trees unless you have a documented reason and recovery plan.
Linux eHub demonstrates immutable-file behavior, including setting +i, testing modification and deletion, verifying with lsattr, and clearing the attribute.
Not Every lsattr Flag Can Be Changed
lsattr can display some read-only attributes that chattr cannot change. The current manual identifies encrypted (E), indexed-directory (I), inline-data (N), and verity (V) attributes as read-only from chattr‘s perspective.
Do not assume that every letter shown by lsattr is something you should remove. Some attributes describe filesystem implementation details rather than an administrator-applied protection flag.
Real-World Troubleshooting Workflow
- Reproduce the failure without destructive retries.
- Check ownership and mode bits with
ls -ldorstat. - Check ACLs with
getfacl. - Inspect file attributes with
lsattr FILE. - Inspect the parent directory with
lsattr -d PARENT_DIRECTORY. - Identify the filesystem with
findmnt -T FILE. - If
iorais set, determine whether it is intentional. - Change only the required attribute with
chattr. - Run
lsattragain to verify the exact resulting state. - Document or restore the protection if the application expects it.
Operational caution: the e2fsprogs manual notes that setting a or i does not retroactively block writes through file descriptors that were already open. Attribute changes should therefore be treated as one control within a larger operational and security design, not as a complete isolation mechanism.
Hands-On Exercise
- Create a disposable directory:
mkdir -p ~/chattr-lab. - Create a test file:
printf 'baseline\n' > ~/chattr-lab/test.txt. - Record its baseline attributes with
lsattr ~/chattr-lab/test.txt. - Set immutable:
sudo chattr +i ~/chattr-lab/test.txt. - Verify with
lsattr. - Attempt a normal edit, rename, and deletion. Record the errors instead of forcing the operations.
- Clear immutable with
sudo chattr -i ~/chattr-lab/test.txt. - Set append-only with
sudo chattr +a ~/chattr-lab/test.txt. - Append one line with
tee -a, then verify that truncating the file fails. - Clear append-only with
sudo chattr -a ~/chattr-lab/test.txt. - Remove the disposable lab directory when finished.
Knowledge Check
- What does
chattrchange? - What does
+ido? - What does
-ido? - What is the purpose of
+a? - Why should
lsattrbe run afterchattr? - What is the difference between
+and=? - Why can the same attribute behave differently across filesystems?
- Which capability can permit setting or clearing immutable/append-only attributes?
- Does setting
+ion a directory recursively applyito every child file? - Why should recursive
chattr -Rcommands be used cautiously?
Answers
- Filesystem attribute flags.
- It adds the immutable attribute when supported.
- It removes the immutable attribute.
- It makes supported writes append-only while the attribute is set.
- To verify the actual resulting attribute state.
+adds named attributes;=replaces the changeable set with exactly what is specified.- Not every filesystem implements every attribute or implements it identically.
CAP_LINUX_IMMUTABLE.- No. The directory itself becomes immutable; recursive application requires
-R. - They can affect many files and directories at once and may disrupt services or recovery workflows.
Key Takeaway
chattr changes Linux filesystem attributes; lsattr verifies them. Use immutable and append-only flags deliberately, understand the filesystem underneath them, and change only the attribute required for the task.
Next in this Linux command sequence: Linux Command #56 will continue with another filesystem-inspection tool after this attribute pair.
BitcoinVersus.Tech provides technical education and analysis for informational purposes only. Test administrative commands in a disposable environment before applying them to production systems.

Leave a Reply