Set USB Ethernet Interface Connection Priority in Ubuntu
In some situations, you might need to connect your Ubuntu machine to the internet using a USB Ethernet adapter and want this connection to have a higher priority than other network interfaces during the boot process. This article will discuss key concepts, steps, and commands for achieving this goal.
Understanding Network Interfaces and ifmetric
In Linux, network interfaces come in various forms, such as wired Ethernet (e.g., enp2s0), wireless (e.g., wlp3s0), and USB Ethernet devices. Each interface can be assigned a priority level that determines in which order they are used for network traffic.
To set connection priorities in Ubuntu, you will use the ifmetric command. This command allows you to configure metrics or interface priority levels. The lower the metric, the higher the priority for network traffic.
Identifying Network Interfaces
Before configuring interface priorities, it is vital to identify the USB Ethernet device and its current IP address. You can achieve this using the following commands:
$ ip link show # Display network interfaces and their current state
$ ip addr show # Show IP addresses assigned to network interfaces
After running these commands, find the USB Ethernet interface (likely prefixed with "usb" or "eth") and note down its name and IP address.
Setting USB Ethernet Interface Priority
Now it's time to configure the desired priority level for the USB Ethernet interface. To achieve this, use the ifmetric command:
$ sudo ifmetric usb0 100 # Replace "usb0" with your USB Ethernet interface name
This command configures a metric value of 100 for the USB Ethernet interface, meaning it has a higher priority than other interfaces that have higher metric values assigned.
Persisting USB Ethernet Interface Priority
By default, the interface priority is reset during a system reboot. To ensure the priority stays in place across reboots, create or modify a configuration file based on your Ubuntu version:
-
Ubuntu 16.04 or older:
$ sudo nano /etc/network/interfaces.d/usb-ethernet
Add the following lines:
auto usb0 # Replace "usb0" with your USB Ethernet interface name iface usb0 inet dhcp up ifmetric usb0 100
-
Ubuntu 18.04 and later:
$ sudo nano /etc/netplan/
.yaml Modify the interface block with the following lines:
network: version: 2 ethernets: usb0: # Replace "usb0" with your USB Ethernet interface name dhcp4: true metrics: - 100
In both cases, replace "/etc/netplan/.
Applying Changes
Once the changes are in place, update network configuration settings:
$ sudo netplan apply
- Use the
ifmetriccommand to set connection priorities. - Identify USB Ethernet interfaces using
ip link showandip addr show. - Create or modify configuration files based on your Ubuntu version to persist priority changes.