Example uses of the command "ping"

An Introductory Tutorial

Network Handshake
Network Handshake.

Introduction

According to the manual page the Linux "ping" command uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host of gateway.

The manual page uses a lot of technical terms but all you need to know is that the Linux "ping" command can be used to test whether a network is available and the amount of time it takes to send and get a response from the network.

Why Would You Use The "ping" Command

Most of us visit the same useful sites regularly. For instance I visit the BBC website to read the news and I visit the Sky Sports website to get the football news and results. You will undoubtedly have your own set of key sites such as Lifewire.

Imagine you entered the web address for Lifewire.com into your browser and the page did not load at all. The cause of this can be one of many things.

For instance you might not have an internet connection at all even though you are connected to your router. Sometimes the internet service provider has localised issues which prevents you from using the internet.

Another reason might be that the site is genuinely down and unavailable.

Whatever the reason you can easily check the connectivity between your computer and another network using the "ping" command.

How Does The Ping Command Work

When you use your phone you dial a number (or more commonly nowadays pick their name from an address book on your phone) and the phone rings at the receiver's end.

When that person answers the phone and says "hello" you know you have a connection.

The "ping" command works in a similar way. You specify the IP address which is the equivalent to a phone number or a web address (the name associated with the IP address) and "ping" sends off a request to that address.

When the receiving network receives the request it will send back a response which is basically saying "hello".

The time taken for the network to respond is called the latency.

Example Use Of The "ping" Command

To test whether a website is available type "ping" followed by the name of the site you wish to connect to. For example to ping Lifewire.com you would run the following command:

ping lifewire.com

The ping command continuously sends requests to the network and when a response is received you will receive a ling of output with the following information:

  • number of bytes received
  • the IP address
  • a sequence number
  • the time taken to respond

If the network you are trying to ping doesn't respond because it is unavailable then you will be notified of this.

If you know the IP address of the network you can use this in place of the website name:

ping 151.101.65.121

Get An Audible "ping"

You can get the ping command to make a noise whenever a response is returned by using the "-a" switch as part of the command as shown in the following command:

ping -a lifewire.com

Return The IPv4 Or IPv6 Address

IPv6 is the next generation protocol for assigning network addresses as it provides more unique possible combinations and it is due to replace the IPv4 protocol in the future.

The IPv4 protocol assigns IP addresses in the way we are currently used to. (For example 151.101.65.121).

The IPv6 protocol assigns IP addresses in the format [fe80::51c1::a14b::8dec%12].

If you want to return the IPv4 format of the network address you can use the following command:

ping -4 lifewire.com

To use the IPv6 only format you can use the following command:

ping -6 lifewire.com

Limit The Amount Of Pings

By default when you ping a network it continues to do so until you press CTRL and C at the same time to end the process.

Unless you are testing the network speed you will probably only want to ping until you receive a response.

You can limit the number of attempts by using the "-c" switch as follows:

ping -c 4 lifewire.com

What happens here is that the request in the above command is sent 4 times. The result is you might get 4 packets sent and only 1 reply.

Another thing you can do is set a deadline of how long to run the ping command by using the "-w" switch.

ping -w 10 lifewire.com

This sets a deadline for the ping to last for 10 seconds. 

What is interesting about running the commands in this way is the output as it shows how many packets were sent and how many received.

If 10 packets were sent and only 9 were received back then that amounts to a 10% packet loss. The higher the loss the worse the connection.

You can use another switch which floods the number of requests to the receiving network. For every packet sent a dot is displayed on the screen and every time the network responds the dot is taken away. Using this method you can see visually how many packets are getting lost.

You need to be a super user to run this command and it really is for network monitoring purposes only.

sudo ping -f lifewire.com

The opposite of flooding is to specify a longer interval between each request. To do this you can use the "-i" switch as follows:

ping -i 4 lifewire.com

The above command will ping lifewire.com every 4 seconds.

How To Suppress Output

You might not care about all the stuff that happens between each request sent and received but just the output at the beginning and end.

For instance if you sent the following command using the "-q" switch you will receive a message stating the IP address being pinged and at the end the number of packets sent, received and the packet loss without every intervening line repeated.

ping -q -w 10 lifewire.com

Summary

The ping command has a few other options which can be found by reading the manual page.

To read the manual page run the following command:

man ping