Skip to content

iptables / nftables Rule Generator

Pure frontend: pick a scenario, fill in interfaces and addresses, and get iptables / nftables commands plus persistence steps. Text is assembled locally — no firewall changes, no scanning, nothing uploaded.

How to use

Choose a scenario: outbound sharing (SNAT/MASQUERADE), publishing a port to an internal host (DNAT), local port rewriting (REDIRECT), or an nftables version. Fill in the egress interface, source subnet, target address and ports, then hit Generate rules for a copyable command set with forwarding, ip_forward and persistence steps alongside it.

Get the direction right: which chain does NAT happen on

In the nat table, what you rewrite determines which chain the rule belongs to. This is the core reference on the page; choosing the wrong chain makes everything after it pointless.

Goal What changes Chain Common action
Internal hosts share a connection Source address POSTROUTING (after routing, before egress) SNAT / MASQUERADE
Publish an internal service Destination address PREROUTING (after ingress, before routing) DNAT
Rewrite a port on this host Destination becomes localhost PREROUTING or OUTPUT REDIRECT

Source translation happens as the packet leaves, because only then do you know the egress interface and source IP. Destination translation has to happen early because routing depends on the destination. Remember "rewrite the destination early, the source late" and chain selection stops being confusing.

Choosing between SNAT and MASQUERADE

Both rewrite source addresses on POSTROUTING; the difference is where the IP comes from. SNAT needs an explicit --to-source, right for a fixed address in a datacentre and more precise. MASQUERADE does not pin an address and uses the egress interface's current IP, right for dial-up, DHCP, elastic IPs or 4G.

A subtle failure is leaving SNAT in place after the outbound IP changes. The rule does not error — it still matches, but rewrites the source to an address the machine no longer owns, so replies never come back. It looks like "the gateway pings but the internet does not work" and is hard to connect to the NAT rule. Fixed IPs with MASQUERADE work fine but cost an extra lookup; unnecessary, not broken.

DNAT is more than one command

The most-missed part of port forwarding is the way back. DNAT only rewrites the destination, turning publicIP:8080 into internalIP:80. For the packet to actually be forwarded, three things must hold: IP forwarding is on (net.ipv4.ip_forward=1, persisted in sysctl), the FORWARD chain accepts traffic to the internal target, and the return path works — the internal host routes back through this gateway, or the gateway SNATs/MASQUERADES the reply. Otherwise the reply takes another route and the connection never opens.

The generator lists those alongside the DNAT rule because the trap is so common. Copy only the DNAT line and you will likely end up with a rule that looks installed but carries no traffic. Also note that traffic originating on the host itself does not traverse PREROUTING, so testing the mapped public port locally fails while external access works — an extra layer of confusion. Add an OUTPUT-chain DNAT if you need that case.

Rule order and persistence

iptables matches top-down and the first hit wins. INSERT at the head of a chain and APPEND at the tail can behave completely differently if an earlier ACCEPT or DROP catches the packet first. Before adding to an existing ruleset, look at it with iptables -t nat -L -n --line-numbers and decide where the new rule belongs.

Persistence is the other required piece. iptables rules exist only in memory and disappear on reboot. Debian/Ubuntu use netfilter-persistent save, RHEL uses service iptables save or a saved iptables-save output. For nftables, redirect nft list ruleset into /etc/nftables.conf and enable the service. The generator prints the relevant commands so you can pick the one for your distribution.

One last boundary: this tool only writes command text. It does not connect to your host, read existing rules or detect port conflicts. Before running firewall commands remotely, keep a session that will not drop or out-of-band access — one badly written DROP can lock you out.

Official documentation