Ubuntu – Ubuntu server 16.04 to 2 gateway at same time


I cant find a working guide to make work the following scenario:

Need to configure a server to be capable of working with 2 routers at same time. Its a virtual machine.

Network 1 Router 1 with ip

Network 2 Router 2 with ip

I have 2 lan adapters "ens160" primary, and "ens192" secondary. If i active only one adapter i can access from vpn clients to server, doing a ping or ssh. If i put 2 adapters on i cant access from vpn clients from "ens192" the default adapter.

I looking for server works with Network 1 and 2, not need to make network 1 can see network 2 and viceversa. I want my server attend petitions from router 1 responding to router 1 and petitions from router 2 responding to router 2

I review a few guides talking about add routes, but no one works and all are for more old versions of Ubuntu. May be i loosing some detail, but i cant find it.

Some one can point a guide for ubuntu 16.04 working with 2 gateways at same time.

Edit to add more info to Alvaro:

Thansk for your help.

oscar@LinuxTest:~$ route -n
Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface         UG    0      0        0 ens192   UG    0      0        0 ens160   U     0      0        0 ens160   UG    0      0        0 ens192   U     0      0        0 ens192

Whit this i only got working the petitions from router 2 from, if i delete the default Gateway no one works.

The theory is If petition comes from respond to by ens160. If petition comes from respond to by ens192

Best Answer

This is really easy, just add routes as following... In this case I'll consided ens160 as default route:


route del default dev ens192
route add default dev ens160 gw
route add -net VPN_NETWORK_1/VPN_NETWORK_MASK_1 dev ens192 gw
route add -net VPN_NETWORK_2/VPN_NETWORK_MASK_2 dev ens192 gw
route add -net VPN_NETWORK_3/VPN_NETWORK_MASK_3 dev ens192 gw
route add -net VPN_NETWORK_X/VPN_NETWORK_MASK_X dev ens192 gw

Please check your routes, once you have all checked you can add this script to /etc/network/if-up.d/routes with execution permissions in order to have permanent routes (I know there are other ways to do this, although this is the only one that worked for me for permanent routes).

If it doesn't work send me a feedback of your route -n command.

Related Question