The “ping” Command in Linux [9 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

The ping (Packet Internet Groper) command in Linux lets the user check the internet connectivity. This is widely used to find out the network and its latency. Users frequently check the internet quality with this command. In this article, I’ll discuss the ping command and its different examples.

A. Description

The ping command shows the connection latency between the host and servers. The ping command takes a URL, and IP address as arguments and shows the latency between the local machine and the server.

B. Syntax

The syntax of the ping command may look a bit difficult, as there are many options available. But actually, it’s not that difficult if you try it out.

Here is the syntax available for the ping command.

man page of ping command in linux

C. Options

There are many options available for the ping command in Linux. You may need different options for different purposes to use this command.

You can use the man command to display the manual page of the ping command.

man ping

Useful Options

  • -c COUNT (Limits Ping requests for packets after COUNT number)
  • -T (Timestamp option)
  • -w (Limits the total time)
  • -D (Print timestamps before each line)
  • -f (Floods the network with packets)
  • -i (Fixes the interval between each packet sent)
  • -q (Quiet an output, that means nothing is shown in the command line)
  • -s (Fixes the packet size sent)
  • -v (Provides verbose information)
  • -4 (Use IPv4 only)
  • -6 (Use IPv6 only)
Note: To exit from the ping command, press CTRL+C. The options in Linux CLI (Command Line Interface) are case-sensitive, so be careful while using them

Practical Examples of the “ping” Command in Linux

There are many practical uses of the ping command in Linux. Here you will find some of the most used examples of the ping command.

Example 1: Display the Localhost Latency

You can show the localhost latency using the ping command. The syntax is also really easy. Follow the steps below to try it out.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type any of the following commands in the command line to show the latency of the local host.

ping localhost

OR,

ping 0

OR,

ping 127.0.0.1

➌ Press the ENTER key to view the output.

❹ Now, press the CTRL+C key to exit the command.

Output >

As you can see in the image below the output for the command is shown and pressing CTRL+C exits the command.

ping command on local host


Similar Readings


Example 2: Display the Latency of a Website Using the “ping” Command in Linux

The ping command can be used to view the latency with a remote website. The syntax is also quite similar to the syntax of localhost. The general syntax is shown below.

ping URL

You can try the following steps to try this example.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type any of the following commands in the command line to show the latency of a remote website.

ping www.linuxsimply.com

➌ Press the ENTER key to view the output.

❹ Now, press the CTRL+C key to exit the command.

Output >

As you can see in the image below the output for the command is shown and pressing CTRL+C exits the command.

Using ping command in linux on another url

Example 3: Limiting the Ping Requests/Packets

You can limit the number of ping requests or packets using the -c option. The general syntax is in the following.

ping -c NUM URL

You can try the following steps to try this example.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type any of the following commands in the command line to limit the number of packets.

ping -c 3 linuxsimply.com

➌ Press the ENTER key.

Output >

In the Command line, you will find the command stop after the 3 requests or packets of data is transferred.

Limiting the packet number

Example 4: Setting the Interval of Packets Using the “ping” Command in Linux

You may want to keep an interval for every packet sent or received. To do that you need to set an interval using the -i option. The general syntax is as follows.

ping -i NUM URL

You can try the steps below to try it out.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type any of the following commands in the command line to show fix the interval of packets.

ping -i 3 linuxsimply.com

➌ Press the ENTER key to view the output.

❹ Now, press the CTRL+C key to exit the command.

Output >

As you can see in the image below the output for the command is shown and pressing CTRL+C exits the command.

fixing the interval of packets

Example 5: Limit the Total Time of Sending Packets Using the “ping” Command

You can also limit the total time of ping requests or packets using the -w option. The general syntax is in the following.

ping -w NUM URL

You can try it out by following the steps below.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type the following command in the command line to limit the total time to 9 seconds.

ping -w 9 linuxsimply.com

➌ Press the ENTER key.

Output >

As you can see the command exits after the 9-second run time.

Fixing the total time of ping command

Example 6: Show the Timestamps While Using the “ping” Command in Linux

Sometimes, you may want to see the timestamps of the packets while using the ping command. To do that, you need to use the -D option of the ping command.

You have to follow the steps to try it.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type the following command in the command line to show the timesteps of packets.

ping -D linuxsimply.com

➌ Press the ENTER key to view the output.

❹ Now, press the CTRL+C key to exit the command.

Output >

In the Command line, you will find the timestamps of every packet sent.

Showing Timestamps of every packet


Similar Readings


Example 7: Flooding a Host with Packets

Sometimes while checking the performance of a host, you may want to flood the host with massive amounts of packets and see how it handles that. To do that, you need to use the -f option. You also need the superuser’s permission to do that. The general syntax is as follows.

sudo ping -f URL

Follow the steps below to try it out.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type the following command in the command line to flood the host with packets.

sudo ping -f linuxsimply.com

➌ Press the ENTER.

Note: You won’t see much, except some dots in the display. This indicates the flooding of packets with the ping command

❹ Now, press the CTRL+C key to exit the command.

Output >

In the Command line, you will find the timestamps of every packet sent.

Flooding a host using ping command in lInux

Example 8: Fixing the Packet Size Using the “ping” Command in Linux

You may want to limit the packet size while using the ping command. To do that, you need to use the -s option and specify the size of the pings. Actually, you will get packet sizes a little larger than you specify. The general syntax is as follows.

ping -s SIZE URL

Follow the steps below to try it out.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type the following command in the command line to fix the packet size.

ping -s 64 linuxsimply.com
Note: The actual packet size will be a little larger than 64 bytes in this case

➌ Press the ENTER.

❹ Now, press the CTRL+C key to exit the command.

Output >

In the Command line, you will find the size of every packet sent.

Fixing size of each packet

Example 9: Specifying the IP (Internet Protocol)

You can specify the IP you want to use for the ping command using -4 for IPv4 and -6 for IPv6.

You can follow the steps below to try it out.

Steps to Follow >

➊ Open the Terminal in the Home directory.

➋ Type the following command in the command line to specify IPv4.

ping -4 linuxsimply.com

➌ Press the ENTER.

❹ Now, press the CTRL+C key to exit the command.

Output >

In the Command line, you will find the packets sent in IPv4 format.

Specifying IP address

Conclusion

In this article, you have learned about the ping command and its uses with many examples. This command is very important for working with networks and checking network latency and strength. You should try these examples in your own Linux system to get a grasp of the ping command in Linux.


Similar Readings

Rate this post
Md. Rafsan Zani

Hello, I’m Md. Rafsan Zani. I have recently completed my Undergraduate from the Bangladesh University of Engineering and Technology (BUET). Currently, I’m pursuing higher studies abroad. I’m really interested in computer science and would like to learn a lot about the wonderful world of computers. Currently, I’m working as a Linux Content Developer Executive and find Linux really interesting. I certainly would like to learn more about Linux and implement them in my future studies. Read Full Bio

Leave a Comment