2

I've looked through some of the examples on this site with regards to multi-interface pings but none of the approaches seem to work for me.

I have two interfaces

  • eth0 ( 192.168.0.x)
  • wlan0 (192.168.1.2)

eth0 gets its IP from dhcp server.

wlan0 has static IP.

When I run the command

ping -I eth0 www.google.com

I get results that appear to be from eth0 (ipv6 stuff).

When I run the command

ping -I wlan0 www.google.com

I get the following output.

connect: Network is unreachable

If I unplug my eth0, and I run the comand again, I get desired results.

How can ping an external server with the choice of an interface ? It's almost like one is dominating, and if one exists, the other is disregarded.

  • 1
    You need first to setup multi homing, with policy routing / source-specific routing. I'm sure there are examples on SE. look at en.wikipedia.org/wiki/… and en.wikipedia.org/wiki/Source-specific_routing#The_problem to understand what it's about. Settings and implementation details will probably depend on OS. – A.B Jul 5 '19 at 6:05
  • 1
    Note that even if you get this to work with policy routing as described in the answer, this is not a good way to use two ISPs simultaneously: Every program you use in this configuration will have to bind to an interface, just as ping does. And some programs can't do that. So the answer to the FAQ "how do I use two ISPs at the same time?" is "you don't, at least not in any sane way". – dirkt Jul 5 '19 at 6:56
  • @dirkt, I am the author of that answer, but I can't really understand your warning. It is easy to run services on multihomed host accessible via both ISPs. You always can bind them to 0.0.0.0, which will make them answer to any locally-destined packet, and to use a system-assigned address on outgoing packets. Only when socket is accepted, it binds to concrete address with which it was accepted. And this is default configuration for almost everything I've seen, with very little exceptions, in particular ntp, dns, dhcp services, which by default bind independently to every address anyway. – Nikita Kipriyanov Jul 16 '19 at 10:21
  • @NikitaKipriyanov: The problem is not having services that will listen on multiple network interfaces - as you say, that's trivial and works out of the box, and you might even get away without routing for this. The problem is outgoing connections with multiple ISPs. – dirkt Jul 16 '19 at 16:03
5

The issue is how the routing works.

If you have more than one interface, each could be configured with a gateway. I can't sure how routing table really was filled up on your particular system, but I suspect your system gave the best priority to the first default route appeared, then second best to the second appeared and so on. Only the best default route is active. If you put the corresponding interface down, its default route also disappears, so if there some other default route left it will take over. That priority is called "metric".

When you ping, your system creates a packet with some source address. It could pick some address by itself (from the "src" attribute of a local-network route), but you can specify an address to use or an interface from which local-network route to pick up that address, with -I switch. This is what this swich do; in only sets source address. It doesn't select routes and doesn't affect by itself which interface to use.

What is going on: you send a packet with a source address of a "second" interface from the "first" interface to the "first" gateway. Most probably it is dropped on gateway, which doesn't know how to reach that source address (when the time comes to reply, to whom it should reply?), or doesn't expect it appearing that way.

What to do? You should set up routing based on source address. This is advanced topic. In Linux you set up several routing tables, each will have a different default route, and instruct system to select which routing table to use based on packet source address. This is called multi-homing.

In Linux basic multi-homing setup in your case will be:

ip route add default via 192.168.0.GW0 table 1
ip route add default via 192.168.1.GW1 table 2
ip rule add from 192.168.0.x lookup 1
ip rule add from 192.168.1.2 lookup 2

the first two are default routes with resp. gateways, third and fourth are which table to use for which address. Of course, you'd better put concrete real values into placeholders. DHCP? Ether forget about multi-homing, or write a script which sets up everything dynamically on reception of address, and removes that when address is lost (in general the route will be automatically deleted, the rule should always be deleted manually). Autoconfigure on boot up? Your distribution manual could direct you into right way, be prepared to break networking and know how to restore it into clean state.

| improve this answer | |
  • This was great. It gave me some areas to look into and I have come across similar topics while searching for the answer, but I didn't realize that they are the reason for my specific problem. I appreciate the detail and it has helped a great deal in my understanding and where I need to look or what questions to ask. – efox29 Jul 5 '19 at 16:13
  • You're welcome. This is the essential IP routing, applied to Linux, so if you have to manage these things it could be benefitical to just go and dive into LARTC. – Nikita Kipriyanov Jul 16 '19 at 10:12

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.