Unlike CentOS, ubuntu doesn’t provide a nice tool to set default routes when there are more than 1 ethernet interfaces are connected at the same time. It is possible to set up a static route with post-up but it is still difficult to do if the DHCP range is dynamic and you can’t accurately determine the proper gateway.
I was able to reliably implement it with the following settings.
Open /etc/network/interfaces file and
Add the following line to all other interfaces that does NOT act as a default gateway.
post-up route del default dev $IFACE
This code, instead of setting a default route, basically removes undesired default routes as the device goes up.
Example configuration when you want to use ens160 as default route:
auto ens172 iface ens172 inet dhcp post-up route del default dev $IFACE auto ens192 iface ens192 inet dhcp post-up route del default dev $IFACE # The primary network interface auto ens160 iface ens160 inet dhcp