Ping Command in Linux

By 

Updated on

5 min read

Linux Ping Command

The 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 packets to a specified destination IP on the network and waiting for a reply. When the destination receives the packet, it responds 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.

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

How to Use the Ping Command

The syntax for the ping command is as follows:

txt
ping [OPTIONS] DESTINATION

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

Terminal
ping google.com

The output will look something like this:

output
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 packets to the destination IP. If the destination IP is reachable, it will respond 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 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 packets 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 statistics, including the percentage of packet loss. 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. For packet-level capture, use tcpdump .

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.

Info
On some systems, ping requires elevated permissions. If you see a permission error, run it with sudo or install ping with the right capabilities.

Specify the Number of Packets

As already mentioned, by default, ping will continue to send ICMP packets until it receives an interrupt signal. To specify the number of Echo Request packets to be sent after which ping will exit, use the -c option followed by the number of the packets:

sh
ping -c 1 DESTINATION

For example, to ping linuxize.com only one time you would use:

Terminal
ping -c 1 linuxize.com

Specify the Source Interface

The default behavior of the ping command is to send ICMP packets via the default route. If you have multiple interfaces on your machine you can specify the source interface with the -I option:

sh
ping -I INTERFACE_NAME DESTINATION

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

Terminal
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:

sh
ping -4 DESTINATION

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

sh
ping -6 DESTINATION

Change the Interval and Timeout

By default, ping sends a packet every second. To change the interval between packets, use the -i option:

sh
ping -i 2 DESTINATION

To set a timeout for each reply, use -W (in seconds):

sh
ping -W 2 DESTINATION

To stop the command after a given deadline, use -w:

sh
ping -w 10 DESTINATION

Change the Packet Size

To change the size of the ICMP payload, use the -s option:

sh
ping -s 120 DESTINATION

Quiet Output

To show only a summary, use the -q option:

sh
ping -q -c 5 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.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page