I have Ubuntu 18.04 and two network interfaces (eth0 & wlan0) attached to it. So, I have effectively two LAN's (say LAN1 and LAN2) When I fire a ping command simply like ping followed by some IPv4/IPv6 address, how does it work internally? I have following questions. Please help to clear these.

  • How does ping find route to the address?
  • Which interface ping uses to reach to the destination address? (LAN1 or LAN2 or both)
  • Are both interfaces tried one after another in any particular sequence?
  • How does it work internally, when address is from a host in one of the local networks? (LAN1 or LAN2)
  • How does it work when, destination address is not part of local network (LAN1 or LAN2). How does the ping command reach to the gateway? How does it pick the gateway? is there any pre-defined sequence?

1 Answer 1


Your OS maintains a route table which tell it where a packet should go. This table is also used to put on the packet from which address the packet comes from.

Then, you can program source.sin_addr.s_addr=htol(INADDR_ANY); to indicate before bind() that the source address is not specified. (It is unspecified by default, but this line is useful if you want to use bind() to bind only the source port.)

Note that the -B option makes ping bind the socket then the second and following source address can be fixed. The -I can bind an interface which set the source address.

This replies more or less to the 3 first points.

About the case of a ping from LAN1 (let’s say to LAN2 ( uses its route table indicating that is reacheable through (the gateway). It sends to this ethernet address a ping « from to ». Then the gateway see it, find is directly reachable via LAN2 and forward the packet. The answer from to follows the same pattern.

The fourth point is more tricky. With public addresses, it works flawlessly because all gateways exchange their routes and everyone knows who is where (through which gateway). Sometimes, the route table is simple : LAN1 is reachable directly through the interface eth0, everything else through the router X on the LAN1. But for many uses we use local addresses : a addresse is not reachable from Internet. Then when send an IP packet, the router change the from address with a public address well known by Internet routers and memorizes the switch. When the answer come back to the public address, the router (which had memorised the switch), recover the actual address ( and can have its response.

  • Frédéric are you sure you intend this as a community answer rather than one of your own?
    – roaima
    Oct 9, 2021 at 12:32
  • I can’t interpret this comment. The question is rather complex (OS behaviour, system programming since ping is rather a system program, Internet routing), then it deserves a complex and detailed answer. Maybe the point about ping options are superfluous but they illustrate ping can work in different way. Oct 9, 2021 at 12:36
  • Yes, it can be intended as a community answer. But I guess a community answer should be more polished (typically examples), but it can be a good start. Oct 9, 2021 at 12:45
  • But it already is (willingly or by mistake) a community answer.
    – A.B
    Oct 9, 2021 at 12:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .