Linux ping Command Tutorial for Beginners (8 Examples)

Regardless of the type of Linux user you are (beginner or pro), there are certain tools that you must be aware of. One such utility is Ping. In this tutorial, we will discuss the basics of this tool using some easy to understand examples. But before we do that, it's worth mentioning that all examples in the article here have been tested on an Ubuntu 16.04 LTS machine.

Linux ping command

In most basic terms, the ping command lets you check whether or not a remote host is alive and responding. Following is the tool's syntax:

ping [OPTIONS] destination

And here's how the man page explains it:

       ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit
       an ICMP ECHO_RESPONSE from a host or gateway.   ECHO_REQUEST  datagrams
       (``pings'')  have  an  IP and ICMP header, followed by a struct timeval
       and then an arbitrary number of ``pad'' bytes  used  to  fill  out  the
       packet.

       ping6  is  IPv6  version  of  ping,  and can also send Node Information
       Queries (RFC4620).  Intermediate hops may not be allowed, because  IPv6
       source routing was deprecated (RFC5095).

Following are some Q&A-styled examples that should give you a good idea on how the ping command works.

Q1. How to use ping command?

Basic usage is fairly easy - just execute the 'ping' command with destination as input.

For example:

ping howtoforge.com

Following is the result of this command produced on my system:

How to use ping command

Note that by default, the command will continue executing until you stop it through Ctrl+C.

Q2. How to change time interval between ping packets?

By default, there's a gap of one second between ping packets. However, if you want, you can customize this delay using the -i command line option.

For example, the following command makes sure there's a gap of 3 seconds:

ping -i 3 howtoforge.com

And the following command sends packets with a delay of half-a-second.

ping -i 0.5 howtoforge.com

Note that only super-user may set interval to values less 0.2 seconds

Q3. How to change ping packet size?

By default, the number of bytes in a ping packet is 56 (64 if you include 8 bytes of ICMP header). However, if you want, you change this value, something which you can do using the -s command line option.

For example, to send an 80 byte packet (88 including ICMP header), use the following command:

ping -s 80 howtoforge.com

How to change ping packet size

Q4. How to make ping send set number of packets?

The default behavior of ping is to keep sending packets until interrupted by the user. However, if you want, you can force ping to stop executing after sending a set number of packets. This you can do using the -c command line option.

For example, the following command will send 3 packets and then stop.

ping -c 3 howtoforge.com

Q5. How to launch a flood of packets using ping?

Yes, the ping command also offers an option to launch a flood of packets. This you can do using the -f command line option.

ping -f howtoforge.com

Here's what the official docs say about this option:

              For  every  ECHO_REQUEST  sent  a  period  ``.''  is
              printed,  while  for  ever  ECHO_REPLY  received  a backspace is
              printed.  This provides a rapid display of how many packets  are
              being  dropped.   If  interval is not given, it sets interval to
              zero and outputs packets as fast as they come back or  one  hun?
              dred  times  per second, whichever is more.  Only the super-user
              may use this option with zero interval.

Q6. How to make ping print timestamp before each line?

There's an option (-D) that lets you do this. The timestamp that's printed is a combination of unix time and microseconds (as in gettimeofday).

ping -D howtoforge.com

Here's a screenshot of this option in action:

How to make ping print timestamp before each line

Q7. How to set a hard timeout for ping?

You can specify a time deadline for ping to exit. This you can do using the -w command line option that requires a numeric value representing number of seconds.

For example, the following ping command will stop after 3 seconds:

ping -w 3 howtoforge.com

Here's how the official docs explain this option:

              Specify  a  timeout, in seconds, before ping exits regardless of
              how many packets have been sent or received. In this  case  ping
              does  not  stop after count packet are sent, it waits either for
              deadline expire or until count probes are answered or  for  some
              error notification from network.

Q8. How to set a soft timeout for ping?

While the -w option makes sure ping stops in any case after the deadline has expired, there's another option (-W, in caps) that also makes ping stop but only when there's no response from the destination side.

ping -W 3 howtoforge.com

Here's how the man page explains the behavior in this case:

              Time to wait for a response, in seconds. The option affects only
              timeout in absence of any responses, otherwise  ping  waits  for
              two RTTs.

Conclusion

Ping is an important tool that almost certainly used in debugging network-related issues. In this tutorial, we discussed some main features (command line options) of this utility. Practice these, and once you're done, head to the tool's man page to know more about it.

Share this page:

0 Comment(s)