Port Forwarding with iptables

Thu 02 October 2014
By alex

Configuring a port forward with iptables takes 2 rules, a PREROUTING rule and a FORWARD rule. The heavy lifting is accomplished by the PREROUTING rule on the nat table. The FORWARD rule simply allows traffic across the FORWARD chain on the filter table.

iptables -A PREROUTING -t nat -i [external_iface] -p [proto] --dport [external_port] -j DNAT --to [internal_ip]:[internal_port]
iptables -A FORWARD -p [proto] -d [internal_ip] --dport [internal_port] -j ACCEPT

The FORWARD rule isn't necessarily necessary, but if the default policy for the FORWARD chain on the filter table is DROP, then the port forward won't work without the second rule.