1

The high-level overview is this: I have an Impish Ubuntu server with two interfaces, addresses of 172.16.2.103/24 and 10.1.2.10/24. I would like default traffic to go via the gateway 172.16.2.254. However, when I specify a source address of 10.1.2.10 I want it to have a gateway of 10.1.2.254. What follows works 99% of the time, but I wish for the kernel to select the source IP 172.16.2.103 even when contacting 10.1.2.0/24. This was possible in /etc/networks/interfaces but I haven't been able to figure it out using netplan.

This is what I have in my netplan config:

network:
  ethernets:
    ens160:
      addresses:
      - 172.16.2.103/24
      routes:
      - to: 0.0.0.0/0
        via: 172.16.2.254
      nameservers:
        addresses:
        - 10.1.2.1
        search:
        - localdomain
      optional: yes
    ens192:
      addresses:
      - 10.1.2.10/24
      routing-policy:
      - from: 10.1.2.10
        table: 10
      routes:
      - to: 0.0.0.0/0
        via: 10.1.2.254
        table: 10
      optional: yes
  version: 2

As I say this works fine mostly. I have a routing-policy (aka ip rule) which means that if I change the source address of packets I look up the 10 routing table. However, there is an additional route I wish to remove from main.

This is what I have when I check out the routing tables:

IN1:  me@host:~$ ip route
OUT1: default via 172.16.2.254 dev ens160 proto static
OUT2: 172.16.2.0/24 dev ens160 proto kernel scope link src 172.16.2.103
OUT3: 10.1.2.0/24 dev ens192 proto kernel scope link src 10.1.2.10

IN2:  me@host:~$ ip route list table 10
OUT5: default via 10.1.2.254 dev ens192 proto static

I would like the OUT3 removed, or more accurately moved into the 10 routing table, so that there is no layer2 link to 10.1.2.0/24 in the main routing table. I can do it manually thus:

me@host:~$ sudo ip route del 10.1.2.0/24 dev ens192

But I cannot see how to embed that in netplan. Previously I would have added an up statement to /etc/network/interfaces but that's not an option any more.

Thanks in advance.

1 Answers1

0

I wouldn't call this a solution, more a workaround. My problem was that I wanted to remove an unwanted route: the workaround is to create another route with a higher precedence!

network:
  ethernets:
    ens160:
      addresses:
      - 172.16.2.103/24
      routes:
      - to: 0.0.0.0/0
        via: 172.16.2.254
      # ** ADDITIONAL ROUTE **
      - to: 0.0.0.0/0
        via: 172.16.2.254
        table: 172
      # **********************
      nameservers:
        addresses:
        - 10.1.2.1
        search:
        - localdomain
      optional: yes
    ens192:
      addresses:
      - 10.1.2.10/24
      routing-policy:
      - from: 10.1.2.10
        table: 10
      # ** ADDITIONAL ROUTING POLICY **
      - to: 172.16.2.0/24
        table: 172
      # *******************************
      routes:
      - to: 0.0.0.0/0
        via: 10.1.2.254
        table: 10
      optional: yes
  version: 2

Note the additional routing-policy, and route for the ens160 interface. This will create another routing table called 172, and this has one default route the same as the main table's default route (172.16.2.254). Now when I route to 172.16.2.0/24, instead of going via the main table with the wrong route, it will instead go via the 172 routing table.

I'd still appreciate to know if there's a way of deleting the route in the main table, but I now have a functioning system so in 2 weeks I will mark this as the accepted solution.