Ubuntu – Netplan, two gateways for two subnets

gatewaynetplanroutingsubnetUbuntu

I'm trying to get working following:

Have 2 subnets (ie. 192.168.15.0/24 and 192.168.19.0/24) and two gateways (192.168.105.1/24, 192.168.109.1/24), and Ubuntu 18.04 server with netplan as router (.1/24 in subnets, .2/24 in gateway subnets).
Goal is to use one gateway for for one subnet and other gateway for second subnet.
Even if I'm using source-based routing as in netplan docs, ending with two gateways (random order) and only one subnet can access internet.

Please, how can I make it work?

Thanks

edit: – added settings

with this setting, works from 192.168.105.xx to internet, but nothing from 192.168.109.xx.

And with route there are two gateways for 0.0.0.0/0

/etc/iptables/rules.v4

# Generated by iptables-save v1.6.1 on Sat Aug 22 10:38:15 2020
*nat
:PREROUTING ACCEPT [386:180180]
:INPUT ACCEPT [0:0]
:OUTPUT ACCEPT [1:76]
:POSTROUTING ACCEPT [1:76]
-A POSTROUTING -s 192.168.15.0/24 -o br105 -j MASQUERADE
-A POSTROUTING -s 192.168.19.0/24 -o br109 -j MASQUERADE
COMMIT
# Completed on Sat Aug 22 10:38:15 2020
# Generated by iptables-save v1.6.1 on Sat Aug 22 10:38:15 2020
*filter
:INPUT ACCEPT [4:1760]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [28:2536]
-A INPUT -i br105 -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -i br109 -m state --state RELATED,ESTABLISHED -j ACCEPT
COMMIT
# Completed on Sat Aug 22 10:38:15 2020

/etc/netplan/01-netcfg.yaml

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp7s0:
      dhcp4: no

    enp7s1:
      dhcp4: no

    enp1s0f0:
      dhcp4: no

    enp1s0f1:
      dhcp4: no


  bridges:
    br15:
      dhcp4: no
      interfaces: [ enp7s0 ]
      addresses: [ 192.168.15.1/24 ]

    br19:
      dhcp4: no
      interfaces: [ enp7s1 ]
      addresses: [ 192.168.19.1/24 ]

    br105:
      dhcp4: no
      interfaces: [ enp1s0f0 ]
      addresses: [ 192.168.105.2/24 ]
      nameservers:
        addresses: [ 8.8.8.8 ]
      gateway4: 192.168.105.1

    br109:
      dhcp4: no
      interfaces: [ enp1s0f1 ]
      addresses: [ 192.168.109.2/24 ]
      routes:
        - to: 0.0.0.0/0
          from: 192.168.109.2
          via: 192.168.109.1
          on-link: true

Best Answer

While each subnet has a gateway you can only every have one default gateway, the way around this is actually quite simple - you create a 'static route'.

So you need to pick which subnet will be the default one and set that as the default gateway (DG), then all you then do is add the second IP address to the second adapter/VLAN and then simply add the static route.

So in the example again you have say NIC 1 as 192.168.15.whatever as the IP, 255.255.255.0 as the NM and 192.168.15.1 (or whatever as the DG, it has to be within the subnet by the way, which 192.168.105.1 isn't), then setup 192.168.19.whatever on NIC 2, same NM but this one doesn't get a DG. You then create the static route as anything in 192.168.19.0/24 must go through the IP you assigned to NIC 2, which is acting as a gateway, but not the default one, for that IP range and nothing else. Oh and 192.168.109.1 isn't in that range of course so it wrong too.

Hopefully this is clear enough but otherwise come back ok.