How to use netplan to create two separate routing tables?

I have an Ubuntu 18.04 host with two network interfaces on two subnets. I would like to set up symmetric routing so that traffic received from one interface is sent symmetrically out that same interface.

I know how to use Netplan for simple network configurations, but I'm stumped for more advanced configurations. Specifically:

  1. What is the Netplan syntax to add a default route like I do with ip route add default via 192.168.0.1 dev ens192 tab 1?
  2. How can I add a priority tag to a route?

My server has two IPs:

$ ip a|grep "inet "
inet 127.0.0.1/8 scope host lo
inet 192.168.0.10/22 brd 192.168.0.255 scope global ens192
inet 192.168.1.10/24 brd 192.168.1.255 scope global ens224
$

I can use the following ip rules to get the tables that I want:

First, I create a route for each network and then add a default gateway.

$ ip route add 192.168.0.0/24 dev ens192 tab 1
$ ip route add 192.168.1.0/24 dev ens224 tab 2
$ ip route add default via 192.168.0.1 dev ens192 tab 1
$ ip route add default via 192.168.1.1 dev ens224 tab 2

Then, I can create corresponding rules:

$ ip rule add from 192.168.0.10/32 tab 1 priority 100
$ ip rule add from 192.168.1.10/32 tab 2 priority 200
$ ip route flush cache

This gets the routes that I want:

$ ip route show tab 1
default via 192.168.0.1 dev ens192
$ ip route show tab 2
default via 192.168.1.1 dev ens224
$ ip route
default via 192.168.0.10 dev ens192

As well as the rules that I want:

$ ip rule show
0: from all lookup local
100: from 192.168.0.10 lookup 1
200: from 192.168.1.10 lookup 2
32766: from all lookup main
32767: from all lookup default

2 Answers

 ens2f1: addresses: - x.x.x.x/x routes: - to: 0.0.0.0/0 via: y.y.y.y/y table: 200 routing-policy: - from: z.z.z.z/z table: 200 priority:

Hope this answers both of your questions

0

When you specify routes: and then you specify table: for those routes, you've effectively created a routing table that can be referenced elsewhere in your config. You can make source based routing for a given interface by adding routing-policy such that from: that interface's IP, use table: table you defined earlier in routes. None of these configurations made any sense to me until I figured this out just now.

ip route add default via 192.168.0.1 dev ens192 tab 1 would correspond to

ens192: addresses: [foo.bar] routes: - to: default via: 192.168.0.1 table: 1 routing-policy: - from: foo.bar table: 1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like