How to add multiple gateways in Linux

I have two interfaces eth0 and eth1. They belong to different vlans. They are accessible from their respective vlan. I have a need to access eth1 from the other vlan and it doesn’t work. Inter-vlan routing is configured in the switch and ideally it should work. When I ping eth1 from the other vlan, it timeout. This suggests the problem is not reaching eth1 but the response is unable to come through. When the server tries to respond the icmp traffic it receives at eth1, it tries to send it via the default gateway which belongs to eth0. Hence there is a need to configure per interface gateway. Windows let you do this easily.

To fix this, I need to configure policy based routing using iproute2. I followed the steps from here. The steps involve creating routing table for each interface, a route, default gateway for each subnet and then rules for each network.

As the referenced blog has a neat document on the configuration and commands to use, I don’t think I need to duplicate it. Just two notes though-

  1. You need to add the tables to /etc/iproute2/rt_tables before you can add the route and rule
  2. To inspect the route for each table run

    ip route show table tablename

How to add multiple gateways in Linux