linux

Add static routes to Ubuntu under Netplan

If you’re on the 10.40.24.0 network, the route target would be 10.40.24.250. If you’re on the 10.40.28.0 network, it’s 10.40.28.250, and so on. The 10.40.28.0 network is used in this experiment.

 

  • Edit the /etc/netplan/01-netcfg.yaml file.

Here is an example from skayal.com.

network:

version: 2

renderer: networkd

ethernets:

eth0:

addresses: [ 10.40.28.108/24 ]

gateway4: 10.40.28.1

nameservers:

search: [ skayal.com ]

addresses:

- 208.82.128.10

- 208.82.128.2

routes:

- to: 10.10.0.0/16

via: 10.40.28.250

- to: 10.20.0.0/16

via: 10.40.28.250

 

  • Save your changesnetplan tryYou should see the routes in the kernel routing table:


    # netstat -nr

    Kernel IP routing table

Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface

0.0.0.0         10.40.28.1      0.0.0.0         UG        0 0          0 eth0

10.10.0.0       10.40.28.250    255.255.0.0     UG        0 0          0 eth0

10.20.0.0       10.40.28.250    255.255.0.0     UG        0 0          0 eth0

10.40.28.0      0.0.0.0         255.255.255.0   U         0 0          0 eth0
3. Now try a ping test either direction.

$ ping 10.40.28.108

PING 10.40.28.108 (10.40.28.108): 56 data bytes

64 bytes from 10.40.28.108: icmp_seq=0 ttl=60 time=27.549 ms

64 bytes from 10.40.28.108: icmp_seq=1 ttl=60 time=27.277 ms

64 bytes from 10.40.28.108: icmp_seq=2 ttl=60 time=27.571 ms

64 bytes from 10.40.28.108: icmp_seq=3 ttl=60 time=27.597 ms

64 bytes from 10.40.28.108: icmp_seq=4 ttl=60 time=29.772 ms

64 bytes from 10.40.28.108: icmp_seq=5 ttl=60 time=26.666 ms

64 bytes from 10.40.28.108: icmp_seq=6 ttl=60 time=29.389 ms

64 bytes from 10.40.28.108: icmp_seq=7 ttl=60 time=27.083 ms

64 bytes from 10.40.28.108: icmp_seq=8 ttl=60 time=30.078 ms

^C

--- 10.40.28.108 ping statistics ---

9 packets transmitted, 9 packets received, 0.0% packet loss

round-trip min/avg/max/stddev = 26.666/28.109/30.078/1.201 ms