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


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.

  • 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 – Oscar Soriano Feb 9 '18 at 17:42
  • easy, run this ` route add default dev ens160 gw route del default dev ens192` – Alvaro Niño Feb 9 '18 at 19:16
  • Thats not work, only make work ens160, the default route you add. I need to work at same time with ens160 and ens190. – Oscar Soriano Feb 12 '18 at 8:52
  • Let me clarify, do you need to combine traffic for both interfaces or you just need to use an interface or other depending on your destination IP?... – Alvaro Niño Feb 12 '18 at 9:35
  • 1
    Thanks Alvaro for your time i find a solution. Gracias por tu ayuda y tu tiempo he encontrado la solución. – Oscar Soriano Feb 12 '18 at 15:21

My error was thinking i need to reconfigure the 2 adapters to work, when all i need was is tell the new adapter to use his own gateway (from the network has configured)

My thanks to Alvaro for his responses and help.

Links help me to resolve was:

Two Default Gateways on One System

A Quick Introduction to Linux Policy Routing

SOLUTION: Create a new routing table for second adapter "ens192" and add a line whit the name of the new routing table "2 rt2"

oscar@LinuxTest:~$ sudo nano /etc/iproute2/rt_tables

GNU nano 2.5.3                                   File: /etc/iproute2/rt_tables

# reserved values
255     local
254     main
253     default
0       unspec
# local
#1      inr.ruhep
2 rt2

Add the post-up lines to the "/etc/network/interfaces"

oscar@LinuxTest:~$ sudo nano /etc/network/interfaces

GNU nano 2.5.3                                   File: /etc/network/interfaces

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

# The primary network interface
# Normal configuration for static ip
auto ens160
iface ens160 inet static
    # dns-* options are implemented by the resolvconf package, if installed
    dns-search testLinux

#Only address and mask for the new adapter
auto ens192
iface ens192 inet static

#Every time the adapters goes Up the next lines are executed.
#these 2 lines fill the new table with 2 routes for the "ens192" adapter     and establish the default gateway for that adapter.
post-up ip route add dev ens192 table rt2
post-up ip route add default via dev ens192 table rt2

#these 2 lines add a rule from/to new network to use the new table.
post-up ip rule add from table rt2
post-up ip rule add to table rt2

oscar@LinuxTest:~$ sudo reboot

if you loosing the default adapter ens160 stop working, recreate the default route for him.

route add default dev ens160 gw

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.