2

UPDATE 2

Top posting because it is needed to apply the suggested solution.

Found on second answer to this question: Can netplan configured nameservers supersede (not merge with) the DHCP nameservers:

This has been fixed in the netplan repository on github, and will presumably make its way into Ubuntu at some point.

Two new options have been added, dhcp4-overrides and dhcp6-overrides. To ignore DNS servers from DHCP you would do something like

Anyone knows how to update netplan in Ubuntu 18.04?

Original question

I have an LXD vm with 2 network interfaces. If I use Ubuntu 16.04 (interfaces) I have 1 default gateway. If I use Ubuntu 18.04 (netplan) I have 2 default gateways. How can I get rid of the second gateway?

Ubuntu 16.04 configuration:

root@servizi:/etc/network/interfaces.d# ls -lha
total 3.5K
drwxr-xr-x 2 root root   4 Jul 31  2017 .
drwxr-xr-x 7 root root   8 Jul 12  2017 ..
-rw-r--r-- 1 root root 367 Jul 30  2017 50-cloud-init.cfg
-rw-r--r-- 1 root root  97 Jul 31  2017 60-locale.cfg
root@servizi:/etc/network/interfaces.d# cat 50-cloud-init.cfg 
# 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}
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet dhcp
root@servizi:/etc/network/interfaces.d# cat 60-locale.cfg 
auto eth1
iface eth1 inet dhcp
        up route add -net 192.168.99.0 netmask 255.255.255.0 gw 10.0.1.1
root@servizi:/etc/network/interfaces.d# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.220.90.1     0.0.0.0         UG    0      0        0 eth0
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.220.90.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
192.168.99.0    10.0.1.1        255.255.255.0   UG    0      0        0 eth1
root@servizi:/etc/network/interfaces.d# 

Ubuntu 18.04 configuration

root@servizi2:/etc/netplan# ls -lh
total 2.0K
-rw-r--r-- 1 root root 381 Jan 24 17:30 50-cloud-init.yaml
-rw-r--r-- 1 root root 177 Feb  1 20:25 60-locale.yaml
root@servizi2:/etc/netplan# cat 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:
    version: 2
    ethernets:
        eth0:
            dhcp4: true
root@servizi2:/etc/netplan# cat 60-locale.yaml 
# seconda scheda
network:
    version: 2
    ethernets:
        eth1:
            dhcp4: true
            routes:
            - to: 192.168.99.0/24
            via: 10.0.1.1

root@servizi2:/etc/netplan# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.220.90.1     0.0.0.0         UG    100    0        0 eth0
0.0.0.0         10.0.1.1        0.0.0.0         UG    100    0        0 eth1
10.0.1.0        0.0.0.0         255.255.255.0   U     0      0        0 eth1
10.0.1.1        0.0.0.0         255.255.255.255 UH    100    0        0 eth1
10.220.90.0     0.0.0.0         255.255.255.0   U     0      0        0 eth0
10.220.90.1     0.0.0.0         255.255.255.255 UH    100    0        0 eth0
root@servizi2:/etc/netplan#

UPDATE

Tried what @slangasek suggested and the second file is now:

root@servizi2:/etc/netplan# cat 60-locale.yaml 
# seconda scheda
network:
    version: 2
    ethernets:
        eth1:
            dhcp4: true
            dhcp4-overrides:
                    use-routes: false
            routes:
                - to: 192.168.99.0/24
                  via: 10.0.1.1

But netplan try gives the error "unknown key dhcp4-overrides"

root@servizi2:/etc/netplan# netplan try
Error in network definition /etc/netplan/60-locale.yaml line 5 column 12: unknown key dhcp4-overrides

An error occurred: the configuration could not be generated

Reverting.
root@servizi2:/etc/netplan#

Wha can I do?

3 Answers 3

6

It appears you have a default route on both eth0 and eth1 that is coming from dhcp; and you want to use only the default route through eth0.

To ignore the default route provided by your dhcp server, you would need to use dhcp4-overrides, like so:

network:
    version: 2
    ethernets:
        eth1:
            dhcp4: true
            dhcp4-overrides:
                use-routes: false
            routes:
                - to: 192.168.99.0/24
                  via: 10.0.1.1

This option is only supported in netplan 0.95 and later, which is currently available in Ubuntu 18.04 LTS and later releases.

6
  • Well, I want a default gateway on eth0 which is generated by cloud-init. On eth1 I want a static route to another lan that is not managed by LXD. Seems that adding this static route generates a default gateway (destination 0.0.0.0) instead the route to 192.168.99.0/24. Hope it is more clear Feb 4, 2019 at 8:18
  • Sorry, read your route table too quickly without scrolling to check interface names. I've fixed the interface names around in my answer, but the basic answer is the same - your default route appears to be coming from dhcp.
    – slangasek
    Feb 5, 2019 at 19:01
  • Sorry seems not to work. See question update Feb 6, 2019 at 11:04
  • 1
    As I said, 'dhcp4-overrides' is only supported in netplan 0.95, which is not yet available in any stable Ubuntu releases. In the meantime you should use static IP configuration for eth1 instead of dhcp.
    – slangasek
    Feb 6, 2019 at 22:32
  • Ubuntu server 18.04.3 has netplan 0.98 and the proposed solution with dhcp4-overrides: use-routes: false to avoid multiple default routes from multiple dhcp configured interfaces works just fine, thanks
    – fermar
    Oct 16, 2019 at 19:00
0
cat /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}

and

cat /etc/netplan/50-cloud-init.yaml
network:
    ethernets:
        ens3:
            dhcp4: true
            dhcp4-overrides:
                use-routes: false
            gateway4: 10.2.100.101
            match:
                macaddress: 52:54:00:0e:ad:cc
            set-name: ens3
    version: 2
0
0

In case you have to define second default gateway for the second NIC you can use this example:

# networks
network_1 ens3: 10.0.10.0/24
network_2 ens8: 10.0.20.0/24
 
# /etc/netplan/50-cloud-init.yaml
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: yes
    ens8:
      dhcp4: yes
      dhcp4-overrides:
        use-routes: false
      routes:
        - to: default
          via: 10.0.20.1
          table: 200
        - to: 10.0.20.0/24
          via: 10.0.20.1
          table: 200
      routing-policy:
        - from: 10.0.20.0/24
          table: 200

see also: https://netplan.readthedocs.io/en/latest/examples/#how-to-configure-source-routing

You must log in to answer this question.

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