1

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.

2
  • Welcome to Unix & Linux! :-) Go static on both adapters or static on at least one of the adapters. Going DHCP on both is a bad idea as you found out.
    – Fabby
    May 9, 2019 at 14:10
  • Network Manager has a specific option to ignore the default route on DHCP. No idea about netplan. Just telling some people thought about this issue, even for DHCP
    – A.B
    Jun 3, 2019 at 0:43

4 Answers 4

2

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

1

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)

0
super@gitlab:~$ lsb_release -dc
Description:    Ubuntu 18.04.4 LTS
Codename:   bionic
super@gitlab:~$ cat /etc/netplan/50-cloud-init.yaml 
# This file is generated from information provided by
# the datasource.  Changes to it will not persist across an instance.
# To disable cloud-init's network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        ens18:
            addresses:
            - 172.16.104.177/24
            dhcp4: false
            gateway4: 172.16.104.1
            nameservers:
                addresses:
                - 172.16.104.1
                search: []
        ens19:
            dhcp4: true
            dhcp4-overrides:
                use-routes: false
            routes:
              - to: 10.0.0.0/16
                via: 10.0.90.1
    version: 2
super@gitlab:~$ ip r
default via 172.16.104.1 dev ens18 proto static 
10.0.0.0/16 via 10.0.90.1 dev ens19 proto static 
10.0.90.0/24 dev ens19 proto kernel scope link src 10.0.90.21 
172.16.104.0/24 dev ens18 proto kernel scope link src 172.16.104.177 
super@gitlab:~$
2
  • 1
    Welcome to Unix.SE! Could you edit your answer to explain what it does? I take it the significant piece is use-routes: false in the DHCP4 overrides... Sep 23, 2020 at 20:40
  • It seems that the dhcp4-overrides: use-routes: false is indeed the useful part. It worked for me in Ubuntu 20.04
    – wojtow
    Sep 6, 2021 at 22:21
0

dhcp4-overrides doesn't work for me on ubuntu 20.04.

Try to create separate files for different interfaces, for example,

For the first interface that you want the default route to be added, let's say eth0:

/etc/netplan/00-eth0.yaml

network:
  version: 2
  ethernets: 
    eth0: 
      dhcp4: true

Then for the other one you want to avoid the default route, eth1, create another file:

/etc/netplan/01-eth1.yaml

network:
  version: 2
  renderer: networkd
  ethernets: 
    eth1: 
      dhcp4: true

Then run sudo netplan apply and check your routes with ip r

It appears that using networkd as the renderer it won't create the default route for you.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .