Netplan – Set Default Route with Netplan on Ubuntu 18.04 Server

18.04dhcpnetplanserver

I have two NIC, both is controled over DHCP. One have public IP, second private.

Both interfaces have static IP reserved in DHCP and both interfaces get right IP, but somethimes when reboot server I can't access from public, because default route is from private NIC.

How can I set permanently this with netplan ?

Public NIC ens18 (IP: 213.133.xxx.xxx)
Private NIC ens19 (IP: 10.10.10.xxx)

My netplan config is:

network:
  version: 2
  renderer: networkd
  ethernets:
    ens18:
      dhcp4: yes
      dhcp6: no
      nameservers:
        addresses: [8.8.8.8,8.8.4.4]
  ethernets:
    ens19:
      dhcp4: yes
      dhcp6: no

Best Answer

The issue is that networkd will bring up both networks, and both will have a default gateway set, and both will be at the same metric.

Netplan does not currently allow you to skip setting the route on one interface, but you can configure networkd separately to tell it to do this, by basing the config on what netplan has already generated.

I have copied the commands below. Here I am assuming that ens19 is the "secondary" interface for which you do not want a default gateway set -- note that to do this successfully, it also needs to happen before rebooting with the new interface (or you can copy part of the config, omit the MACAddress= line, etc. so that it's generic enough that a new interface will be matched).

sudo cp /run/systemd/network/10-netplan-ens19.network /etc/systemd/network
sudo vi /etc/systemd/network/10-netplan-ens19.network

Then add under [DHCP]:

UseRoutes=false            # if you don't want to apply any routes from DHCP

RouteMetric=200        # any number above 100 if you want the routes applied, but that they are less preferred.

If you don't have the file yet (ie. you have not attached the interface yet) then you could copy the contents of another interface set for DHCP, and remove MACAddress=.

In general, the file should look something like this:

[Match]
Name=interfacename

[Network]
DHCP=ipv4

[DHCP]
UseMTU=true
RouteMetric=200    # or UseRoutes=false, as you prefer.
Related Question