CHATTR
chattr Immutable Attribute & Advanced File Protections for WordPress VPS
Introduction
chattr is a Linux command used to set file attributes at filesystem level. It is extremely powerful for WordPress security hardening, especially against malware that modifies core files, uploads shells, or injects spam scripts.
In WordPress VPS management, the **immutable flag (+i) is the most valuable. It prevents modification, deletion, renaming even by root unless removed.
This module teaches you how to use chattr safely in production and development environments.
What You Will Learn
- Meaning of
chattrand immutable attributes - Exact syntax and safe execution patterns
- Critical WP use cases (wp-config.php, core files, uploads)
- Differences between dev vs production protection
- Audit attributes (
lsattr) - Troubleshooting and rollback
- Real-world attack prevention scenarios
5W + 1H Framework
| Point | Description |
|---|---|
| What | File attribute command that locks files at filesystem-level |
| Why | Prevent unauthorized edits, malware injection, persistent backdoors |
| When | After site configuration, in hardened production security stage |
| Where | Key WordPress files: wp-config.php, .htaccess, security scripts |
| Who | VPS admin / security engineer |
| How | Set immutable flag chattr +i /path/file and monitor with lsattr |
Prerequisites
| Requirement | Why |
|---|---|
| Ubuntu VPS with SSH | Execute commands |
| Root or sudo | chattr requires elevated privilege |
| WordPress installed | Real WP security context |
| Understanding file permissions | chmod/chown interact with chattr |
Core Syntax Formula
Core Syntax Formula
Basic Pattern
chattr [options] [attribute flags] file_or_directory
Most Common Usage
Add an attribute:
sudo chattr +i /path/to/file
Remove an attribute:
sudo chattr -i /path/to/file
List attributes (using lsattr):
lsattr /path/to/file
Wildcard:
sudo chattr +i /home/*/public_html/wp-config.php
Attribute Operators
| Operator | Meaning |
|---|---|
+ | Add attribute to file |
- | Remove attribute |
= | Set attribute exactly (replace all others) |
Example (force only immutable, remove others):
sudo chattr =i wp-config.php
Most Important Flags for WordPress Security
These attributes are filesystem-level, NOT chmod permission bits.
| Flag | Meaning | Use in WP Security? |
|---|---|---|
i | Immutable: cannot modify/delete/rename | Yes (critical) |
a | Append-only: only append allowed, no delete/edit | Yes (logs, shell logs) |
e | Extents format (ext4 internal default) | System internal |
A | No atime update | Performance files, rarely used |
S | Synchronous updates | Integrity sensitive files |
d | No backup dump | Ignore dump utility |
j | Journal data writes | Journaling FS only |
t | No tail-merging | Old system optimization |
u | Undeletable (recoverable) | Not widely used anymore |
Explanation of Key Flags
Immutable (i)
Prevents changing, deleting, renaming.
sudo chattr +i wp-config.php
To modify later:
sudo chattr -i wp-config.php
Append-Only (a)
Only allow adding to file.
Great for logs to prevent log tampering:
sudo chattr +a /var/log/auth.log
Remove:
sudo chattr -a /var/log/auth.log
All Flags Reference Table
| Flag | Full Name | Description |
|---|---|---|
| a | append only | add only; no edit/delete |
| A | no atime update | do not modify access time |
| c | compress | automatic filesystem compression |
| C | no copy-on-write | avoid COW (btrfs) |
| d | no dump | exclude from backup/dump |
| D | synchronous dir updates | reflect changes immediately |
| e | extents | ext4 flag (default) |
| i | immutable | cannot modify/rename/delete |
| j | data journaling | write in data journal |
| s | secure deletion | overwrite blocks before deletion |
| S | synchronous write | sync write to disk |
| t | no tail merging | disable tail-packing |
| u | undeletable | data recoverable |
Note: Not all flags apply on every filesystem.
In WP/Ubuntu you mainly use:
ia
Command Options (Execution Flags)
| Option | Meaning |
|---|---|
-R | Recursive |
-V | Verbose |
-v | Version |
Example recursive:
sudo chattr -R +i /var/www/html/wp-content/
Verbose:
sudo chattr -V +i wp-config.php
Real-World WordPress Examples
Lock core config:
sudo chattr +i /home/USER/DOMAIN/public_html/wp-config.php
Lock .user.ini (OLS security config):
sudo chattr +i /home/USER/DOMAIN/public_html/.user.ini
Append-only access log:
sudo chattr +a /var/log/auth.log
Safety Rules
- Do not apply immutable to entire WordPress root unless maintenance mode
- Remove immutable before theme/plugin updates
- Never recursive lock
/wp-contentunless incident response - Always document locked files
Quick Knowledge Test
Answer mentally:
- What does
+ido? - How do you unlock an immutable file?
- When should you use
+a? - True or false:
chattrreplaces chmod?
Summary
chattrmodifies filesystem attributes+i= strongest protection for WP critical files+aensures tamper-proof logs- Must remove attributes before updates/migrations
- Use
lsattrto verify protected files
If you want next, choose:
- Real-world WP malware attack demo and defense with
chattr - Automatic bash scripts for update cycle (unlock update lock)
chattrfirewall model for multi-site WP fleet- Incident response SOP using
chattr
Reply with: 1, 2, 3, or 4.
Add Attribute
sudo chattr +i /path/to/file
Remove Attribute
sudo chattr -i /path/to/file
List Attributes
lsattr /path/to/file
WP-Focused Use Cases
Lock wp-config.php (production)
sudo chattr +i /home/USER/DOMAIN/public_html/wp-config.php
Lock .htaccess (Apache) / .user.ini (OLS/PHP)
sudo chattr +i /home/USER/DOMAIN/public_html/.user.ini
sudo chattr +i /home/USER/DOMAIN/public_html/.htaccess
Prevent malware in uploads (optional, risky)
sudo chattr +i /home/USER/DOMAIN/public_html/wp-content/uploads/.htaccess
Lock security scripts
sudo chattr +i /usr/local/bin/wpbackup
Development vs Production Strategy
| Environment | Action |
|---|---|
| Development / Staging | DO NOT use immutable flags |
| Production | Use immutable on core config & sensitive files |
| Plugin/theme update phase | Remove immutable, update, re-lock |
Best Practices
| Rule | Purpose |
|---|---|
| Lock only critical files | Avoid breaking WP updates |
| Document locked paths | Avoid confusion later |
| Automate unlock-update-lock cycle | Efficiency and safety |
| Pair with permissions & owner rules | Multi-layer defense |
Go-Live Checklist
| Check | Expected |
|---|---|
| Confirm file owner & perms first | No misconfiguration |
| Test WP writes | Uploads, logs still work |
| Save script of locked files | For automation |
| Test restore commands | Safety rollback verified |
Troubleshooting Matrix
| Problem | Cause | Fix |
|---|---|---|
| WP updates fail | Immutable blocks writes | sudo chattr -i file |
| Plugin cannot write config | Wrong file locked | Unlock correct file |
| Migration stuck | rsync blocked | Remove immutable then retry |
Quick Lab
Step 1: Check attributes
lsattr wp-config.php
Step 2: Lock file
sudo chattr +i wp-config.php
Step 3: Attempt edit (should fail)
nano wp-config.php
Step 4: Unlock
sudo chattr -i wp-config.php
Cheat Sheet
| Action | Command |
|---|---|
| Add immutable | sudo chattr +i file |
| Remove immutable | sudo chattr -i file |
| List file attributes | lsattr file |
| Lock wp-config | sudo chattr +i wp-config.php |
| Unlock wp-config | sudo chattr -i wp-config.php |
Mini Quiz
- Why
chattr +iis dangerous if misused? - Which file is highest priority to lock in WP?
- What command unlocks immutable file?
- Should you use immutable on uploads folder?
Command Automation Script (safe pattern)
# freeze critical WP files
sudo chattr +i wp-config.php
sudo chattr +i .user.ini
Unlock script:
sudo chattr -i wp-config.php
sudo chattr -i .user.ini
Final Notes
chattr is **not a replacement for proper permissions (chmod, chown) it is an extra layer. Use wisely, document paths, and always unlock before updates.
Is it chattr part of file permission ? Is it part of regular permission or special or any categor Can i apply chattr -i in find command and exec to all
Lets clarify your questions precisely and technically correct, in VPS-for-WordPress context.
Is chattr part of file permission?
No. chattr is NOT a file permission.
Linux has three different layers of file control:
| Layer | Mechanism | Example | Purpose |
|---|---|---|---|
| 1. Standard Permissions | chmod / chown | rwx bits, owner/group | OS-level access control |
| 2. ACL (optional) | setfacl | fine-grained allow/deny | Per-user overrides |
| 3. Extended Attributes | chattr / lsattr | +i, +a flags | Kernel-level protection |
chattr belongs to extended filesystem attributes, NOT chmod permissions.
So your security layers look like:
Filesystem ACL < hardest to bypass
chattr flags < kernel level lock
chmod/chown < OS user/group access
.htaccess/PHP < application level
WordPress < CMS level control
In short:
chmodchanges permissionschattrchanges filesystem attributes
Is chattr regular permission or special category?
It is special, kernel-level attribute control.
Category:
Filesystem extended attribute (Ext4 attribute)
It overrides normal file permissions even root respects immutable unless removed.
Can you apply chattr -i (or +i) using find -exec to all files?
Yes, you technically can.
Example unlock all:
sudo find /var/www/html -type f -exec chattr -i {} ;
Example lock all (not recommended):
sudo find /var/www/html -type f -exec chattr +i {} ;
But this is dangerous for WordPress.
Why its dangerous
Locking the entire WP folder prevents:
- Plugin updates
- Theme updates
- Cache writes
- Logs
- Uploads
- Transients
- Cron writing files
Your site will break.
Proper use pattern
Only lock specific critical files, Example:
sudo chattr +i wp-config.php
sudo chattr +i .user.ini
sudo chattr +i .htaccess (Apache only)
Then create an unlock script before maintenance.
Correct WP-safe update cycle (best practice)
Unlock
sudo chattr -i wp-config.php .user.ini
Do updates
WP updates, plugin updates, theme updates
Lock again
sudo chattr +i wp-config.php .user.ini
You can automate this, I can provide script later.
Advanced: Safe find + chattr example (for logs only)
Append-only to logs:
sudo find /var/log -type f -name "*.log" -exec chattr +a {} ;
Remove:
sudo find /var/log -type f -name "*.log" -exec chattr -a {} ;
Never do blanket +i on WP folders.
Summary
| Question | Answer |
|---|---|
Is chattr file permission? | No |
| Category? | Extended filesystem attribute |
| Regular or special? | Special kernel-enforced attribute |
| Safe to apply to all files? | No — breaks WP |
| Safe to unlock all via find? | Yes |
| Safe to lock specific files? | Yes (wp-config, .user.ini) |
Quiz
- Which layer is stronger: chmod or chattr?
- What happens if you set +i on /wp-content/uploads?
- Best files to protect with chattr for WP?
- Which command removes immutable from all files?
Next options choose one
Reply with:
A. Provide automate script (unlock WP update lock)
B. Provide find + exclude pattern (skip uploads/logs/cache)
C. Visual Linux security layer map (whiteboard style)
D. Incident response plan using chattr after malware detection
Your choice?
What risk if i have finished on phase dev now move to live phase then i use chattr -i on all file
If you finish development and then apply **chattr +i on ALL WordPress files when moving to live/production, you are creating a **full system lockdown and this will cause major operational failures.
Below is a precise risk list.
Critical Risks When Applying chattr +i to All WordPress Files
WordPress Cannot Write Anything
WP needs to write many files dynamically:
| Area | What breaks |
|---|---|
| Plugins update | Fails, site may break |
| Theme update | Fails |
| Core update | Fails |
| Permalink rewrite | Fails |
| Uploads | Cannot upload images/files |
| Cache files | WP/Perfmatters/Caching fails |
| Yoast/SEOPress sitemap cache | Breaks SEO |
| Security plugins logs | Break |
| Auto-update system | Fails |
| File-based sessions | Login issues |
Automatic Operations Stop
| System | Impact |
|---|---|
| Cron jobs | Cannot create files |
| Backup plugins | Cannot write backup files |
| Log rotation | Logs cannot rotate |
| WP Rocket / LSCache / FastCGI cache | Broken caching |
| Composer / npm (if CI/CD) | Blocked writes |
This kills performance, automation, security, and reliability.
Security Tools Break
| Tool | Problem |
|---|---|
| Wordfence | Cannot write malware scan logs |
| Fail2ban WP filters | Limited logs |
| Malware cleanup | Cannot delete malicious files |
| .htaccess / .user.ini hardening rules | Cannot update |
You lose reactive & proactive defense ability.
DNS / Server Config Management Pain
When you need to adjust:
.user.ini.htaccess- TLS renew scripts
- Server-block rewrites (when using symlinked files)
You must manually unlock files first. Painful maintenance.
CI/CD & Deployment Failure
If using:
- Git deploy
- Capistrano
- Rsync deployment
- FTP/SFTP deploy pipeline
Deployment fails.
Bottom Line
Applying chattr +i to **ALL WP files =
You freeze the site like solid concrete.
Good for forensic mode, NOT for production.
Correct Strategy (What Professionals Do)
Immutable only for critical files
| File | Why |
|---|---|
wp-config.php | Database & secrets |
.user.ini | PHP security directives |
.htaccess (Apache) | Rewrite layer |
Optional: index.php | Entry point safety |
Command example:
sudo chattr +i wp-config.php
sudo chattr +i .user.ini
NEVER apply +i to:
/wp-content/uploads//wp-content/cache//wp-content/plugins//wp-content/themes/
Use Append-only (+a) for logs instead
sudo chattr +a /var/log/auth.log
sudo chattr +a /var/www/html/wp-content/debug.log
When chattr +i to all files is acceptable
Only in these cases:
| Situation | Why |
|---|---|
| Incident response malware lock | Stop reinfection immediately |
| Site archival mode | Static site, no changes |
| Legal/compliance evidence freeze | Forensics mode |
Never for normal production.
Professional Deployment Workflow
Dev Staging Live Phase
| Phase | chattr policy |
|---|---|
| Development | Never use |
| Staging | Test critical file locks only |
| Production | Lock only core security files |
Then automate:
Unlock Update Relock cycle
I can build script for:
- WP single site
- Multi-site
- Multi-tenant VPS (your fleet)
Recommendation For Your Curriculum
Add section:
"Chattr Global Lock = Malware Emergency Only"
Teach students incident-response usage, not default usage.
Want the automation pack?
Reply with which version you want me to generate:
- Single-site WordPress
- Multi-site WordPress
- Multi-tenant WP VPS (your style)
- All scripts + cron update hook
- Emergency malware-recovery lock script
Reply: 1, 2, 3, 4, or 5.
List me what chattr -i and -a that can be used in manage wp server
Below is a **practical, real-world list of how chattr +i (immutable) and chattr +a (append-only) can be used in managing a WordPress VPS.
No emojis, deep sysadmin context.
A. chattr +i (Immutable) Where It Is Safe & Recommended
Protect WordPress Configuration
sudo chattr +i /home/USER/DOMAIN/public_html/wp-config.php
Purpose: database credentials, keys, salts protection
Lock PHP User Configuration (OpenLiteSpeed / Nginx)
sudo chattr +i /home/USER/DOMAIN/public_html/.user.ini
Purpose: prevent malware modifying PHP security flags
Lock .htaccess (Apache setups only)
sudo chattr +i /home/USER/DOMAIN/public_html/.htaccess
Purpose: block rewrite rule injections & backdoors
Lock Entry Point Files (Optional)
sudo chattr +i /home/USER/DOMAIN/public_html/index.php
Purpose: block index entry injection shell
Lock Key Security Tools / Scripts
Example wp backup script:
sudo chattr +i /usr/local/bin/wpbackup
Lock Firewall, Fail2Ban & WAF Config (optional)
sudo chattr +i /etc/fail2ban/jail.local
sudo chattr +i /etc/ufw/user.rules
sudo chattr +i /etc/ufw/user6.rules
Lock SSH & sudoers config
sudo chattr +i /etc/ssh/sshd_config
sudo chattr +i /etc/sudoers
IMPORTANT NOTE
Never use immutable on these in WP production:
| Path | Reason |
|---|---|
/wp-content/uploads/ | Breaks media uploads |
/wp-content/cache/ | Cache plugins fail |
/wp-content/plugins/ | Plugin updates fail |
/wp-content/themes/ | Theme updates fail |
B. chattr +a (Append-only) Where To Use
Server Authentication Logs
sudo chattr +a /var/log/auth.log
Purpose: prevent attackers clearing proof of login attempts
WP Debug Logs (if enabled)
sudo chattr +a /home/USER/DOMAIN/public_html/wp-content/debug.log
Purpose: prevent log tampering
Web Server Logs
Nginx:
sudo chattr +a /var/log/nginx/access.log
sudo chattr +a /var/log/nginx/error.log
OpenLiteSpeed:
sudo chattr +a /usr/local/lsws/logs/access.log
sudo chattr +a /usr/local/lsws/logs/error.log
Audit Trail & Custom Security Logs
sudo chattr +a /var/log/wp-security.log
(Custom locations vary)
C. Removal Commands (for maintenance)
Unlock immutable:
sudo chattr -i file
Remove append-only:
sudo chattr -a file
D. Summary Table
| Action | File Type | Purpose |
|---|---|---|
+i | wp-config | protect secrets |
+i | .user.ini | block PHP override malware |
+i | .htaccess | protect rewrite rules |
+i | security scripts | prevent tampering |
+i | ssh/sudo configs | stop privilege escalation |
+a | auth logs | stop log wiping |
+a | WP debug logs | detect injections |
+a | server logs | prevent attacker cleanup |
E. When NOT To Use chattr
Do not apply on entire WP root or:
- uploads
- cache
- plugins
- themes
- sessions folders
This will break your site.
Want me to generate next?
- Bash automation: unlock update relock
- WP multisite version
- Multi-server fleet version + inventory file
- Malware incident response chattr script
Reply with: 1, 2, 3, or 4.
as i know the capabilities is only for modify , delete and rename i thought it can be executed. read and write
You are mixing **two layers of control and it's normal, many sysadmins get confused here at first.
Lets clear it cleanly and permanently:
chmod controls permissions
Example RWX bits:
| Meaning | chmod controls |
|---|---|
| Read | Can read file contents |
| Write | Can modify file contents |
| Execute | Can run the file (for scripts/binaries) |
So chmod decides if a file should be writable or executable.
chattr controls filesystem behavior
The immutable flag overrides chmod and ACL.
| Attribute | Effect |
|---|---|
+i immutable | Kernel blocks all changes, even if chmod allows them |
So even if a file is writable according to chmod,
chattr +i says:
I don't care what permissions say NO WRITE, NO DELETE, NO RENAME.
But READ and EXECUTE are still allowed.
Why execution still works (important)
PHP execution = reading the file and running the code.
Execution does **not mean editing the file.
So **PHP can execute immutable .php files because:
- It only needs read permission
chattr +idoes not block readschattr +idoes not block execution
Execution Write.
This is the missing link.
Why write does NOT work even if chmod allows it
Example:
chmod 644 wp-config.php
chattr +i wp-config.php
644 says:
Owner can read/write, others read only
BUT kernel says (immutable):
Nobody writes, period.
So write fails:
Operation not permitted
This is why you thought "execute and write has been allowed"
chmod allowed it, but chattr overrides it.
Why +i can break WordPress
Because WP needs to:
- Delete/replace plugin PHP files
- Modify theme PHP files
- Write cache-generated PHP files
- Modify CSS/JS cache files
When +i stops writing, updating, deleting
WP breaks caching, updating, asset generation.
So chattr +i impacts write,
even if chmod says write allowed.
That is the negative effect you are thinking about.
Simple truth table
| Action | Allowed under +i | Why |
|---|---|---|
| Read file | Yes | Needed to load PHP |
| Execute code | Yes | Execution only needs read |
| Edit file | No | Immutable denies write |
| Delete file | No | Immutable denies deletion |
| Rename | No | Immutable denies rename |
| Append | No | Immutable denies append |
Final explanation (in plain language)
chmoddecides who is allowed to writechattr +idecides whether writing is possible at all
So yes, immutable **negatively impacts write operations even if permissions allow them.
That is the entire point of +i deep protection.
If your thought was:
"If chmod allows write, chattr shouldn't block it"
Now you know:
chattr is stronger than chmod.
Think of it like a filesystem bank vault:
chmod= the keychattr +i= welded door shut
Even with the key, door doesn't open.
If you want, I can now give you:
A) Memory shortcut chart to remember layers
B) Visual diagram: ACL vs chmod vs chattr
C) WP-safe chattr automation script
D) Student training example to demonstrate this behavior
Reply: A, B, C, D or ALL.