Set USB Ethernet priority in Ubuntu by editing the Netplan configuration file to assign a lower metric value to the desired interface. This ensures the system routes traffic through the USB adapter over other connections like Wi-Fi or internal Ethernet. The change requires applying the configuration and verifying the active route.
Understanding Network Metrics
In Linux networking, the kernel uses a routing table to determine which network interface handles outgoing traffic when multiple connections are active. Each route has a "metric," which acts as a cost indicator. The route with the lowest numeric metric is preferred. By default, interfaces often receive similar metrics, leading to unpredictable behavior where the system might switch between Wi-Fi and wired connections arbitrarily. To prioritize a specific USB Ethernet interface, you must manually adjust this metric in your Netplan configuration. Lower numbers indicate higher priority. For example, setting a metric of 100 for the USB adapter and 200 for Wi-Fi forces the OS to use the USB connection for general internet access unless it fails. This method is robust because it works at the kernel level, independent of desktop environment settings.Identifying Your Interface Names
Before modifying any configuration files, you need to know the exact name of your USB Ethernet adapter. Modern Linux systems use predictable naming conventions, but USB adapters can sometimes have dynamic names depending on the port used. Open a terminal and run the following command to list all network interfaces along with their current status:ip link show
Look for an interface that corresponds to your USB dongle. It might be named something like enx78e5d9ea46f1 (based on the MAC address) or usb0. You can also check the output of nmcli device status if you are using NetworkManager alongside Netplan. Note the exact interface name, as you will need to replace placeholder names in the configuration steps below.
Configuring Netplan for Priority
Ubuntu uses Netplan to generate network configurations. The configuration files are typically located in the/etc/netplan/ directory. There is usually one YAML file there, such as 00-installer-config.yaml or 50-cloud-init.yaml. Open this file with a text editor using sudo privileges:
sudo nano /etc/netplan/*.yaml
The structure of a Netplan config involves defining each network interface under the network key. You need to locate the section for your USB Ethernet interface and add or modify the metrics parameter. If the interface is not yet defined, you may need to add it entirely.
Here is an example structure where we prioritize the USB interface named enp0s20u1:
network:
version: 2
ethernets:
enx78e5d9ea46f1: # Replace with your actual USB interface name
dhcp4: true
metrics: 100
wlp2s0: # Example Wi-Fi interface
dhcp4: true
metrics: 200
In this example, the USB interface has a metric of 100, while the Wi-Fi interface has 200. Since 100 is lower, the USB connection will be the primary route. Ensure that indentation is correct; YAML is sensitive to whitespace. Use spaces, not tabs.
If you are connecting via DHCP, the dhcp4: true line remains essential. The metrics line is added directly under the interface definition. Do not add it inside the dhcp4 block.
Alternative: Using Static IP
Some users prefer static IP addresses for critical servers. If you are using a static configuration, the process is identical. Locate theaddresses block for the interface and add the metrics field at the same level as addresses and gateway4 (if applicable).
enp0s20u1:
addresses: [192.168.1.10/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
metrics: 100
This approach ensures that even if the DHCP server assigns different parameters, your local priority rules remain intact.
Applying and Verifying Changes
Once you have saved the Netplan configuration file, you must apply the changes. Run the following command to validate the syntax and apply the new routing rules:sudo netplan apply
If there are syntax errors in your YAML file, this command will report them. Fix any issues and try again. After successful application, verify that the USB interface is indeed the primary route. Use the ip route command to view the routing table:
ip route show
Look for the default route (default via ...). The interface listed next to the preferred default route should match your USB adapter. You can also check the metric associated with the route:
ip route show | grep default
The output will show the metric value. If you see metric 100 for the USB interface and metric 200 for others, your configuration is working correctly.
Testing Connectivity
To ensure the system is actually using the USB connection, try pinging an external host like Google DNS:ping 8.8.8.8
While ping does not explicitly show the interface, you can monitor the interface statistics with ip -s link while downloading data. The USB interface should show increasing byte counts if it is handling the traffic.
For more details on managing network interfaces, you might find our guide on Preventing Ubuntu from Trying to Autoconnect to New Wi-Fi Networks helpful for further control over connection preferences. Additionally, understanding how IPv6 interacts with these settings can prevent unexpected routing loops, as discussed in Interface-Specific IPv6 Forwarding Configuration Effect on Ubuntu 22.04.
Frequently Asked Questions
What happens if two interfaces have the same metric?
If multiple interfaces share the same metric, the kernel uses a tie-breaking algorithm based on interface index or administrative distance. This can lead to unstable routing where traffic jumps between interfaces. Always ensure your preferred interface has a strictly lower metric than secondary connections.Can I set the metric dynamically after boot?
Yes, you can change the metric of an existing route using theip route change command. However, this change will revert after a reboot unless you update the Netplan configuration file. For persistent changes, always edit the YAML files in /etc/netplan/.
Does this work on older Ubuntu versions without Netplan?
Netplan was introduced in Ubuntu 17.10. If you are running a very old LTS version like 16.04, you likely useifupdown or NetworkManager directly. In those cases, you would configure metrics in /etc/network/interfaces or via NetworkManager's GUI settings, not Netplan.
Will changing the metric break my internet connection?
No, changing the metric only affects which interface is chosen when multiple are active. It does not disable the interface or alter its IP configuration. If the USB adapter is disconnected, the system will automatically failover to the next available route with the next lowest metric.How do I reset to default metrics?
To remove custom metrics, simply delete themetrics: line from your Netplan YAML file and run sudo netplan apply. The system will then assign default metrics, which are typically determined by the interface type and driver.