Software & AppsOperating SystemLinux

How To Change Network Interface Priority in Ubuntu

Ubuntu 18

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.

Quick Answer

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 iptables and ip commands.

Method 1: Using the ifmetric tool

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 ifmetric

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 Iface and 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 /etc/network/interfaces file

Another way to change the network interface priority is by modifying the /etc/network/interfaces file.

Step 1: Open the /etc/network/interfaces file

Open the /etc/network/interfaces file using a text editor.

Step 2: Modify the Interface Configuration

Locate the interface configuration section for new0 and new1. Add the metric option to the interface configuration. For example:

auto new0
iface new0 inet static
 address xxx.xxx.xxx.xxx
 netmask xxx.xxx.xxx.xxx
 metric 10

auto new1
iface new1 inet static
 address yyy.yyy.yyy.yyy
 netmask yyy.yyy.yyy.yyy
 metric 5

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 iptables and ip commands to route packets based on their destination port.

Step 1: Mark Packets with iptables

Use the 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:

100 sshtable

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.

Can I change the network interface priority in Ubuntu without installing any additional tools?

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.

How can I check the current metric value of a network interface?

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 Metric column.

Can I prioritize a network interface based on its speed?

No, the network interface priority is determined by the metric value, not the speed of the interface.

Is it possible to change the network interface priority dynamically?

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.

Will changing the network interface priority affect my internet connection?

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.

Leave a Comment

Your email address will not be published. Required fields are marked *