I have 3 network interfaces: lo, new0 and new1. I use new1 to access the internet.

My computer went nuts a few days ago and began to try to access the internet on lo. Doing ifconfig lo down causes packets to go via new0. But I want it to use new1.

I can use ssh, download torrents and use other protocols, but http and https are not working; I just get instant "Server not found" without loading time in browser.

How can have the system prefer new1 over new0 and lo?

I'm not able to change my metric, both new1 and new0 are stuck at 1.


Prioritising interfaces for general traffic is done by manipulating the routing metrics.

Each route has associated parameters such as hop-counts and bandwidths. See netstat -nr and the "metric" option in the man-page for route command.

For very simple control of metric use ifmetric tool:

sudo apt-get update

sudo apt-get install ifmetric

Once installed use it to change metric value for interface, refer this http://manpages.ubuntu.com/manpages/vivid/man8/ifmetric.8.html

| improve this answer | |
  • This worked for on Ubuntu 16.04, but it took me some time to figure out exact commands. The first thing to do is finding interface you want to update and metric value to set. To do so run route command and note Iface and Metric column values. (netstat doesn't show metric value, metric from ifconfig is not what you are looking for). To de-prioritize interface (e.g. ens33) you need, pick metric value a bit larger than other interfaces. Let's say it is 110. Now run ifmetric as follows: sudo ifmetric ens33 110 – Anton Matosov Feb 20 '18 at 20:47

Metric can change on several place. Best working for me is configuration in etc/network/interface

auto lo
iface lo inet loopback
auto new0
iface new0 inet static
    address xxx.xxx.xxx.xxx
    netmask xxx.xxx.xxx.xxx
    up route add default gw aaa.aaa.aaa.aaa metric 10
    down route del default gw aaa.aaa.aaa.aaa

auto new1
iface new1 inet static
    address yyy.yyy.yyy.yyy
    netmask yyy.yyy.yyy.yyy
    up route add default gw bbb.bbb.bbb.bbb
    down route del default gw bbb.bbb.bbb.bbb

On this way when interface new0 go up will bring route to gateway aaa.aaa.aaa.aaa with metric 10. Interface new1 bring up route to bbb.bbb.bbb.bbb with lower metric and traffic will be routed on that interface.

If you do sudo ifdown new1 on unplug cable from card route will be deleted and route to aaa.aaa.aaa.aaa will be new best route for traffic.

When again brin int new1 up route will again be added and bbb.bbb.bbb.bbb will again be your best route.

Edit 1

Route priority is not the same like route based on destination port.

But I give you example to route traffic for ssh, port 22 and you can implement for port you need for

Let’s start from the iptable. Here we shall take ssh as the service for routing. Use mangle table of iptable for modifying the ssh packets.

  sudo iptables -t mangle -A OUTPUT -p tcp –dport 22 -j MARK –set-mark 0×1

We are marking all packets with destination port 22 as ’0×1′ .

Now save and restart iptables.

service iptables save
service iptables restart

To delete this entry from iptables we can use -D instead of -A.

Next, create a new IP route table in /etc/iproute2/rt_table by just giving an entry

100 sshtable

Write rule for ssh packets.

ip rule add fwmark 0×1 lookup sshtable

Add route at new table sshtable. Here we shall use ’′ as the gateway for ssh. Address ’′, in this example will be gateway address for new2 All other traffic will go through the default gateway aka new1, which can be seen by IP route show command.

We copy all entries except default gateway entry from main table.

 sudo ip route show table main | grep -Ev ^default | while read ROUTE ; do ip route add table sshtable $ROUTE; done

Add default gateway entry for ssh packets to table sshtable

  sudo ip route add default via table sshtable

Use ip route show table sshtable to show all routes at sshtable.

That’s it..we have done it!!!

Repeat everything for imap on port 465 or hhtp on port 80 or https on port 443 ...

| improve this answer | |
  • Thanks, but I'm not sure my problem is that simple. Strangely i can access ssh and other protocols. (but NOT http or https) Is there some "special" protocol routing ? – bob dylan Aug 14 '15 at 1:44
  • You have My Edit 1 in answer for routing based on destination port. – 2707974 Aug 14 '15 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.