I’ve got two networks on my Apache machine, an internal and an external. The internal network is used to communicate to the MySQL machine housing databases for use by the sites on my Apache machine. The MySQL machine only has an internal network, with no gateway to the world. I need to be able to temporarily add a route to the internet so I can update the MySQL machine. For the explicit purpose of giving internet access for updates to my MySQL machine, I will use my Apache machine as the gateway.
Apache Internal = eth0 (192.168.1.32)
Apache External = eth1 (unimportant)
MySQL Internal = eth0 (192.168.1.31)
Step 1 (Apache machine)
iptables --table nat --append POSTROUTING --out-interface eth1 -j MASQUERADE
iptables --append FORWARD --in-interface eth0 -j ACCEPT
echo 1 > /proc/sys/net/ipv4/ip_forward
Step 2 (MySQL machine)
route add default gw 192.168.1.32
Test your new gateway by pinging an external IP address (like google.com). We didn’t save the firewall rules, or the gateway configuration to any permanent settings so a reboot will clear the configuration.