ping command is one of the most popular and useful tools for diagnosing and troubleshooting network connectivity issues. It provides simple syntax and good functionality to perform basic diagnostic operations.
How does ping work?
Ping is used to verify connectivity at IP-level to a TCP/IP address. Ping woks by sending a Internet Control Message Protocol or ICMP echo request and waits for a response. Once the target host receives the echo request, it replies back by sending an echo reply packet.
Once the host responds to the echo request, ping determines that the specified host is up. You can also
ping to determine the Round Trip Time.
RTT refers to the duration taken for a host to receive the request and respond back. The RTT value is measured in milliseconds.
Installing ping on Linux
ping command is part of the
iputils package. This package is readily available in almost any Linux distribution by default.
However, if you do not have ping installed on your machine, run the command:
sudo apt-get install iputils-ping -y
How to use the Ping Command
ping command provides a very simple syntax. We start by the ping command followed by the target hostname, URL, or IP Address.
The command syntax is as shown:
ping [options] <destination>
An example illustration is as shown:
You should see an output as shown:
PING geekbits.io (188.8.131.52) 56(84) bytes of data. 64 bytes from 184.108.40.206 (220.127.116.11): icmp_seq=1 ttl=51 time=182 ms 64 bytes from 18.104.22.168 (22.214.171.124): icmp_seq=2 ttl=51 time=177 ms 64 bytes from 126.96.36.199 (188.8.131.52): icmp_seq=3 ttl=51 time=196 ms 64 bytes from 184.108.40.206 (220.127.116.11): icmp_seq=4 ttl=51 time=218 ms ^C --- geekbits.io ping statistics --- 10 packets transmitted, 10 received, 0% packet loss, time 9771ms rtt min/avg/max/mdev = 170.179/181.536/217.791/13.877 ms
In the output above, we can see the
ping command resolves the specified domain name to its corresponding IP address. Once resolved, it start sending ICMP.
If the specified destination address is up, it responds back and the ping command returns the information such as data bytes sent, IP address of the specified destination, ICMP sequence number for the packet, time to live, and ping time (the round trip time for packet to reach the target host and respond back).
By default, the ping will delay 1 second before sending then next packet.
ping command will continuously send ICMP packets to the specified destination unless you manually cancel it. You can do by pressing CTRL + C.
Specifying the Number of Packets
ping will continuously send packets until you manually terminate it. However you can specify the number of packets to send using the
For example, to send 3 packets:
ping -c 3 geekbits.io
ping -c 3 geekbits.io PING geekbits.io (18.104.22.168) 56(84) bytes of data. 64 bytes from 22.214.171.124 (126.96.36.199): icmp_seq=1 ttl=51 time=178 ms 64 bytes from 188.8.131.52 (184.108.40.206): icmp_seq=2 ttl=51 time=171 ms 64 bytes from 220.127.116.11 (18.104.22.168): icmp_seq=3 ttl=51 time=175 ms --- geekbits.io ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2662ms rtt min/avg/max/mdev = 171.129/174.598/177.583/2.656 ms
In this case, once ping sends 3 packets, it automatically terminates and returns the summary statistics.
Specifying the Internet Protocol Version
If you run the
ping command, it will default to the protocol version of local machine. However, you can force ping to use either IPv4 or IPv6 using the
Fore example, to use IPv4, run:
ping -4 <target>
For IPv6, run:
ping -6 <target>
Specifying the Source Interface
ping will use the default route to send the ICMP packets. However, you can specify the target interface using the
The command syntax is as shown:
ping -I <interface> <target>
ping -I eth0 geekbits.io
The command above tells ping to use the
Specifying Time Interval Between Packets
By default, ping will delay 1 second before sending the next packet. However, you can change this value using the
ping -i 0.5 geekbits.io
In this case,
ping will delay for half a second before sending the next packet.
Specifying Time Limit
You can tell ping to stop sending packets after a specific amount of time using the
-w option. For example, to stop ping after 10 seconds:
ping -w 10 geekbits.io
Suppressing the Summary Statistics
If you do not want ping to display the summary statistics, we can use the
-q option for quiet mode. An example
ping -c 3 -q geekbits.io
PING geekbits.io (22.214.171.124) 56(84) bytes of data. --- geekbits.io ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2321ms rtt min/avg/max/mdev = 170.702/170.999/171.305/0.246 ms
In this case, ping does not return summary for every packet sent.
Get Audio Ping If Host is Up.
Ping also allows you to play an audio "ping" when the host is reachable using the
ping -a geekbits.io
NOTE: Ping will play an audio for every packet sent.
Show Ping Version
To check the ping version installed on your system with the command:
ping -V geekbits.io
ping from iputils s20190709
Congratulations, you have successfully learned how to tap into the power of
ping for network diagnostics and testing. We hope you enjoyed this tutorial.
Feel free to reach out to us or leave a comment down below.