Ping - How does it work?


Nitin Venkatesh's Gravatar

Nitin Venkatesh
published Sept. 11, 2012, 6 p.m.


Ping is a network diagnostic tool that’s used to check if a host in a network is alive and responding. Ping uses ICMP messages. More particularly ICMP query messages

ICMP (Internet Control Message Protocol) is a companion to the IP protocol. It compensates the IP protocol in error reporting since IP protocol doesn’t have an error reporting method in place. ICMP only reports errors and expects higher layers of the OSI architecture model to handle and correct the errors.

ICMP has two types of messages - error reporting messages and query messages. Query messages are generally used to diagnose network problems. There are two types of query messages -

  • Echo-request message
  • Echo-reply message

So what happens when we ping a machine?

  • The source sends an ICMP echo-request message to the destination.
  • The ping program sets an sequence identifier which gets incremented with each echo-request message. It also sets a TTL (Time-to-live) period.
  • Ping also inserts the sending time in the data section of the message.
  • If the host is alive and responding, it sends an ICMP echo-reply message back to the source.
  • Ping notes the time of the arrival of the response message, uses the sending time in the message part and calculates the Round-trip time
  • It then increments the sequence identifier (as said above) and sends a new echo-request message. This goes on for the number of ping requests set by the user or the program is terminated.

The whole of the data is calculated to summarize the percentage of packet loss and other such information and the summarized data is then displayed, showing the number of packets transmitted, received, percentage of packet loss, total time taken, the minimum, average and maximum round-trip time. This ofcourse, is in addition to the data displayed live when the program is running. Here’s an example output of showing how pinging Askubuntu from my machine looks like:

nits@excalibur:~$ ping -c 5 askubuntu.com
PING askubuntu.com (64.34.119.12) 56(84) bytes of data.
64 bytes from stackoverflow.com (64.34.119.12): icmp_req=1 ttl=50 time=252 ms
64 bytes from stackoverflow.com (64.34.119.12): icmp_req=2 ttl=50 time=252 ms
64 bytes from stackoverflow.com (64.34.119.12): icmp_req=3 ttl=50 time=253 ms
64 bytes from stackoverflow.com (64.34.119.12): icmp_req=4 ttl=50 time=252 ms
64 bytes from stackoverflow.com (64.34.119.12): icmp_req=5 ttl=50 time=251 ms