6

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

3 Answers 3

4
    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
3

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

2

Both provided answers have something wrong. First does not exactly correspond to the question asked, and may cause confusion for others. The second fails to note that the full corresponding shell commands to match the netplan config provided would include:

ip rule add from foo.bar table 1

So for the sake of completeness here is a netplan config, that achieves OPs required goals according to OP's provided data and leaves out anything unnecessary:

   ens192:
        addresses:
        - 192.168.0.10/22
        routes:
        - to: default
          via: 192.168.0.1
   ens224:
        addresses:
        - 192.168.1.10/24
        routes:
        - to: default
          via: 192.168.1.1
          table: 2
        routing-policy:
        - from: 192.168.1.10
          table: 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.