16

My problem is quite similar to this question. My vagrant machine sets default route metric incorrectly.

The following is netplan configuration.

network:
  version: 2
  ethernets:
    enp0s3:
      dhcp4: true
      match:
        macaddress: 02:70:4e:c8:68:e9
    enp0s8:
      dhcp4: false
      addresses: [192.168.33.10/24]
      gateway4: 192.168.33.1
      routes:
        - to: 192.168.33.0/24
          via: 192.168.33.1
          metric: 0

And route table.

# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.33.1    0.0.0.0         UG    0      0        0 enp0s8
0.0.0.0         10.0.2.2        0.0.0.0         UG    100    0        0 enp0s3
10.0.2.0        0.0.0.0         255.255.255.0   U     0      0        0 enp0s3
10.0.2.2        0.0.0.0         255.255.255.255 UH    100    0        0 enp0s3
192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 enp0s8
192.168.33.0    192.168.33.1    255.255.255.0   UG    0      0        0 enp0s8

enp0s3 is NAT interface and internet connection should use this. sudo route del default enp0s8 resolves this problem, but adding routes to yaml couldn't solve the problem and has the limit of fixed gateway.

What configuration should I use to reorder metrics automatically?

1 Answer 1

24

For systems with multiple interfaces, only the interface that should get the default gateway should have "gateway4:" set; keeping in mind that DHCP is quite likely to set the default gateway as well:

network:
  version: 2
  ethernets:
    enp0s3:
      dhcp4: true
      match:
        macaddress: 02:70:4e:c8:68:e9
    enp0s8:
      dhcp4: false
      addresses: [192.168.33.10/24]

Note that the additional route you had set is typically unneeded: 192.168.33.0/24 is already reachable, as it is directly connected to the enp0s8 interface in this case (when addresses: lists the subnet already). Having a metric: set to 0 can further confuse the kernel.

If you need the interface with static IP (in this case enp0s8 to have the default gateway instead of the interface that does DHCP, then you can set a route with a metric value lower than 100; so that it is picked before the default gateway set via DHCP by systemd-networkd. For example:

network:
  version: 2
  ethernets:
    enp0s3:
      dhcp4: true
      match:
        macaddress: 02:70:4e:c8:68:e9
    enp0s8:
      dhcp4: false
      addresses: [192.168.33.10/24]
      routes:
        - to: 0.0.0.0/0
          via: 192.168.33.1
          metric: 50
2
  • 7
    Additionally it is possible to change metrics weight with dhcp4-overrides: {route-metric: 100}, ref
    – Viacheslav
    Commented Jun 12, 2019 at 10:40
  • 5 years later and still useful...I was searching for a solution to my problem for 2 hours until I found this thank you so much
    – NegassaB
    Commented Apr 28, 2023 at 12:14

You must log in to answer this question.

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