β οΈ For learning/practice only β DO NOT use direct in Dev/SIT/UAT/Prod environments
1. π Protect /data/script.sh β Read + Execute Only, No Edit/Delete
# Set permissions: read + execute for owner, group, others (no write)
chmod 555 /data/script.sh
# Make the file IMMUTABLE (even root can't edit/delete it)
sudo chattr +i /data/script.sh
# Verify immutable flag is set
lsattr /data/script.sh
# Output: ----i--------e-- /data/script.shβ What this does:
chmod 555β No write accesschattr +iβ is the strongest protection β it prevents modification, deletion, and renaming even by root. (even for root)
π To Undo immutability later:
sudo chattr -i /data/script.sh2. π» Hide Commands from CLI History
Option A β Hide a single command (prefix with space):
# Add a space before the command
sudo ./script.sh βRequires HISTCONTROL=ignorespace or ignoreboth in your shell config.
export HISTCONTROL=ignorebothOption B β Disable history temporarily for a session :
# Turn off history for current session
unset HISTFILE
# Or set file to /dev/null
export HISTFILE=/dev/nullOption C βPermanently configure in ~/.bashrc or ~/.zshrc:
# Ignore commands starting with space, and ignore duplicates
echo 'export HISTCONTROL=ignoreboth' >> ~/.bashrc
# Reload config
source ~/.bashrcOption D β Delete a specific command after running:
# Open history, find the line number, then delete it
history
history -d <LINE_NUMBER>3. π‘οΈ Secure the /data/ Folder β No Add/Delete Files
# Set sticky bit + restrict permissions on the folder
chmod 555 /data # No write permission (can't add/delete files)
# Make the directory itself immutable
sudo chattr +i /data
# Verify
lsattr -d /dataπ Behavior Summary
Command Effect
chmod 555 /data = Removes write permission β no one can add or delete files
chattr +i /data = Immutable flag - Directory locked completely
Sticky bit (chmod +t) = Only file owner can delete their own files (useful for shared dirs)π Quick Verification Commands
ls -la /data/script.sh # Check permissions
lsattr /data/script.sh # Check immutable flag
lsattr -d /data # Check folder flag
stat /data/script.sh # Full file detailsπ 4. π Monitor Changes Using -w (Audit Example)
Use auditd to watch files/directories for changes.
Install & start auditd
sudo apt install auditd -y # Ubuntu/Debian
sudo systemctl enable auditd
sudo systemctl start auditdAdd watch on file
sudo auditctl -w /data/script.sh -p rwxa -k script_monitorAdd watch on directory
sudo auditctl -w /data/ -p wa -k data_monitorπ Meaning of flags
| ---- | ------------------------ |
| Flag | Meaning |
| ---- | ------------------------ |
| `-w` | Watch file/directory |
| `-p` | Permissions to monitor |
| `r` | Read |
| `w` | Write |
| `x` | Execute |
| `a` | Attribute change |
| `-k` | Key (for searching logs) |
| ---- | ------------------------ |π Check logs
sudo ausearch -k script_monitor
sudo ausearch -k data_monitorβ Remove watch
sudo auditctl -W /data/script.sh
sudo auditctl -W /data/β οΈ Important Notes
chattr +iis very powerful β use carefully β requires root/sudo to set and unset- Root cannot override immutable without removing flag
- Hiding from history only works if you run the command β system logs (like
auditd) may still record it - Always test in lab environment only
π Real-World Use Cases (VERY IMPORTANT)
β Use Case 1: Protect Production Deployment Script
Problem:
Dev accidentally modifies deploy.sh
Solution:
chmod 555 /prod/deploy.sh
chattr +i /prod/deploy.sh
auditctl -w /prod/deploy.sh -p wa -k deploy_watchβ Prevents:
- Accidental edits
- Malicious tampering
β Use Case 2: Detect Unauthorized Changes
Problem: Someone modifies critical config silently
Solution:
auditctl -w /etc/nginx/nginx.conf -p wa -k nginx_change
ausearch -k nginx_changeβ Result:
- Full audit trail
β Use Case 3: Restrict Production Access
Problem: Too many users have root access
Solution:
- Remove direct SSH root login
- Use sudo with logging
β Result:
- Traceable actions
- Reduced blast radius
π§ͺ DevOps Security Lab: Attack vs Defense (Step-by-Step)
π― Lab Goal
Simulate:
- Unauthorized file modification
- Secret exposure
Then implement:
- File protection
- Audit monitoring
- Access restriction
ποΈ Lab Setup
1. Create Test Environment
sudo mkdir -p /lab/data
cd /lab/data2. Create a "critical script"
cat <<EOF > deploy.sh
#!/bin/bash
echo "Deploying application..."
EOF
chmod +x deploy.shπ΄ Phase 1: Simulate Attack
π¨βπ» Attack 1 β Modify Critical Script
echo 'echo "HACKED by attacker"' >> deploy.shRun it:
./deploy.shπ Output:
Deploying application...
HACKED by attackerβ Impact: Production script tampered
π Attack 2 β Read Secret File
echo "DB_PASSWORD=SuperSecret123" > /lab/data/.env
cat /lab/data/.envβ Impact: Sensitive data exposed
π’ Phase 2: Apply Defense
π‘οΈ Defense 1 β Protect Script
chmod 555 deploy.sh
sudo chattr +i deploy.shTry attack again:
echo "malicious code" >> deploy.shπ Output:
Operation not permittedβ Attack blocked
π‘οΈ Defense 2 β Monitor File Changes (-w)
sudo apt install auditd -y
sudo systemctl start auditd
sudo auditctl -w /lab/data/deploy.sh -p wa -k deploy_watchTry modifying:
sudo chattr -i deploy.sh
echo "test change" >> deploy.shCheck logs:
sudo ausearch -k deploy_watchβ You'll see who changed it + when
π‘οΈ Defense 3 β Secure Secrets
chmod 400 /lab/data/.envTry access as another user:
sudo useradd attacker
sudo su - attacker
cat /lab/data/.envπ Output:
Permission deniedβ Secret protected
π‘οΈ Defense 4 β Lock Directory
sudo chattr +i /lab/dataTry:
touch test.txtπ Output:
Operation not permittedβ Directory protected
π Phase 3: Detection & Analysis
Check file attributes
lsattr deploy.shCheck permissions
ls -l deploy.shAudit logs
ausearch -k deploy_watchπ§ What You Learned (Real DevOps Mapping)
| ---------------------- | --------------- | ------------------ |
| Attack | Real Scenario | Defense |
| ---------------------- | --------------- | ------------------ |
| Script modified | CI/CD tampering | `chattr +i` |
| Secret exposed | Git leak | `chmod 400`, Vault |
| Unauthorized access | Insider threat | auditd |
| File creation/deletion | Malware | immutable dir |
| ---------------------- | --------------- | ------------------ |π Advanced Challenge
Try these:
1. Break your own security
sudo chattr -i deploy.shπ Learn: root control limitations
2. Add real-time monitoring
Use:
- Falco
- Wazuh
3. Simulate CI/CD attack
- Add secret in script
- Push to Git
- Scan with: - Trivy
β οΈ Key Takeaways
chmod= basic protectionchattr +i= strong protectionauditd -w= visibility- Secrets must NEVER be open
- Security = Prevent + Detect
π§© Real Interview Insight
If asked:
π "How do you secure production scripts?"
Answer:
"I use immutable flags (
chattr +i), restrict permissions (chmod 555), monitor via auditd (-w), and ensure secrets are managed via vault instead of files."
π Conclusion
DevOps without security is a risk multiplier.
By combining:
- Strong access control
- Secure pipelines
- File-level protection
- Continuous monitoring
You can transform your infrastructure into a resilient, production-ready system
π Thank You..! ππ
If you found this article helpful: π Click the clap button π Drop a comment with your questions π Follow for more DevOps, Linux & Cloud tutorials