Ping Command in Linux With Examples
ping command is one of the most used tools 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 responds with an ICMP echo reply.
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.
ping is part of the
iputils-ping) package, which is pre-installed on nearly all Linux distributions. It is also available on Windows, macOS, and FreeBSD.
How to Use Ping Command
The syntax for the ping command is as follows:
ping [OPTIONS] DESTINATION
To better illustrate, how the ping command works let’s ping
$ ping google.com
The output will look something like this:
OUTPUT PING google.com (18.104.22.168) 56(84) bytes of data. 64 bytes from muc11s01-in-f14.1e100.net (22.214.171.124): icmp_seq=1 ttl=53 time=40.2 ms 64 bytes from muc11s01-in-f14.1e100.net (126.96.36.199): icmp_seq=2 ttl=53 time=41.8 ms 64 bytes from muc11s01-in-f14.1e100.net (188.8.131.52): icmp_seq=3 ttl=53 time=47.4 ms 64 bytes from muc11s01-in-f14.1e100.net (184.108.40.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
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 –
- The IP address of the destination –
from muc11s01-in-f14.1e100.net (220.127.116.11).
- The ICMP sequence number for each packet.
- 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 the response to return to the sender. –
By default, the interval between sending a new packet is one second.
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.
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
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
reconshell.com only one time you would use:
$ ping -c 1 reconshell.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
$ ping -I INTERFACE_NAME DESTINATION
The following command will ping reconshell
em2 as a source interface:
ping -I em2 reconshell.com
Specify the Internet Protocol
When you run 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. 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.