To configure two gateways on a Linux machine—one for all traffic (default gateway) and the other for a specific network, you have to consider static routes and multiple network interfaces
Suppose you have 2 networks on a PC:
- Default Gateway (GW1) – For General Internet Traffic, 192.168.90.0/24 (eth0).
- eth0 (Connected to GW1: 192.168.90.1
- Specific Gateway (GE2) – For Specific Traffic for AD Authentication, 192.168.0.1/24 (eth1)
- eth1 (Connected to GW2: 192.168.1.1)
Set the Default Gateway (GW1)
You can set the default gateway for all traffic using the following command:
ip route add default via 192.168.90.1 dev eth0
This sends all non-specific traffic through GW1.
Add Static Route for Specific Network Range via GW2
ip route add 172.21.0.0/16 via 192.168.1.1 dev eth1
ip route add 144.21.0.0/16 via 192.168.1.1 dev eth1
This ensures traffic for 172.21.0.0/16 and 144.21.0.0/16 is routed via GW2.
Verify the Route
ip route show
default via 192.168.90.1 dev eth0 proto static metric 102
172.21.0.0/16 via 192.168.1.1 dev eth1 proto static metric 103
144.21.0.0/16 via 192.168.1.1 dev eth1 proto static metric 103