NAT'ing two sub-nets thru Linux


This page describes how to configure Linux to properly route network traffic between two separate networks. This works the same for wireless and wired connections.

IP Address of Middle

Have a computer that has a NIC with an IP address on each network. I’m not sure if this is called a router or a bridge or something else. For this page, I call this computer the “middle”. Use ifconfig to verify that each network card is up. Try pinging a host on each network as a second check.

Routing of Middle

Check the routing for each of the cards in the middle. If pinging failed, it was probably because routing is broken. Use netstat -nr to quickly see the routes to each subnet. Then, just add routes to the appropriate NIC for each subnet. Adding a route will look like this:

route add -net netmask dev eth0
route add -net netmask dev eth1

NAT in Middle

Turn on NAT’ing on the middle:

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

Route the clients

For each client machine, simply adjust the routing to use the middle as a gateway. I’m assuming the middle has the IP address of .5 in each network (eg and

A computer on the network would use the following command to NAT to

route add -net netmask gateway dev eth0

This tells the the client machine that all 192.168.0.x traffic goes thru eth0 to for further routing. At that point it will use the routing tables in middle jump to the 192.168.0.x subnet and go to the target machine.


271 Words

2011-04-18 23:39 +0000