Configure Linux as a Router

Thu 02 October 2014
By alex

This guide is for a dual NIC machine. The machine will use iptable's NAT feature to act as a router/gateway. This guide does not require editing of the system's routing table.

1) Enable routing in the kernel

sudo vi /etc/sysctl.conf
net.ipv4.ip_forward = 1     # Uncomment this line

Enable the changes

sudo sysctl -p

2) Configure iptables to route packets. Verify that you're configuring the correct interfaces for in/out

sudo iptables -t nat -A POSTROUTING --out-interface eth1 -j MASQUERADE
sudo iptables -A FORWARD --in-interface eth0 -j ACCEPT

3) Write the iptables ruleset to a firewall script or file via iptables-save

sudo iptables-save > /etc/firewall.rules

4) Configure the ruleset to load at boot time

sudo vi /etc/rc.local
iptables-restore < /etc/firewall.rules  # Insert above 'exit 0'

5) Reboot and verify