Skip to main content

rawops.dev

Skip to tool content

Firewall Rule Generator

Define firewall rules once, export to 6 formats: iptables, nftables, UFW, AWS Security Groups, Azure NSG, or GCP Firewall. Built-in conflict detection. 100% client-side — nothing leaves your browser.

Linux netfilter

iptables Settings

No rules yet. Click “Add Rule” or pick a recipe below.
No rules defined — output will only contain default policy

Generated Rules

iptables-rules.sh
#!/bin/bash
# Generated by RawOps.dev — Firewall Rule Generator
# Format: iptables

# Set ACCEPT first to avoid lockout during rule loading
iptables -P INPUT ACCEPT
iptables -P OUTPUT ACCEPT

# Flush existing rules
iptables -F INPUT
iptables -F OUTPUT

# Allow loopback
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# Allow established and related connections
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT


# Apply default policies (set last to prevent lockout)
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP

Quick Recipes

Click a recipe to populate the form with common firewall rules.

Firewall Configuration Guide

A firewall controls network traffic by enforcing rules that allow or deny connections based on IP addresses, ports, and protocols. On Linux servers, iptables (legacy) and nftables (modern replacement) are the standard kernel-level packet filters. UFW provides a simpler interface on top of iptables for Ubuntu/Debian systems. Cloud providers offer their own abstractions: AWS Security Groups, Azure NSGs, and GCP Firewall Rules.

Linux Firewall Comparison

FeatureiptablesnftablesUFW
StatusLegacy (still widely used)Recommended (kernel 3.13+)Frontend for iptables
SyntaxCommand-line flagsStructured ruleset fileSimple keywords
Atomic updatesNo (per-rule)Yes (full ruleset)No
IPv6Separate ip6tablesUnified (inet family)Built-in
Learning curveSteepModerateEasy

Cloud Firewall Comparison

FeatureAWS SGAzure NSGGCP Firewall
DefaultDeny all inboundDeny all inboundDeny all inbound
Deny rulesNot supportedSupportedSupported
PriorityNot applicable100-40960-65534
StatefulYes (always)Yes (always)Yes (always)
ScopeVPC + instanceSubnet or NICNetwork + tags

Common Ports Reference

PortProtocolService
22TCPSSH
53TCP/UDPDNS
80TCPHTTP
443TCPHTTPS
3306TCPMySQL / MariaDB
5432TCPPostgreSQL
6379TCPRedis
6443TCPKubernetes API
8080TCPHTTP Alternate
9090TCPPrometheus
27017TCPMongoDB
30000-32767TCPKubernetes NodePort
51820UDPWireGuard

iptables Packet Flow & Chain Architecture

Understanding how packets traverse iptables chains is essential for writing correct rules. Packets follow different paths depending on whether they're destined for the local machine, being forwarded (routed), or originating from the local machine:

Incoming Packet
      │
      ▼
 PREROUTING (nat, mangle) ─── Destination NAT (DNAT), port forwarding
      │
      ├── For local machine? ──▶ INPUT (filter) ──▶ Local Process
      │                                              │
      └── For another host? ──▶ FORWARD (filter) ──────┐
                                                        │
                                                        ▼
Local Process ──▶ OUTPUT (filter, nat) ──▶ POSTROUTING (nat, mangle)
                                                        │
                                       ◀────────────────┘
                                                        │
                                                        ▼
                                                  Network (out)

Key rules:
• Firewall rules for server services → INPUT chain
• Firewall rules for routed traffic → FORWARD chain (routers, Docker hosts)
• NAT rules (port forwarding) → PREROUTING + POSTROUTING
• Outbound filtering → OUTPUT chain (rarely needed)

In nftables, the same concepts apply but chains are user-defined. You create chains with explicit types (type filter hook input) instead of the fixed chain names.

iptables vs nftables: Syntax Comparison

nftables replaces iptables with a cleaner syntax and better performance. Here are the same rules written in both formats:

Taskiptablesnftables
Allow SSHiptables -A INPUT -p tcp --dport 22 -j ACCEPTnft add rule inet filter input tcp dport 22 accept
Allow from subnetiptables -A INPUT -s 10.0.0.0/8 -j ACCEPTnft add rule inet filter input ip saddr 10.0.0.0/8 accept
Drop all inboundiptables -P INPUT DROPnft add rule inet filter input drop
Conntrackiptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPTnft add rule inet filter input ct state established,related accept
Log + dropiptables -A INPUT -j LOG --log-prefix "DROP: "nft add rule inet filter input log prefix "DROP: " drop
List rulesiptables -L -n -v --line-numbersnft list ruleset

Key nftables advantages: atomic ruleset updates (replace entire ruleset in one operation), unified IPv4/IPv6 (inet family handles both), sets and maps (match against lists without multiple rules), and better performance (fewer kernel modules).

Docker & iptables: The DOCKER-USER Chain

Docker modifies iptables rules automatically, which can bypass your INPUT chain rules. When you publish a port (-p 8080:80), Docker adds DNAT rules in PREROUTING and FORWARD chain rules that bypass INPUT entirely.

# Problem: This rule does NOT block Docker-published ports!
iptables -A INPUT -p tcp --dport 8080 -j DROP  # Useless for Docker ports

# Solution: Use the DOCKER-USER chain (processed before Docker's rules)
iptables -I DOCKER-USER -p tcp --dport 8080 -s 0.0.0.0/0 -j DROP
iptables -I DOCKER-USER -p tcp --dport 8080 -s 10.0.0.0/8 -j ACCEPT

# Allow only specific IPs to access Docker-published ports
iptables -I DOCKER-USER -i eth0 -j DROP
iptables -I DOCKER-USER -i eth0 -s 10.0.0.0/8 -j RETURN
iptables -I DOCKER-USER -i eth0 -p tcp --dport 80 -j RETURN
iptables -I DOCKER-USER -i eth0 -p tcp --dport 443 -j RETURN

# Persist DOCKER-USER rules (they survive Docker restarts)
# Create a systemd service that loads rules on boot

This is the most common Docker security mistake: administrators add INPUT chain rules thinking they protect Docker services, but published ports are routed through the FORWARD chain. Always use DOCKER-USER for firewall rules on Docker hosts. Alternatively, bind ports to 127.0.0.1 and use a reverse proxy.

Common Firewall Mistakes

MistakeConsequenceFix
Locking yourself out via SSHCan't access server remotelyAlways add SSH allow rule FIRST, use at command to auto-flush rules after 5 min
No conntrack ruleReturn traffic blocked (outbound DNS, apt, etc.)Add ESTABLISHED,RELATED rule before specific rules
Rules not persistedRules lost on rebootUse iptables-persistent or nft -f /etc/nftables.conf
Forgetting IPv6IPv6 traffic bypasses IPv4 rulesUse ip6tables or nftables inet family for dual-stack
REJECT vs DROPDROP causes timeouts; REJECT sends RST/ICMPUse REJECT for internal services (faster failure), DROP for internet-facing (no info to attacker)
Docker bypasses INPUTPublished ports accessible despite INPUT DROPUse DOCKER-USER chain or bind to 127.0.0.1

IPv6 Support

This generator fully supports IPv6 addresses and CIDR ranges. Enter IPv6 CIDRs like 2001:db8::/32, fe80::1/64, or ::/0 (all IPv6) in source/destination fields. For iptables, IPv6 rules are automatically output as ip6tables commands with essential ICMPv6 rules included. For nftables, the inet family handles both IPv4 and IPv6 in a single ruleset. UFW and cloud formats (AWS, Azure, GCP) pass IPv6 CIDRs through natively. The conflict detection engine correctly handles IPv6 range overlap analysis.

Privacy First

All rules are generated entirely in your browser using JavaScript. Your IP addresses, subnets, and infrastructure details are never sent to any server. This tool has zero backend dependencies.

Related Tools & Resources