Ping Command in Linux

Updated 

4 min read

The ping command is one of the most used utilities for troubleshooting, testing, and diagnosing network connectivity issues.

Ping works by sending one or more ICMP (Internet Control Message Protocol) Echo Request packages to a specified destination IP on the network and waits for a reply. When the destination receives the package, it will respond back with an ICMP echo reply.

With the ping command, you can determine whether a remote destination IP is active or inactive. You can also find the round-trip delay in communicating with the destination and check whether there is a packet loss.

The ping utility is a part of the iputils (or iputils-ping) package, which is pre-installed on nearly all Linux distributions. ping is also available on Windows, macOS, and FreeBSD.

How to Use the Ping Command

The syntax for the ping command is as follows:

ping [OPTIONS] DESTINATION

To best illustrate how the ping command works let’s ping google.com:

ping google.com

The output will look something like this:

PING google.com (172.217.22.206) 56(84) bytes of data.
64 bytes from muc11s01-in-f14.1e100.net (172.217.22.206): icmp_seq=1 ttl=53 time=40.2 ms
64 bytes from muc11s01-in-f14.1e100.net (172.217.22.206): icmp_seq=2 ttl=53 time=41.8 ms
64 bytes from muc11s01-in-f14.1e100.net (172.217.22.206): icmp_seq=3 ttl=53 time=47.4 ms
64 bytes from muc11s01-in-f14.1e100.net (172.217.22.206): icmp_seq=4 ttl=53 time=41.4 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 7ms
rtt min/avg/max/mdev = 40.163/42.700/47.408/2.790 ms

The ping command resolves the domain name into an IP address and starts sending ICMP packages to the destination IP. If the destination IP is reachable it will respond back and the ping command prints a line that includes the following fields:

  • The number of data bytes. The default is 56, which translates into 64 ICMP data bytes - 64 bytes
  • The IP address of the destination - from muc11s01-in-f14.1e100.net (172.217.22.206)
  • The ICMP sequence number for each packet. icmp_seq=1
  • The Time to Live. - ttl=53 - How does TTL work?
  • The ping time, measured in milliseconds which is the round trip time for the packet to reach the host, and for the response to return to the sender. - time=41.4 ms

By default, the interval between sending a new packet is one second.

The ping command will continue to send ICMP packages to the Destination IP address until it receives an interrupt. To stop the command, just hit the Ctrl+C key combination.

Once the command stops, it displays a statistic including the percentage of packet loss. The packet loss means the data was dropped somewhere in the network, indicating an issue within the network. If there is a packet loss, you can use the traceroute command to identify where the packet loss occurs.

If ping does not return a reply, it 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 1 or 2. This can be useful when using the ping utility in a shell script.

In the following sections, we’ll go over the most commonly used ping command options.

Specify the 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 linuxize.com only one time you would use:

ping -c 1 linuxize.com

Specify the Source Interface

The default behavior 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 -I option:

ping -I INTERFACE_NAME DESTINATION

The following command will ping linuxize.com using em2 as a source interface:

ping -I em2 linuxize.com

Specify the Internet Protocol

When you run the ping command, it will use either IPv4 or IPv6, depending on your machine DNS settings.

To force ping to use IPv4, pass the -4 option or use its alias ping4:

ping -4 DESTINATION

For IPv6, pass the -6 option or use ping6:

ping -6 DESTINATION

Conclusion

ping is a command-line network utility that allows you to test the IP-level connectivity of a given host on the network.

To view all available options of the ping command, type man ping in your terminal.

If you have any questions or feedback, feel free to leave a comment.