Ubuntu 18.04 netplan.io how to prevent the creation of 2 default gateways

6,881

Just remove the gateway configuration from the eth0 interface.

Omit the line which says gateway4: 172.16.0.1.

You will still be able to reach hosts in the 172.16.x.y subnet through eth0, since it's directly connected there.

If you need to access other hosts in that network (for example, an intranet with private addresses in the RFC1918 ranges), then just add static routes to access those networks using the 172.16.0.1 gateway. See this reference for routing in Netplan, which should help you add static routes.

If you get your IPs through DHCP, it should be mostly the same... But then you need the DHCP server that hands you an IP on eth0 not to set any gateways... There doesn't seem to be a good way in Netplan to disable a gateway you receive from DHCP. (Though networkd can do that, see this comment on another answer for an attempt to address that.)

Share:
6,881

Related videos on Youtube

filbranden
Author by

filbranden

Linux Plumber 👨‍🔧 Vim enthusiast and advocate. I'm part of the core team and a frequent contributor to the systemd project.

Updated on September 18, 2022

Comments

  • filbranden
    filbranden over 1 year

    When I add 2 interfaces (eth0 LAN, eth1 WAN) with static IPs (or maybe also with DHCP), Ubuntu 18.04 Server is adding 2 default gateways. Thats not the goal.

    How can I define only 1 default gateway?

    File: /etc/netplan/01-netcfg.yaml

    network:
      version: 2
      renderer: networkd
      ethernets:
        eth0:
          addresses:
            - 172.16.1.22/16
          gateway4: 172.16.0.1
          nameservers:
            search: [mydomain.internal]
            addresses: [172.16.1.2, 172.16.1.1]
        eth1:
          addresses:
          - 172.19.3.235/24
          gateway4: 172.19.3.1
          nameservers:
            addresses: [195.50.xxx.xxx,195.50.xxx.xxx]
    

    ip route shows:

    user@server:~$ ip route
    default via 172.19.3.1 dev eth1 proto static
    default via 172.16.0.1 dev eth0 proto static
    172.16.0.0/16 dev eth0 proto kernel scope link src 172.16.1.22
    172.19.3.0/24 dev eth1 proto kernel scope link src 172.19.3.134
    

    2 default GWs......

    In my case, the default GW from eth1 is the one and only I need.