Linux – Routing between two subnets using a Linux box with two NICs

iptableslinuxnetworkingroutingsubnet

I have a Linux box with two NICs, each connected to a different LAN:

  • Network A: 192.168.1.0 255.255.255.0
  • Network B: 192.168.2.0 255.255.255.0

The routers are:

  • 192.168.1.1
  • 192.168.2.1

Linux NICs are:

  • 192.168.1.2
  • 192.168.2.2

I want all hosts in network A to be able to ping all hosts in network B and vice versa.

What I've done so far:

On a Linux box: sudo sysctl -w net.ipv4.ip_forward=1

On 192.168.1.1: route add 192.168.2.0 255.255.255.0 gw 192.168.1.2

On 192.168.2.1: route add 192.168.1.0 255.255.255.0 gw 192.168.2.2

I tried it with two different linux distributions (Zentyal and Ubuntu) and nothing's working.

Hosts in network A can only ping 192.168.2.2, but can’t ping any of the other hosts in network B and vice versa. What am I doing wrong?

Best Answer

You should set up the following static routes:

On 192.168.1.1 router:

192.168.2.0/24 next hop 192.168.1.2

On 192.168.2.1 router:

192.168.1.0/24 next hop 192.168.2.2

This way the other computers in those networks send packets to their default gateway (.1), which then uses its static routing table entry to forward the packet to Linux box, which then forwards the packet to the other network.