I have running an Ubuntu 18.04 with 2 ethernet NICs. On both NICs I'm using DHCP. How can I prevent netplan from creating a default route to 0.0.0.0/0 on both devices? I want one NIC routing to 0.0.0.0/0 and the other one in a specific /24 subnet via a gateway. I know that I can add a route in the netplan config but it will always create the default route to 0.0.0.0/0.
For more control of your NICs, go static on both adapters or static on at least one of the adapters.
This can be easily done in that specific range by reserving both IPs for their respective MAC addresses in their respective DHCP servers and then you will not have any of the limitations of DHCP and configure everything or part manually.
(E.G. If your corporate policy would require you to use a totally different IP range if using static, you can still use the aforementioned hybrid technology)
According to this answer, it should be possible with
dhcp4-overrides, however that is not supported in Ubuntu as of 18.04. My workaround was to create a hook script in
/etc/networkd-dispatcher/routable.d/ to remove the default route:
#!/bin/sh # Only remove the default route on the second interface, e.g. eth1 [ "$IFACE" != eth1 ] && exit 0 # delete the default route for this interface ip route del default dev eth1
Make sure the file owner and group are
root and that it is executable.
You can find out more about networkd-dispatcher here: https://gitlab.com/craftyguy/networkd-dispatcher