I have a router (running Linux) that is connected to two ISPs, the routes are selected using BGP, however, I would like to ping/trace each connection separately.

The standard traceroute command seems to be able to do it (allow selection of outgoing interface, source IP), but it does not allow selection of the gateway. While there is a command line option (-g), all it does is set the IP routing option instead of just sending the packet there.

Here's what I want: Let's say My router has two IPs - (given by first ISP), (given by second ISP). The default gateway (next hop) on the first ISP is and on the second is One of them is selected as the current default gateway (by BGP), but I also want to be able to use trace/ping trough the other gateway. I cannot change the routing table (without causing connection interruptions, especially if the other ISP has a problem).

traceroute_oth --nexthop should give

2 some-iother-ip.first-isp.com

traceroute_oth --nexthop should give

2 some-ip.second-isp.com

EDIT: "traceroute -i" does not work because the router does not know the other gateway. Basically, the routing table is this (simplified): gw eth0

There is no way for the program to know what the default gateway on eth1 (link to the other ISP) is without me explicitly specifying it.


You did read the man page, right?

   -i interface, --interface=interface
          Specifies  the  interface  through  which traceroute should send
          packets. By default, the interface is selected according to  the
          routing table.
  • 1
    And it goes nowhere. The default gateway is still the gw of the other ISP so traceroute to nexthop shows OK, to anything else - nothing. Besides, how would traceroute know that the default gateway on eth1 is if it is not written anywhere? Routing table only shows default gw link eth0.
    – Pentium100
    Jul 15 '13 at 8:00
  • How do you know that he has 2 interfaces? He wants to use a different router, not a different interface.
    – Navin
    Jun 14 '16 at 6:11
  • @Navin I know because it was explicitly stated in the original question! Jun 14 '16 at 9:48

Posting this in case someone runs into the same problem I did:

On ubuntu, the inetutils-traceroute package does not allow you to set the source interface, whereas the traceroute package does. You almost certainly want the latter.


I had the same problem, but luckily I was able to fix it.

In a multihomed computer, only one default gateway can be defined, regardless of the number of interfaces present. If you try to traceroute to a host that is not in the same subnet as the interface that you are specifying with the -i flag, the traceroute will fail because of the absence of gateway (assuming you are not lucky enough to use the single interface that has a default gateway defined).

The solution to this is to define additional routing tables for every "extra" interface in your machine

A detailed guide can be found in this link.

In Summary if you have 2 interfaces with characteristics:

Interface A: IP:    subnet: Gateway
Interface B: IP: subnet: Gateway

and only the default gateway of A appears in the routing table you would do the following steps to include the gateway of B:

  1. Go to /etc/iproute2/rt_tables and at the end the following line

    1 routingTableB
  2. Run as root the following commands:

    ip route add dev <Interface B> src table routingTableB
    ip route add default via dev <Interface B> table routingTableB
    ip rule add from table routingTableB
    ip rule add to table routingTableB

After this you will be able to use the -i flag with Interface B without any issue.

Please note that this configuration will be lost after a reboot. Check the link above on how to make it permanent.

As a side note, the flag -g, does not dictate to which IP the interface will send the packets. It is a parameter included in the probes meant to dictate to the intermediate nodes how to route the packets.


Hmm... Just a concept here but I would say - policy routing:

  1. Add new RT table to your linux

    echo 200 test >> /etc/iproute2/rt_tables

  2. Add policy rule matching it

    ip rule add oif <iface_to_second_ISP> table test

  3. Add default route in your newly crated table

    ip route add via <second_ISP_next_hop> dev <iface_to_second_ISP> table test

  4. Flush the caches and test

    ip route flush cache

    ping -I <IP_on_second_ISP_iface> google.com

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.