Configures UFW to allow inbound HTTP and HTTPS traffic only from Cloudflare IP ranges.
Use this script for hosts where public web traffic should reach the origin server through Cloudflare rather than directly from the internet.
- Bash 4.0 or newer
- Root privileges
- UFW
curltar- Internet access
From the repository root:
sudo ./hardening/UFW\ Cloudflare/ufw-cloudflare.bashOR from the script directory:
sudo ./ufw-cloudflare.bash- Reads existing UFW rules marked with the
Cloudflare IPcomment. - Downloads current Cloudflare IPv4 and IPv6 ranges from Cloudflare.
- Creates a temporary backup archive of
/etc/ufw. - Temporarily allows ports
80and443from any IP to avoid traffic interruption while rules are replaced. - Removes existing Cloudflare-marked rules.
- Adds new UFW allow rules for Cloudflare IP ranges on TCP ports
80and443. - Removes the temporary allow rule.
The script creates a temporary backup archive similar to:
/tmp/.../ufw-backup-YYYY-MM-DD.tar.gz
If the script is interrupted during rule replacement, it attempts to:
- Disable UFW temporarily.
- Restore the previous UFW configuration from the backup archive.
- Re-enable UFW.
- Print the current UFW status.
The temporary backup is removed during normal cleanup.
- Direct origin access may remain possible through other open ports or non-UFW firewall layers.
Review UFW status after the script finishes:
sudo ufw status verboseYou can also inspect numbered rules:
sudo ufw status numbered- Ubuntu 24.04, 22.04, 20.04
- Debian 11, 10, 9
See CHANGELOG.md.