Automating Firewall Rules with UFW and Cron Jobs
Automating Firewall Rules with UFW and Cron Jobs
Introduction
Uncomplicated Firewall (UFW) is a user-friendly frontend for managing iptables on Linux. Automating firewall rules with cron jobs allows dynamic security adjustments, such as enabling specific ports during certain times or blocking unwanted connections periodically. This guide will walk you through automating UFW with cron jobs.
Step 1: Install and Enable UFW
- Install UFW if it’s not already installed:
sudo apt install -y ufw
- Enable UFW and allow SSH access:
sudo ufw allow OpenSSH
sudo ufw enable
Step 2: Create Custom Firewall Rules
- Add rules to allow or deny specific traffic. Example: Allowing HTTP traffic:
sudo ufw allow 80/tcp
- Denying a specific IP:
sudo ufw deny from 192.168.1.100
Step 3: Automate Firewall Rules with Cron Jobs
- Open the cron job editor:
crontab -e
- Add a rule to block SSH access every night from midnight to 6 AM:
0 0 * * * sudo ufw deny OpenSSH
0 6 * * * sudo ufw allow OpenSSH
- Save and exit the editor.
Step 4: Verify and Monitor Firewall Rules
- Check the current firewall rules:
sudo ufw status verbose
- Monitor firewall logs in real-time:
sudo tail -f /var/log/ufw.log
Conclusion
Automating firewall rules using UFW and cron jobs enhances security while providing flexibility in managing access. Regularly reviewing firewall logs and rules ensures your system remains protected against unauthorised access.