UFW Rules Generator
Linux Security & Infrastructure Automation

Ubuntu UFW (Uncomplicated Firewall) Rules & Script Generator

Generate Ubuntu UFW firewall rules, default drop policies, rate limiting, and port forwarding commands with an executable bash script.

Security Score: 100/100
100% Client-Side Privacy
Presets:
Global Firewall Policies & Service Whitelist
Inbound: DENY•Outbound: ALLOW•IPv6: Dual-Stack
Quick Service Injector (One-Click Whitelist)
Configured Rules Table (5)Toggle checkbox to enable/disable
#1limitIN22/tcp
#2allowIN80/tcp
#3allowIN443/tcp
#4allowIN3306/tcpfrom 10.0.0.0/16on eth1
#5allowIN6379/tcpfrom 127.0.0.1on lo
#!/usr/bin/env bash
# ==============================================================================
# Ubuntu UFW (Uncomplicated Firewall) Production Provisioning Script
# Generated securely via ToolsAA (https://toolsaa.com/tools/ufw-rules-generator)
# 100% Client-Side Private • Zero-Knowledge Architecture
# ==============================================================================
set -euo pipefail

# Ensure script is executed with root superuser privileges
if [[ $EUID -ne 0 ]]; then
  echo -e "\033[0;31m[ERROR]\033[0m This script must be run as root: sudo bash $0" >&2
  exit 1
fi

# Step 1: Ensure UFW package is installed and active
if ! command -v ufw &>/dev/null; then
  echo ">>> [INSTALL] UFW not detected. Installing via apt-get..."
  export DEBIAN_FRONTEND=noninteractive
  apt-get update -y && apt-get install -y ufw iptables iptables-persistent
fi

# Step 2: Create atomic timestamped backup of current firewall configuration
BACKUP_DIR="/etc/ufw/backups/ufw_backup_$(date +%Y%m%d_%H%M%S)"
mkdir -p "$BACKUP_DIR"
if [[ -d /etc/ufw ]]; then
  cp -r /etc/ufw/*.rules /etc/default/ufw "$BACKUP_DIR/" 2>/dev/null || true
  echo ">>> [BACKUP] Saved existing configuration to $BACKUP_DIR"
fi

# Step 3: Configure IPv6 policy in /etc/default/ufw
if [[ -f /etc/default/ufw ]]; then
  sed -i 's/^IPV6=.*/IPV6=yes/' /etc/default/ufw
  sed -i 's/^DEFAULT_FORWARD_POLICY=.*/DEFAULT_FORWARD_POLICY="DROP"/' /etc/default/ufw
fi

# Step 4: Reset UFW to a clean baseline (force non-interactive)
echo ">>> [RESET] Resetting UFW to pristine baseline..."
ufw --force reset

# Step 5: Set Global Default Policies
ufw default deny incoming
ufw default allow outgoing
ufw default deny routed
ufw logging low

# Step 6: Loopback (lo) Interface Whitelist
ufw allow in on lo comment 'Allow local loopback incoming'
ufw allow out on lo comment 'Allow local loopback outgoing'

# Step 7: Apply User Defined Firewall Rules
ufw limit 22/tcp comment 'Rate limited SSH access against brute force'
ufw allow 80/tcp comment 'Public HTTP web traffic'
ufw allow 443/tcp comment 'Public HTTPS SSL/TLS traffic'
ufw allow in on eth1 from 10.0.0.0/16 to any port 3306 proto tcp comment 'Internal VPC MySQL cluster replication'
ufw allow in on lo from 127.0.0.1 to any port 6379 proto tcp comment 'Local Redis key-value cache'

# Step 9: Enable Firewall & Verify State
echo ">>> [ENABLE] Activating UFW firewall rules..."
ufw --force enable

echo -e "\n\033[0;32m[SUCCESS]\033[0m UFW Firewall has been configured and activated!"
echo ">>> Active Firewall Status:"
ufw status verbose
echo ""
echo ">>> Numbered Rule List (use: sudo ufw delete <number> to modify):"
ufw status numbered
Active Firewall Rules
5/ 5 defined
Inbound: DENY • Outbound: ALLOW
Open & Forwarded Ports
22, 80, 443, 3306...
0 DNAT forwards • IPv6: Enabled
Security Hardening
100/1000 warning(s)
SSH Rate Limit Active

100% Client-Side Execution

All firewall rules, NAT directives, and deployment scripts are synthesized entirely in your browser without transmitting server IP or topology details.

Production Hardened & Tested

Default deny policies, brute-force rate limiting, and loopback protection ensure your Linux instances avoid remote SSH lockout.

Multi-Format DevOps Export

Export directly as executable bash scripts with safe rollback commands, native iptables before.rules NAT blocks, or Ansible playbook tasks.

Technical Reference & Architecture Guide

Ufw Rules Generator — Reference Guide

Developer utility for ufw-rules-generator.

Key Parameters & Controls

action'allow' | 'deny' | 'reject' | 'limit'

Packet filtering decision applied to traffic matching specified ports, protocols, and CIDR ranges.

Default: allow
direction'in' | 'out'

Traffic packet flow orientation relative to local interfaces ('in' for ingress, 'out' for egress).

Default: in
protocol'tcp' | 'udp' | 'any'

Layer 4 transport protocol filter for packet matching ('tcp', 'udp', or transport-agnostic 'any').

Default: tcp
defaultIncoming'deny' | 'reject' | 'allow'

Global firewall baseline policy enforced for all unsolicited inbound connections.

Default: deny
logging'off' | 'low' | 'medium' | 'high' | 'full'

Kernel packet event verbosity level recorded to /var/log/ufw.log and rsyslog.

Default: low
enableNatMasqueradeboolean

Enables IP masquerading in iptables POSTROUTING chain for private subnet routing.

Default: false

Pro Tips & Best Practices

Mitigate SSH Brute-Force Rate

Use the `limit` action on port 22/tcp instead of `allow`. UFW will automatically deny connections from IP addresses that attempt 6 or more connections within 30 seconds.

Order Specific Rules Before Any

UFW evaluates rules sequentially in first-match order. Ensure tight IP-specific rules (e.g. allowing internal management subnets) are inserted before blanket subnet or port blocks.

Fix Docker Host Port Bypass

Docker binds directly to PREROUTING iptables chains before UFW rules execute. Place restrictive forward rules in `/etc/ufw/after.rules` under the DOCKER-USER chain or set `iptables: false` in `/etc/docker/daemon.json`.

Common Gotchas & Troubleshooting

SSH access is severed immediately when running 'ufw enable'

Pre-seed an explicit rule `ufw allow 22/tcp` prior to running enable. In emergency lockouts on cloud VPS instances, use the hypervisor web/VNC console to execute `ufw disable` or append rules to `/etc/ufw/user.rules`.

Published Docker container ports bypass UFW deny rules

Docker injects rules directly into the PREROUTING iptables chain before UFW filters apply. Set `"iptables": false` in `/etc/docker/daemon.json` or add custom drop rules for public interfaces directly inside `/etc/ufw/after.rules` under the DOCKER-USER chain.

Frequently Asked Questions

Does UFW override Docker port bindings defined with docker run -p?▼

No. Docker creates PREROUTING and DOCKER iptables chains that are traversed before UFW's user input chains. Traffic bound to container ports bypasses standard UFW rules unless routed via DOCKER-USER chains or with Docker iptables integration disabled.

How do deny and reject packet handling policies differ in UFW firewall rules?▼

The 'deny' rule silently drops incoming packets, leaving the remote sender to hang until connection timeout occurs. The 'reject' rule drops the packet while returning an ICMP Port Unreachable message or TCP RST packet, notifying the client immediately.

How does UFW persist rules across system reboots?▼

Rules configured via UFW are written directly into `/etc/ufw/user.rules` (IPv4) and `/etc/ufw/user6.rules` (IPv6). The `ufw.service` systemd unit automatically reloads these tables during system initialization.

Are generated firewall rules sent to or stored on external servers?▼

No. The generator processes all rules, configuration parameters, bash scripts, and Ansible tasks strictly within your local browser memory. No infrastructure data or IP definitions leave your device.

Related Tools & Utilities

All Tools →