CentOS with multiple NIC and static route

centosnetworkingrouting

I am trying to setup a CentOS with 3 NIC and static routes.
eth0 has IP address 192.168.10.2 and GW:192.168.10.1,
eth1 has IP address 192.168.20.2 and GW:192.168.20.1,
eth2 has IP address 192.168.10.3 and GW:192.168.10.1.

I want to accept traffic on eth1 from other machines (I have set eth1 of my server as gateway for those client machines) and send it over eth2 and further to router and vice-versa(reverse path).

Added route to system as

ip route add default via 192.168.10.3 dev eth1  proto static  metric 1024

Now I am facing problem like traffic is receiving on eth1 but not going through eth2. It look like going through eth0.

I do not want to use NAT or IPTABLES for now, is it possible to route traffic of eth1 to eth2 and vice versa.

Best Answer

You can turn on IP forwarding with

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

But that's only half the truth. Not using NAT means, that all hosts need to know which router (gateway) is serving which network. As an example a machine in 192.168.10.0/24, say 192.168.10.25, wishes to connect with a machine in 192.168.20.0/24, say 192.168.20.25, using 192.168.10.3 in one network & 192.168.20.2 in the other. The machine 192.168.10.25 needs this route

route add -net 192.168.20.0 netmask 255.255.255.0 gw 192.168.10.3

and the machine 192.168.20.25 needs

route add -net 192.168.10.0 netmask 255.255.255.0 gw 192.168.20.2

Alternatively, you could use NAT & IPTables

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

iptables --flush

iptables -t nat --flush

iptables -t mangle --flush

iptables -A INPUT -i lo -j ACCEPT

iptables -A OUTPUT -o lo -j ACCEPT

iptables --policy INPUT ACCEPT

iptables --policy OUTPUT ACCEPT

iptables --policy FORWARD ACCEPT

iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE

This way you only have to configure (assuming routes are in place) the router.