Ping command is the most famous and used tools for troubleshooting, testing, and diagnosing network connectivity issues.
It works on sending ICMP (Internet Control Message Protocol) Echo Request packages to a specified destination IP or domain name on the network and waits for a reply. When the destination receives the package it responds with an ICMP echo reply.
Ping command determine a remote destination IP is active or inactive. You can also find the round trip delay in communication with the destination and check whether there is loss of packet.
It is also a part of the iputils or iputils-ping package which is pre-installed in almost all Linux distributions. It is also available in macOS, windows etc.
Use of Ping Command
Syntax of the ping command is:
ping [OPTIONS] DESTINATION
For better understanding, how the ping command work lets ping most common domain name google.com:
The output will look something like this:
Ping command resolve the domain name into the IP address and start sending ICMP packages to the destination IP address. If destination IP is not reachable it will send back and the ping command prints a line that includer the following fields.
- The number of data bytes. The default is 56, which translates into 64 ICMP data bytes –
- The IP address of the destination –
from maa05s24-in-f14.1e100.net (22.214.171.124).
- The ICMP sequence number for each packet.
- The Time to Live. –
- The ping time, measured in milliseconds which is the round trip time for the packet to reach the host, and the response to return to the sender. –
By default, interval between two sending a new packet is one second.
It will continue to send the ICMP packages to the Destinations IP address until it will receive an interrupt. To stop the command, just hit the ctrl+c key.
Once the command stop, it will displays a statistics, including the percentage of packet loss. If packet loss means the data was dropped somewhere in the netwok, indicating an issue within the network. If there is a packet loss, you can use the command traceroute command to find where the packet loss occurs.
In case ping does not return a reply, which means that the network communication is not established. When this happens, it doesn’t always mean that the destination IP is not active. Some hosts may have a firewall that is blocking the ICMP traffic or set to not respond to ping requests.
On success, the
ping command exits with code
0. Otherwise, it will exit with code
2. This can be useful when using the
ping utility in a shell script.
To Specific number of Packets
As already mentioned, by default,
ping will continue to send ICMP packages until it receives an interrupt signal. To specify the number of Echo Request packages to be sent after which ping will exit, use the
-c option followed by the number of the packages:
ping -c 1 DESTINATION
For example, to ping amazon
.com only one time you would use:
ping -c 1
To Specify the source Interface
The default behaviour of the
ping command is to send ICMP packages via the default route. If you have multiple interfaces on your machine you can specify the source interface with the
ping -I INTERFACE_NAME DESTINATION
The following command will ping amazon
em2 as a source interface:
ping -I em2
To specify the Internet Protocols(IP)
When execute the
ping command, it will use either IPv4 or IPv6, depending on your machine DNS settings.
ping to use IPv4, pass the
-4 option, or use its alias
ping -4 DESTINATION
For IPv6, pass the
-6 option or use
ping -6 DESTINATION
ping is a command-line network utility that allows you to test the IP-level connectivity of a given host on the network. We can also use of ping utility to test the network connectivity between ip or networks.