1. Overview

In this quick tutorial, we’ll explore how the Linux ping command can help us to diagnose and troubleshoot network issues.

2. Syntax

Let’s start by having a look at the basic syntax:

ping [OPTIONS] DESTINATION

The ping (Packet INternet Groper) command uses the ICMP (Internet Control Message) protocol to send packets to a server.

To illustrate the ping command, let’s try pinging www.baeldung.com:

$ ping www.baeldung.com

PING www.baeldung.com (104.18.63.78): 56 data bytes
64 bytes from 104.18.63.78: icmp_seq=0 ttl=54 time=203.928 ms
64 bytes from 104.18.63.78: icmp_seq=1 ttl=54 time=181.025 ms
^C
--- www.baeldung.com ping statistics ---
2 packets transmitted, 2 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 181.025/192.476/203.928/11.452 ms

First, the ping command determines the IP address from the given domain name.

Next, it starts sending ICMP packets to the server. When our packet reaches the destination, then the server responds, and the ping command prints a line to our console.

By default, it prints a line that includes the following information:

  • Number of bytes sent, which is 64 ICMP data bytes, by default
  • IP Address of destination, in this case, 104.18.63.78
  • ICMP packet Sequence Number, for example, icmp_seq=0
  • Time to live (TTL), it indicates the number hops before the packet is dropped, here it’s 54
  • Ping command round trip time, in this case, 203.928 ms

So, we can see it sends ICMP packets continuously and we have to manually stop sending the packets using Control + C (Windows) or Command + C (Mac).

Also, it shows the percentage of packets lost due to network issues.

3. Basic Usage

Now that we’ve seen the default output, it’s time to see a few of the options in action.

3.1. Specifying the Interval Between Packets

By default, the ping command sends a new packet every second.

Let’s use -i  flag to change the ping interval**:** 

$ ping -i 3 www.baeldung.com

PING www.baeldung.com (104.18.62.78): 56 data bytes
64 bytes from 104.18.62.78: icmp_seq=0 ttl=54 time=190.037 ms
64 bytes from 104.18.62.78: icmp_seq=1 ttl=54 time=186.061 ms
64 bytes from 104.18.62.78: icmp_seq=2 ttl=54 time=187.458 ms
^C
--- www.baeldung.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 186.061/187.852/190.037/1.647 ms

Now there’s no output to prove this, but by running, we can see that we get a new line output every three seconds.

3.2. Controlling the Number of Packets

In practice, we often use the -c option to specify the number of ICMP packets that we want to send to the destination server:

$ ping -c 3 www.baeldung.com

PING www.baeldung.com (104.18.63.78): 56 data bytes
64 bytes from 104.18.63.78: icmp_seq=0 ttl=54 time=191.813 ms
64 bytes from 104.18.63.78: icmp_seq=1 ttl=54 time=177.645 ms
64 bytes from 104.18.63.78: icmp_seq=2 ttl=54 time=186.750 ms

--- www.baeldung.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 177.645/185.403/191.813/5.862 ms

As we can see above, we have only sent 3 ICMP packets rather than a continuous stream of packets.

3.3. Specifying the Packet Size

Additionally, ping also allows us to specify the packet size using the -s flag:

$ ping -s 40 -c 4 www.baeldung.com

PING www.baeldung.com (104.18.63.78): 40 data bytes
48 bytes from 104.18.63.78: icmp_seq=0 ttl=54 time=180.801 ms
48 bytes from 104.18.63.78: icmp_seq=1 ttl=54 time=181.822 ms
48 bytes from 104.18.63.78: icmp_seq=2 ttl=54 time=175.019 ms
48 bytes from 104.18.63.78: icmp_seq=3 ttl=54 time=172.405 ms

--- www.baeldung.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 172.405/177.512/181.822/3.927 ms

Here, we have used both -s and -c option together in the above example. As a result, it only sent four packets each of size 40 bytes (ICMP: 48 bytes).

This can be used to determine the latency in the network at different packet sizes. For example, in many complex networks, we start running into issues when the packet size is over 1500 bytes. By changing the packet size, we can determine the ideal packet size for our system.

4. Advanced Usage

Let’s now take a look at more advanced options.

4.1. Specify a Timeout

In practice, on a congested network, it might be useful to timeout the response after waiting for a specific time duration. We can do this with the -W option:

$ ping -c 4 -W 2 www.baeldung.com

PING www.baeldung.com (104.18.63.78): 56 data bytes

--- www.baeldung.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss, 4 packets out of wait time
round-trip min/avg/max/stddev = 192.397/195.137/198.573/2.480 ms

First, we send the request to the server. Then, we waited for two seconds to receive a response.

Due to low latency in our network we received all the packets successfully without any packet loss.

4.2. Flooding the Network Using Ping

Sometimes we want to test the network performance by sending as many packets as possible.

To illustrate, let’s run the ping command using the -f option:

$ ping -f localhost

PING localhost (127.0.0.1): 56 data bytes
..Request timeout for icmp_seq 250
.Request timeout for icmp_seq 251
.Request timeout for icmp_seq 252
.Request timeout for icmp_seq 253
...
^C
--- localhost ping statistics ---
271 packets transmitted, 250 packets received, 7.7% packet loss
round-trip min/avg/max/stddev = 0.010/0.019/0.049/0.005 ms

In this particular case, we have sent 271 packets with an average round trip time of 0.019 sec.

4.3. Retrieving the Network Summary

Next, we can retrieve the network summary using the command with the help of the -q flag:

First, let’s run the ping command using both the -c and -q flags:

$ ping -c 4 -q www.baeldung.com

PING www.baeldung.com (104.18.62.78): 56 data bytes

--- www.baeldung.com ping statistics ---
4 packets transmitted, 4 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 180.232/189.512/206.033/10.120 ms

As such, this option is used to run the command in a quiet mode. Here, we get only a summary, and we don’t see details of individual packets being sent.

4.4. Audible Ping Usage

Let’s run the ping utility in an audible mode using -a flag:

$ ping -a www.google.com

PING www.google.com (172.217.163.164): 56 data bytes
64 bytes from 172.217.163.164: icmp_seq=0 ttl=54 time=107.001 ms
64 bytes from 172.217.163.164: icmp_seq=1 ttl=54 time=52.143 ms
64 bytes from 172.217.163.164: icmp_seq=2 ttl=54 time=54.163 ms
^C
--- www.google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 52.143/71.102/107.001/25.398 ms

Here, we can hear a beep sound whenever our client can reach the host server.

5. Conclusion

In this quick tutorial, we’ve seen how to use the ping command.

We first covered how to control the interval, size, and a number of packets that we send in the command.

Finally, we explored some advanced uses like retrieving the network summary and flooding the network.


« 上一篇: 如何终止后台进程