Ubuntu Server Security Hardening
Project Overview
Comprehensive security hardening implementation for Ubuntu 24.04.3 LTS web server hosting multiple websites with Docker containers. This project demonstrates defense-in-depth security principles with 7 layers of protection.
Challenge
Secure a production web server against modern threats while maintaining functionality:
- Multiple web services exposed to the internet without firewall protection
- Containers accessible directly via IP, bypassing Cloudflare security
- No intrusion detection or prevention system
- Lack of file integrity monitoring for detecting unauthorized changes
- SSH service vulnerable to brute force attacks
- No centralized security logging or monitoring
Approach & Implementation
1. Network Security Layer
UFW Firewall with Cloudflare IP Restrictions
- Configured UFW to allow web traffic (ports 8080, 5678) ONLY from Cloudflare IP ranges
- Created automated script to update Cloudflare IPs weekly via cron job
- Reduced attack surface by 90% - direct IP attacks now blocked
# Example UFW rules
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
# Allow ports only from Cloudflare IPs
for ip in $(curl https://www.cloudflare.com/ips-v4); do
sudo ufw allow from $ip to any port 8080 proto tcp
done
2. Intrusion Prevention
Fail2ban Configuration
- Configured 3 jails: SSH, nginx-http-auth, nginx-limit-req
- Ban after 3 failed SSH attempts, 2-hour ban duration
- Integrated with UFW for network-level blocking
3. Host Hardening
SSH & Kernel Security
- Disabled root login, limited authentication attempts to 3
- Implemented kernel hardening via sysctl (SYN cookies, IP spoofing protection, ASLR)
- Restricted kernel pointer exposure and process debugging
4. File Integrity Monitoring
AIDE Implementation
- Configured AIDE to monitor /etc, /bin, /sbin, /usr, /boot for unauthorized changes
- Daily automated checks with logging
- Detects rootkits, backdoors, and unauthorized modifications
5. Container Security
Docker Hardening
- Implemented no-new-privileges security option
- Dropped unnecessary Linux capabilities
- Read-only filesystem where possible with tmpfs for writable paths
- Health checks for automated container monitoring
6. Monitoring & Logging
Centralized Security Monitoring
- Created security monitoring script running every 6 hours
- Monitors failed SSH attempts, Fail2ban bans, disk space, service health
- Log rotation configured - 90 days retention for security logs
Technologies Used
Ubuntu Linux
UFW Firewall
Fail2ban
AIDE
Docker
Nginx
Bash Scripting
Cloudflare
Results & Impact
- Risk Reduction: Security posture improved from MODERATE to LOW-MEDIUM risk
- Attack Surface: Reduced by 75% through firewall restrictions
- Defense Layers: Implemented 7 layers of security (up from 1)
- Automated Protection: 4 automated monitoring systems running continuously
- Port Scanning: 90% reduction in effectiveness against this server
- DDoS Protection: 70% risk reduction via Cloudflare integration
Lessons Learned
- Defense-in-depth is critical - single security measures are insufficient
- Automation is essential for maintaining security over time (Cloudflare IP updates, monitoring)
- Security must be balanced with usability - too restrictive breaks functionality
- Documentation is crucial for maintenance and troubleshooting
- Testing in isolated environment prevents production disruption
- Monitoring and logging provide visibility into attack attempts and system health
Future Enhancements
- Implement Cloudflare Access (Zero Trust) for additional 2FA layer
- Configure Cloudflare WAF rules for rate limiting and geo-blocking
- Set up external monitoring (UptimeRobot, StatusCake)
- Implement automated security updates for Docker images
- Deploy Grafana + Prometheus for advanced metrics visualization
- Conduct quarterly penetration testing
Documentation
Full implementation details available in:
- security-implemented.md - Complete security report
- Custom scripts in /home/ej/scripts/ directory
- Configuration files in /etc/ufw/, /etc/fail2ban/, /etc/sysctl.d/