IpTables NAT in 3 steps

NAT — Network address translation — is a handy feature of any firewall, letting you translate the IP addresses of transit packets.

I’ll cover an example of forwarding traffic from one VPN connection to another.
VPN1: tap0
VPN2: ppp0
no binding to ip addresses

First, enable ip address forwarding:

echo 1 > /proc/sys/net/ipv4/ip_forward

Create packet masquerade rules

iptables -t nat -A POSTROUTING -o tap0 -j MASQUERADE  
iptables -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

Allow packets to flow both ways:

iptables -A FORWARD -i ppp0 -o tap0 -j ACCEPT  
iptables -A FORWARD -i tap0 -o ppp0 -j ACCEPT
  • -i - incoming interface
  • -o - outgoing interface