Read in 4 minutes
Linux Ping Command
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, we can determine whether a remote destination IP is active or inactive, 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-ping) package which is pre-installed on nearly all Linux distributions. The ping command is also available on Windows, MacOSX, 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
The output will look something like this:
PING google.com (220.127.116.11) 56(84) bytes of data. 64 bytes from muc11s01-in-f14.1e100.net (18.104.22.168): icmp_seq=1 ttl=53 time=40.2 ms 64 bytes from muc11s01-in-f14.1e100.net (22.214.171.124): icmp_seq=2 ttl=53 time=41.8 ms 64 bytes from muc11s01-in-f14.1e100.net (126.96.36.199): icmp_seq=3 ttl=53 time=47.4 ms 64 bytes from muc11s01-in-f14.1e100.net (188.8.131.52): 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 will resolve the domain name into an address and will start to send ICMP packages to the destination IP. If the destination IP is reachable it will respond back and the ping command will print a line that includes 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 muc11s01-in-f14.1e100.net (184.108.40.206)
- The ICMP sequence number for each packet.
- The Time to Live. -
ttl=53- How does TTL work?
- The ping time, measured in milliseconds, is the round trip time for the packet to reach the host and for the response to return to the sender. -
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 will display 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. If 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 are 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 if you are 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 we already discussed, by default the ping command 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
By default, ping will 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
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. For IPv6 pass the
-6 option or use
ping -4 DESTINATION
ping -6 DESTINATION
By now you should have a good understanding of how to use the Linux
ping command. You can always view all available
ping command options by typing
man ping in your terminal.