Networking – Ubuntu Server 16.04 to 2 Gateways at Same Time

gatewaynetwork-managernetworkingservervpn

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

Best Answer

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.

Related Question