Question:

How to use a Linux machine as the router?


Solution:

To use a Linux box as a router, you must run these Linux commands on a terminal window, for IP routing between different physical Ethernet networks.

sudo sysctl -w net.ipv4.ip_forward=1
sudo modprobe iptable_nat
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth1 -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o eth1 -p tcp  -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o eth1 -p udp  -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -p tcp  -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -p udp  -j ACCEPT
sudo iptables -A FORWARD -i eth1 -o eth0 -m state --state ESTABLISHED,RELATED    -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE
sudo iptables -A FORWARD -i eth0 -o eth1 -m state --state ESTABLISHED,RELATED    -j ACCEPT
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i eth1 -o eth0  -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o eth1  -j ACCEPT


NOTE: Ensure that the Linux machine has 2 NIC ports and they are named eth0 and eth1.