Security teams are understaffed. Bash scripting automates repetitive security tasks: log analysis, user auditing, firewall backup, and vulnerability scanning. A 50-line Bash script on a cron schedule performs hours of analyst work weekly.
Automation Principles
Idempotency: running twice causes no harm. Logging: all actions timestamped. Error handling: fail safely. Least privilege: minimum permissions. These principles separate professional automation from dangerous ad-hoc scripting.
System Audit Script
Run weekly: open ports via ss -tlnp, failed SSH logins from auth.log, SUID files, UID 0 accounts (only root should appear), world-writable directories, active cron jobs. Save to timestamped reports. Schedule via cron: 0 3 weekly on Sunday.
Brute-Force Log Analysis
Parse auth.log for top attacking IPs. Alert on IPs with both failed AND successful logins – potential brute-force success. Generate daily report of top 20 attacking IPs. Email to security team via cron job at 0700 daily.
User Account Audit
Automate checks: empty passwords, all UID 0 accounts, accounts inactive for 90+ days via lastlog, users with sudo access. Alert on any change from previous baseline. Run weekly and on any account modification event.
Firewall Backup Script
Daily: save UFW status numbered, iptables-save, ip6tables-save to timestamped backup files in /opt/security/firewall-backups/. Rotate and keep last 30 days via find -mtime +30 -delete. Store backups off-host. Firewall configuration is a critical business continuity asset.
CVE Scanning Automation
Run lynis audit system weekly, extract hardening index, alert if score drops below 70. Integrate OpenVAS or Nessus via API for scheduled CVE scanning. Send reports to security email distribution list automatically. Track score trend over time to detect configuration drift.
🔗 Further Reading
FAQs
Why use Bash instead of Python for security automation?
Bash is available on every Linux system without installation. For tasks calling system utilities (grep, awk, find, ss, systemctl) Bash is simpler and more efficient. Use Python for complex data processing and API integration. Use Bash for system-level tasks and Python for orchestration.
How do I run security scripts safely?
Create a dedicated service account with only specific permissions required. Use sudo rules granting access to specific commands only. Never run automation as root unless absolutely necessary. Store credentials in environment variables or secret managers, never in the script itself.
Key Takeaways
- Bash automation multiplies analyst effectiveness on repetitive security tasks
- Every script must log actions, handle errors gracefully, and run with least privilege
- Idempotency ensures scripts are safe to run multiple times without adverse effects
- Schedule weekly audits, daily log analysis, and real-time alerting via cron
- Track Lynis hardening score trends to detect configuration drift between audits
Conclusion
Bash scripting for security automation transforms manual tasks into reliable, repeatable processes. Start with audit and log analysis scripts, add firewall backup automation, then build toward automated alerting. Automation is what allows small security teams to monitor large environments effectively. Related: Linux Server Security Hardening.
Sources
- Linux Bash Security Scripting – linuxsecurity.com, bash.cyberciti.biz
- SANS Security Automation Guide – sans.org
- Lynis Documentation – cisofy.com
Scheduling Security Scripts
Use cron for time-based automation: crontab -e. Format: minute hour day month weekday /path/to/script.sh. Use systemd timers in production for better logging. Key automation targets: failed login alerting (parse /var/log/auth.log), open port scanning (ss -tlnp vs approved baseline), file integrity checks (sha256sum on critical binaries), and log rotation verification.
Key Takeaways
- Dedicated low-privilege service account for all automation scripts
- Credentials in environment variables or secret manager, never hardcoded
- Systemd timers preferred over cron for production automation in 2026
- Log all script output for audit trail and incident investigation
Security Automation Best Practices
Version-control all security scripts in Git with access restricted to security team members. Document each script with a header comment explaining purpose, author, and last review date. Test scripts in a staging environment before production deployment. Set up monitoring on the scripts themselves – alert if a scheduled security script fails to run or exits with an error code.
