Using the ping Command on Linux

In this tutorial, we will be showing you how to use the ping command on a Linux system.

Using the ping command on Linux

The ping command is a handy tool that allows you to troubleshoot your Linux device’s network connection to an IP address. This address can either be local or external.

Using this tool, you can get various helpful information, such as the time it took for the request to return. Additionally, you can also use this to check if your network is experiencing any packet loss reaching its destination.

The ping command works by sending an ICMP echo request to the designated IP address. Once sent, the utility will wait for the server to respond with an ICMP echo reply. The time it took for this request to complete gives your the ping time.

If an ICMP request fails, the tool will assume that this packet was lost and never reached its destination. If you only receive no response, this can also indicate that the server is blocking IMCP requests.

Over the next couple of sections, we will explore the various ways to use the ping command. Additionally, we will touch on how to understand the data it presents to you.

Table of Contents

Syntax of the ping Command

The syntax for the ping command is straightforward as it only has two parameters that you can utilize. Furthermore, one of those parameters is entirely optional.

Below you can see the incredibly straightforward syntax for this utility.

ping [OPTION] DESTINATION

The “[OPTION]” parameter allows you to control the behavior of the ping command and is entirely optional. Many options allow you to modify how often a ping request is sent and how the data is sent.

The final parameter is “DESTINATION“. This parameter is where you specify the address you want to send the ping request to. For example, you can use a hostname, domain name, or IP address.

When used without any option, the tool will continue to ping the destination until the process is stopped. If the process is in the foreground of your terminal, you can press CTRL + C to exit.

Additionally, the time between each ping request is set at 1 second by default.

The Data of the ping Command

Before we start showing you how to use the ping command, let us explore the data that this utility will give you.

PING google.com (142.250.70.142) 56(84) bytes of data.

This line tells you the address you are connecting to. If this is a hostname or domain name, it will show you the IP address it resolves to.

Additionally, it tells you the number of ICMP bytes of data that the ping tool will send for each request.

64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=1 ttl=58 time=19.4 ms
  • 64 bytes” – You will get a line like the above for every ping response. It will indicate how many ICMP bytes were received in the reply.
  • from mel04s01-in-f14.1e100.net (142.250.70.142)” – The next part of the line indicates the address that the response came from. If this is a domain name, it will also show the resolved IP address.
  • icmp_seq=1” – The sequence number for this packet. This value is used to keep track of what number packet was sent. The ping tool can use it to know when a packet never got a response.
  • ttl=58” – This value refers to the number of hops that a packet will go through before dying. Using this value, you can stop a packet from being stuck indefinitely, passing from router to router.
  • time=19.4ms” – The final value represents how long it took for this ping request to be sent and the response to be received.
--- google.com ping statistics ---

When the ping command is stopped, it will give you overall statistics on your requests.

1 packets transmitted, 1 received, 0% packet loss, time 0ms

Within this information you can see how many packets were transmitted (“1 packets transmitted“) and how many were received (“1 received“). This value can indicate the percentage of packets lost in transmission (“0% packet loss“).

Additionally, you can tell the overall time the command took to complete (“time 0ms“).

rtt min/avg/max/mdev = 19.435/19.435/19.435/0.000 ms

rtt” (round-trip time) is the measure of the amount of time it took for a signal to be sent and for it to be returned. These four values give you an indicator of the minimum (min), average (avg), maximum (max), and mean deviation (mdev).

A higher mean deviation indicates the more variable the RTT is over time. This can indicate problems such as speed issues with bulk transfers or poor VoIP quality.

Basic Usage of the ping Command

You can use the ping command at its most basic usage by only specifying a destination. Using the command this way will continue to ping the destination until the process is killed.

This syntax is simple as using “ping” followed by the destination you want to ping.

ping DESTINATION

When you want to stop this utility, you must press CTRL + C on your keyboard.

Example of Using the ping Command

For this example, let us try performing a ping to Googles servers. This is an incredibly simple process as we have no need to specify any options.

We need to use “ping” followed by one of Google’s domain names. In our case, we will use “google.com“.

ping google.com

Using ping this way, it will continue to run into you kill the process. To stop the command, press CTRL + C on your keyboard.

You should end up with a result similar to the one that have shown below.

PING google.com (142.250.70.206) 56(84) bytes of data.
64 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=1 ttl=58 time=19.9 ms
64 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=2 ttl=58 time=19.0 ms
64 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=3 ttl=58 time=19.1 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 18.976/19.304/19.870/0.401 ms

Limiting the Number of Ping Requests

Instead of allowing the ping command to run endlessly, you can limit the number of requests it makes. Once it hits this limit, it will automatically stop.

The command has the “-c count” option that you can utilize to achieve this. Using this option, you can specify the exact amount of times the tool can ping.

ping -c COUNT DESTINATION

Example of Limiting the Number of Pings

For this example, let us use this option to limit the number of pings the command will make to five.

We need to use “ping” followed by the “-c” option and the number 5. Afterward, we need to specify the destination, which will be “google.com“.

ping -c 5 google.com

Below you can see that after five ping requests were made, the command automatically stopped.

PING google.com (142.250.70.142) 56(84) bytes of data.
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=1 ttl=58 time=18.9 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=2 ttl=58 time=19.2 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=3 ttl=58 time=19.7 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=4 ttl=58 time=19.4 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=5 ttl=58 time=19.8 ms

--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 18.882/19.407/19.773/0.331 ms

Setting a Time Limit for the ping Command

The ping command allows you to set the time limit for the ping command. The utility will continue to perform the ping requests until the time limit is up. Even if the tool is still waiting on a response, it will end regardless.

You need to use the “-w deadline” option to set this time limit. The value for this option is the time in seconds before the command will exit.

ping -w SECONDS DESTINATION

Example of How to Set the Time Limit on the ping Command

For example, we can use the following command if we only want to make pings requests for 6 seconds.

All we need to do is use “ping” followed by the “-w” option, the number “6“, and our destination “google.com“.

ping -w 6 google.com

After 6 seconds, the command should automatically terminate, and you will end up with a result similar to what we have shown below. As a ping is sent every second, we ended up with six responses.

PING google.com (142.250.70.142) 56(84) bytes of data.
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=1 ttl=58 time=19.5 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=2 ttl=58 time=19.6 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=3 ttl=58 time=19.4 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=4 ttl=58 time=19.8 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=5 ttl=58 time=19.0 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=6 ttl=58 time=19.5 ms

--- google.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5008ms
rtt min/avg/max/mdev = 18.991/19.462/19.815/0.253 ms

Changing the Time To Live for the ping Command

The default ping commands time to live (TTL) differs depending on the operating system. The tool can adjust this value through one of its options.

The TTL is how many hops a request will perform before it dies. This value ensures the request doesn’t get stuck infinitely going from one hop to another.

To adjust the time to live of a request, you will need to use the “-t ttl” option.

ping -t TTL DESTINATION

Example of Changing the TTL for the ping Command

For example, if we wanted to set the maximum amount of hops our request can make to “30“, we would use the command below.

All we need to do is use “ping” followed by the “-t” option, our new TTL value of “30“, then our destination “google.com

ping -t 30 google.com

Below you can see an example of the result you will get after changing this option. The TTL you see in the ping requests is the one the destination server responds with.

PING google.com (142.250.70.142) 56(84) bytes of data.
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=1 ttl=58 time=19.3 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=2 ttl=58 time=19.3 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=3 ttl=58 time=19.5 ms
^C
--- google.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 5007ms
rtt min/avg/max/mdev = 19.073/19.372/19.592/0.182 ms

Control the Size of the ping Request Packet

By default, the ping command will set a packet size of 56 bytes when sending a request. On top of this ping request is the ICMP header, which accounts for an additional 8 bytes of data bringing the total to 64 bytes.

Changing the packet size can be useful when checking your network’s performance with larger or smaller packet sizes.

You can adjust the size of the ping packet sent by utilizing the “-s packetsize” option.

ping -s PACKETSIZE DESTINATION

Be careful when setting large packet sizes. Servers will often block these requests and potentially block your IP address outright.

Example of Setting the Size of the ping Packet

Let’s say that we wanted to set the packet size to 20 bytes of data in total. Accounting for the 8 bytes of ICMP header, we set the packet size to 12 bytes.

To use this option, we use the “ping” command, followed by the “-s” option, our packet size of “12“, and our destination, which will be “google.com“.

ping -s 12 google.com

After running this command, you will get a similar result to what we have shown below. You can see that our ping request is only sending 20 bytes of data and getting 20 back from our destination.

PING google.com (142.250.70.206) 12(40) bytes of data.
20 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=1 ttl=58 time=18.8 ms
20 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=2 ttl=58 time=19.0 ms
20 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=3 ttl=58 time=18.7 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 18.728/18.847/18.975/0.101 ms

Adjust the Time to Wait for the ping command

For every ping, the tool only waits a certain amount of time to receive a response. The default wait time is different depending on the operating system you are using it on.

The ping manual defines this time to wait as the same time as two “round-trip times”.

If the default time is set too little or to high, you can control this using the “-W SECONDS” option. You define the wait time in seconds. Additionally, it allows decimal times using the decimal separator (.).

ping -W SECONDS DESTINATION

If you set the time to wait to 0, the program will wait infinitely for the request to complete. It is unrecommended to do this.

Example of Changing the Time to Wait

For example, we would use the command below if we wanted a ping request to time out after 5 seconds have passed.

Below you can see how simple it is to use this option, with us using “-W” followed by the number five and our example destination of “google.com“.

ping -W 5 google.com

Alternatively, if we wanted to set a time to wait of half a second, we could use a command like below.

ping -W 0.5 google.com

As long as the pings are being received relatively quickly, the result will look the same as usual.

PING google.com (142.250.70.206) 56(84) bytes of data.
64 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=1 ttl=58 time=19.0 ms
64 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=2 ttl=58 time=32.9 ms
64 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=3 ttl=58 time=19.3 ms
64 bytes from mel05s01-in-f14.1e100.net (142.250.70.206): icmp_seq=4 ttl=58 time=23.9 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 18.989/23.761/32.856/5.595 ms

Using the ping Command’s Quiet Mode

By default, the ping command will print out the details of every response from its ping requests.

If you only want the summary information, you can use the ping utilities “quiet” mode. With this mode active, it will only print out the initial summary and the summary when the utility stops.

To activate the quiet mode, you will need to use the “-q” option.

ping -q DESTINATION

Example of Using the ping Commands Quiet Mode

Let us test the quiet mode while making a ping request as “google.com” to show you how this works.

The following command shows you how simple it is to use this option.

ping -q google.com

Below is an example of what the data will look like after three pings have been transmitted. You can see that the command provided no information on the pings itself.

ping -q google.com
PING google.com (142.250.70.206) 56(84) bytes of data.
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 18.686/20.136/22.346/1.587 ms

Change Interval Between ping Requests

By default, the ping command has a delay of one second between each ping request that it sends. However, you can control this interval by increasing or decreasing it using a simple option.

The “-i SECONDS” option allows you to specify the delay in seconds. You can even use a decimal to set more specific timing. For example, setting half a second would be “0.5“.

ping -i SECONDS DESTINATION

Please note to set an interval that is lower than 0.2 seconds, you will be required to be running this command as a superuser.

Example of Changing the Delay Between ping Requests

For our first example, let us set the interval for the ping requests to 2 seconds. Like most of the examples on this page, we will use “google.com” as our destination.

All we need to do is use “ping“, followed by the “-i” option, our interval of 2 seconds, and finally our destination of “google.com“.

ping -i 2 google.com

Alternatively, you can use more exact numbering by utilizing decimal notation. For example, if we wanted to speed up the interval, we could set the timing to half a second.

ping -i 0.5 google.com

From using this new option, you will start to receive your ping results either faster, or more regularly.

PING google.com (142.250.70.142) 56(84) bytes of data.
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=1 ttl=58 time=19.4 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=2 ttl=58 time=18.9 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=3 ttl=58 time=24.8 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=4 ttl=58 time=19.7 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=5 ttl=58 time=20.0 ms
^C
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 18.926/20.569/24.798/2.142 ms

Specifying the Network Interface for the Ping Request

When sending a ping request, the ping command will use the default network route by default. However, if you have multiple network interfaces, you can tell the ping utility to use a specific one.

This ability is useful when testing the connection from a specific interface. To achieve this, you need to use the “-I INTERFACE” option.

ping -I INTERFACE DESTINATION

The interface can be an address, an interface name, or a VRF Name.

Example of Specifying the Network Interface for the Ping Request

We have both an ethernet (eth0) and a wireless LAN (wlan0) connection setup on our example device. As our ethernet connection is our default, we will try the ping command on our wlan0 interface.

As usual, we will perform this test with “google.com” as our destination.

ping -I wlan0 google.com

Below you can see the results we got after using the command above on our device.

PING google.com (142.250.70.142) from 192.168.0.134 wlan0: 56(84) bytes of data.
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=1 ttl=58 time=19.7 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=2 ttl=58 time=19.7 ms
^C
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 19.694/19.708/19.722/0.014 ms

One of the things you might notice is that on the first line, our network interface is referenced alongside the IP address assigned to that interface (“from 192.168.0.134 wlan0“).

Setting the Network Protocol for the Ping Requests

The ping command also allows you to specify the network protocol to use for a ping request. This is useful when you are experiencing issues with either IPv4 or IPv6 and want to force what you are testing.

To force the network protocol to IPv4, you must use the “-4” option, which utilizes the following syntax.

ping -4 DESTINATION

Alternatively, you can use the IPv6 network protocol by using the “-6” option like the syntax below.

ping -6 DESTINATION

Example of Setting the Network Protocol

For this example, we will be testing both our device’s IPv4 and IPv6 connection capabilities. In addition, we will test this against “google.com“.

For our first test, let us use our IPv4 connection to ping “google.com“.

ping -4 google.com

Below is an example of the results you would get on a working IPv4 connection.

PING google.com (142.250.70.142) 56(84) bytes of data.
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=1 ttl=58 time=19.8 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=2 ttl=58 time=19.5 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=3 ttl=58 time=19.3 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=4 ttl=58 time=19.4 ms
64 bytes from mel04s01-in-f14.1e100.net (142.250.70.142): icmp_seq=5 ttl=58 time=19.8 ms
^C
--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 19.343/19.567/19.812/0.192 ms

Now, let us test our IPv6 connection to ping “google.com“.

ping -6 google.com

As our network provider does not support the IPv6 protocol, we will run into an error that we have shown below.

ping: connect Network is unreachable

Testing a Network with a Flood using the ping Command

The ping command can flood a specified destination. Using this option is useful for testing how a network performs under stress. Please note that most servers will actively block your connection if you use this on an external source.

The ping network floods a connection by performing a request as soon as possible. The utility can perform as many as one hundred requests per second.

To perform a flood, you will need to use the “-f” option alongside the ping command. This option requires you to use a superuser account.

ping -f DESTINATION

When using the flood option, you will only see a single period (.) every time a request has been made. Every time a ping reply has been recieved it will echo a backspace character, deleting the period.

Since the flood ping performs super-fast requests, you will only ever see the period flash now and then.

Example of Sending a Flood using the ping Command

For this example, we will be performing a flood request against a local server we have set up. This device runs on the local IP address of “192.168.0.142“.

We only need to use “ping“, the “-f” option, and finally the destination, which is our local IP, “192.168.0.142“. To show you how many requests this will send in just 5 seconds, we will also use the time limit option (-w).

sudo ping -f -W 5 192.168.0.142

Below is the command result after running for only 5 seconds on our device.

PING 192.168.0.142 (192.168.0.142) 56(84) bytes of data.

--- 192.168.0.142 ping statistics ---
30306 packets transmitted, 30306 received, 0% packet loss, time 5000ms
rtt min/avg/max/mdev = 0.099/0.123/0.911/0.008 ms, ipg/ewma 0.164/0.126 ms

You can see that the ping command performed over thirty thousand ping requests in just a short time.

Conclusion

Hopefully, at this stage of the guide, you will now understand the variety of ways you can utilize the ping command.

The ping utility is one of the best ways to test a network connection for a wide variety of issues. For example, you can quickly check for packet loss, poor network performance, and more.

If you have any questions about using the ping command on Linux, please comment below.

Be sure to check out our many other Linux tutorials or Linux command guides.

Leave a Reply

Your email address will not be published. Required fields are marked *