1

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 192.168.1.0/24 Router 1 with ip 192.168.1.1

Network 2 192.168.2.0/24 Router 2 with ip 192.168.2.1

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

0.0.0.0         192.168.2.1     0.0.0.0         UG    0      0        0 ens192

192.168.1.0     192.168.1.1     255.255.255.0   UG    0      0        0 ens160

192.168.1.0     0.0.0.0         255.255.255.0   U     0      0        0 ens160

192.168.2.0     192.168.2.1     255.255.255.0   UG    0      0        0 ens192

192.168.2.0     0.0.0.0         255.255.255.0   U     0      0        0 ens192

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

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

2 Answers 2

0

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

#!/bin/bash

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

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.

7
  • oscar@LinuxTest:~$ route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.2.1 0.0.0.0 UG 0 0 0 ens192 192.168.1.0 192.168.1.1 255.255.255.0 UG 0 0 0 ens160 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ens160 192.168.2.0 192.168.2.1 255.255.255.0 UG 0 0 0 ens192 192.168.2.0 0.0.0.0 255.255.255.0 U 0 0 0 ens192 Feb 9, 2018 at 17:42
  • easy, run this ` route add default dev ens160 gw 192.168.1.1 route del default dev ens192` Feb 9, 2018 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. Feb 12, 2018 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?... Feb 12, 2018 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. Feb 12, 2018 at 15:21
0

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
    address 192.168.1.243
    netmask 255.255.255.0
    network 192.168.1.0
    broadcast 192.168.1.255
    gateway 192.168.1.1
    # dns-* options are implemented by the resolvconf package, if installed
    dns-nameservers 8.8.8.8
    dns-search testLinux

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

#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 192.168.2.0/24 dev ens192 table rt2
post-up ip route add default via 192.168.2.1 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 192.168.2.0/24 table rt2
post-up ip rule add to 192.168.2.0/24 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 192.168.1.1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .