Development

How to use the ping command in Linux

Captain Salem 4 min read

How to use the ping command in Linux

The ping command is one of the most popular and useful tools for diagnosing and troubleshooting network connectivity issues. It provides simple syntax and good functionality to perform basic diagnostic operations.

In this tutorial, we will teach you how to enhance the power of ping by learning how we can use it in various scenarios.

Let’s dive in.

How does ping work?

Ping is used to verify connectivity at IP-level to a TCP/IP address. Ping woks by sending a Internet Control Message Protocol or ICMP echo request and waits for a response. Once the target host receives the echo request, it replies back by sending an echo reply packet.

Once the host responds to the echo request, ping determines that the specified host is up. You can also ping to determine the Round Trip Time. RTT refers to the duration taken for a host to receive the request and respond back. The RTT value is measured in milliseconds.

Installing ping on Linux

The ping command is part of the iputils package. This package is readily available in almost any Linux distribution by default.

However, if you do not have ping installed on your machine, run the command:

sudo apt-get install iputils-ping -y

How to use the Ping Command

The ping command provides a very simple syntax. We start by the ping command followed by the target hostname, URL, or IP Address.

The command syntax is as shown:

ping [options] <destination>

An example illustration is as shown:

ping geekbits.io

You should see an output as shown:

PING geekbits.io (178.128.137.126) 56(84) bytes of data.
64 bytes from 178.128.137.126 (178.128.137.126): icmp_seq=1 ttl=51 time=182 ms
64 bytes from 178.128.137.126 (178.128.137.126): icmp_seq=2 ttl=51 time=177 ms
64 bytes from 178.128.137.126 (178.128.137.126): icmp_seq=3 ttl=51 time=196 ms
64 bytes from 178.128.137.126 (178.128.137.126): icmp_seq=4 ttl=51 time=218 ms
^C
--- geekbits.io ping statistics ---
10 packets transmitted, 10 received, 0% packet loss, time 9771ms
rtt min/avg/max/mdev = 170.179/181.536/217.791/13.877 ms

In the output above, we can see the ping command resolves the specified domain name to its corresponding IP address. Once resolved, it start sending ICMP.

If the specified destination address is up, it responds back and the ping command returns the information such as data bytes sent, IP address of the specified destination, ICMP sequence number for the packet, time to live, and ping time (the round trip time for packet to reach the target host and respond back).

By default, the ping will delay 1 second before sending then next packet.

NOTE: The ping command will continuously send ICMP packets to the specified destination unless you manually cancel it. You can do by pressing CTRL + C.

Specifying the Number of Packets

As mentioned, ping will continuously send packets until you manually terminate it. However you can specify the number of packets to send using the -c option.

For example, to send 3 packets:

ping -c 3 geekbits.io

Output:

ping -c 3 geekbits.io
PING geekbits.io (178.128.137.126) 56(84) bytes of data.
64 bytes from 178.128.137.126 (178.128.137.126): icmp_seq=1 ttl=51 time=178 ms
64 bytes from 178.128.137.126 (178.128.137.126): icmp_seq=2 ttl=51 time=171 ms
64 bytes from 178.128.137.126 (178.128.137.126): icmp_seq=3 ttl=51 time=175 ms

--- geekbits.io ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2662ms
rtt min/avg/max/mdev = 171.129/174.598/177.583/2.656 ms

In this case, once ping sends 3 packets, it automatically terminates and returns the summary statistics.

Specifying the Internet Protocol Version

If you run the ping command, it will default to the protocol version of local machine. However, you can force ping to use either IPv4 or IPv6 using the -4 and -6 respectively.

Fore example, to use IPv4, run:

ping -4 <target>

For IPv6, run:

ping -6 <target>

Specifying the Source Interface

By default, ping will use the default route to send the ICMP packets. However, you can specify the target interface using the -I option.

The command syntax is as shown:

ping -I <interface> <target>

For example:

ping -I eth0 geekbits.io

The command above tells ping to use the eth0 interface.

Specifying Time Interval Between Packets

By default, ping will delay 1 second before sending the next packet. However, you can change this value using the -i option.

For example:

ping -i 0.5 geekbits.io

In this case, ping will delay for half a second before sending the next packet.

Specifying Time Limit

You can tell ping to stop sending packets after a specific amount of time using the -w option. For example, to stop ping after 10 seconds:

ping -w 10 geekbits.io

Suppressing the Summary Statistics

If you do not want ping to display the summary statistics, we can use the -q option for quiet mode. An example

ping -c 3 -q geekbits.io

Output:

PING geekbits.io (178.128.137.126) 56(84) bytes of data.

--- geekbits.io ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2321ms
rtt min/avg/max/mdev = 170.702/170.999/171.305/0.246 ms

In this case, ping does not return summary for every packet sent.

Get Audio Ping If Host is Up.

Ping also allows you to play an audio “ping” when the host is reachable using the -a.

ping -a geekbits.io

NOTE: Ping will play an audio for every packet sent.

Show Ping Version

To check the ping version installed on your system with the command:

ping -V geekbits.io

Output:

ping from iputils s20190709

Conclusion

Congratulations, you have successfully learned how to tap into the power of ping for network diagnostics and testing. We hope you enjoyed this tutorial.

Feel free to reach out to us or leave a comment down below.

Share
Comments
More from Cloudenv

Cloudenv

Developer Tips, Tricks and Tutorials.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Cloudenv.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.