In this article, we will delve into the process of changing network interface priority in Ubuntu. This can be particularly useful when you want to prioritize one network connection over another. We will explore three methods to accomplish this: using the
ifmetric tool, modifying the
/etc/network/interfaces file, and routing based on destination port.
To change network interface priority in Ubuntu, you can use the
ifmetric tool to modify the metric value of the interface, or modify the
/etc/network/interfaces file by adding the
metric option to the interface configuration. Another method involves routing based on destination port using
- Method 1: Using the ifmetric tool
- Method 2: Modifying the /etc/network/interfaces file
- Method 3: Routing based on destination port
Method 1: Using the
ifmetric tool is a simple utility that allows you to change the metric of a device. The metric value is used to determine the order of preference for routes â€” the lower the value, the higher the priority.
Step 1: Install
First, you need to install the
ifmetric tool. You can do this by running the following commands:
sudo apt-get update
sudo apt-get install ifmetric
Step 2: Check the Current Metric Value
Next, find the interface you want to update and note its metric value. You can use the
route command to view the metrics. For example, run
route and note the
Metric column values.
Step 3: Change the Metric Value
To de-prioritize the interface (e.g.,
ens33), choose a metric value slightly larger than the other interfaces. Let’s say the metric value is 110. Run the
ifmetric command as follows to change the metric value for the interface:
sudo ifmetric ens33 110
Method 2: Modifying the
Another way to change the network interface priority is by modifying the
Step 1: Open the
/etc/network/interfaces file using a text editor.
Step 2: Modify the Interface Configuration
Locate the interface configuration section for
new1. Add the
metric option to the interface configuration. For example:
iface new0 inet static
iface new1 inet static
In the above example,
new1 will have a higher priority over
new0 because it has a lower metric value.
Step 3: Restart the Network Service
Save the file and restart the network service for the changes to take effect.
Method 3: Routing based on destination port
The third method involves using
ip commands to route packets based on their destination port.
Step 1: Mark Packets with
iptables command to mark packets with a specific destination port. For example, to mark SSH packets with destination port 22, run:
sudo iptables -t mangle -A OUTPUT -p tcp --dport 22 -j MARK --set-mark 0x1
Step 2: Create a New IP Route Table
Create a new IP route table in the
/etc/iproute2/rt_table file by adding an entry. For example:
Step 3: Add a Rule to Route Marked Packets
Add a rule to route marked packets to the new table. For example:
sudo ip rule add fwmark 0x1 lookup sshtable
Step 4: Copy Entries from Main Table
Copy all entries from the main table to the new table, excluding the default gateway entry. For example:
sudo ip route show table main | grep -Ev ^default | while read ROUTE ; do ip route add table sshtable $ROUTE; done
Step 5: Add a Default Gateway Entry
Add a default gateway entry for the marked packets to the new table. For example:
sudo ip route add default via 192.168.1.1 table sshtable
Step 6: Verify the Routes
Verify the routes using the
ip route show table sshtable command.
By following these methods, you can effectively change the network interface priority in Ubuntu. This will help you to prioritize one network connection over another, ensuring that your system uses the preferred interface for internet access.
Yes, you can change the network interface priority in Ubuntu by modifying the
/etc/network/interfaces file. No additional tools are required for this method.
You can use the
route command to check the current metric value of a network interface. Run
route in the terminal and look for the
No, the network interface priority is determined by the metric value, not the speed of the interface.
Yes, you can change the network interface priority dynamically by using the
ifmetric tool. Simply run the
ifmetric command with the desired metric value for the interface you want to prioritize.
Changing the network interface priority will affect the routing of network traffic. It may impact which network connection is used for internet access, depending on the routing configuration.