Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In this quick tutorial, we’ll explore how the Linux ping command can help us to diagnose and troubleshoot network issues.
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:
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.
Now that we’ve seen the default output, it’s time to see a few of the options in action.
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.
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.
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.
Let’s now take a look at more advanced options.
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.
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.
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.
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.
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.