Linux ping Command Examples

Filed Under: UNIX/Linux

The ping (Packet INternet Groper) command is one of the most widely used utility across different Operating systems: from Windows to Linux.

It’s a network troubleshooting tool used for testing reachability of remote systems, servers, and network devices.

It achieves this by sending an ICMP echo request to a remote system. The ICMP packet requests are received and relayed back to the source indicating that the host is up.

In this guide, we will look at how it is used and the various options that can be passed to achieve different results.

Linux ping Command Syntax

The syntax of the Linux ping command is quite simple and straight forward.

ping [option] [hostname or IP address]

Using the ping command to get the IP address of a host

We can use the ping command to find out the IP address of a website. The ping command output prints the IP address of the host.


$ ping journaldev.com
PING journaldev.com (45.33.45.237): 56 data bytes
64 bytes from 45.33.45.237: icmp_seq=0 ttl=56 time=59.133 ms
64 bytes from 45.33.45.237: icmp_seq=1 ttl=56 time=43.917 ms

Using the ping command to test connectivity or reachability of a system

The most basic usage of ping command involves sending a ping request to a website address or hostname as shown.

ping google.com

Sample output

Basic ping command example

Alternatively, you can ping a server by specifying its IP address as shown.

ping 173.82.2.236

Sample output

Ping An IP address  With Ping Command

It’s important to note that in the above examples, the ping command will continue sending ping requests until you press CRTL + C.

Ping command uses DNS resolver to find out the IP address of the host and then sends the ping request. If the hostname is invalid, it will return “Unknown host” error.


$ ping google
ping: cannot resolve google: Unknown host
$ ping sasadsasdd.com
ping: cannot resolve sasadsasdd.com: Unknown host
$ 

Specify time interval between ping requests

There’s a 1-second gap between ping requests by default. If you want to modify this and specify a higher value, use the -i argument followed by the time interval as shown.

ping -i 3 google.com

Sample output

Set Time Interval Between Ping Packets

In the example above the time interval between ping packets is 3 seconds.

Modify the ping packet size

The number of bytes contained in a ping request is 56 by default ( 64 bytes if you include the ping header). You can, however, alter this value to your preference by using the -s option followed by the value. To change the value to 80, execute the command.

ping -s 80 google.com

Sample output

Set Ping Packet Size

Specify number of time to send the ping packet size

As you have observed in previous examples, you need to hit CTRL + C to interrupt the sending of ping packets. To avoid this inconvenience, you can specify the number of packets to be sent using the -c flag. For instance, to send 5 ping packets, run the below command.

ping -c 5 google.com

Sample output

Set Number Of  Packets

The above command sends 5 ping packets to the target and finally stops.

Flood a target system

Yes, You are probably skeptical about this, but ping command can also be used to flood a target. You can achieve this using the -f command.

ping -f jaykiarie.com

Sample output

Send A Flood Of Packets

DISCLAIMER: Caution should be taken as flooding a target system with ping requests can lead to a DOS attack which can degrade a system’s reachability or connectivity.

Print timestamp of ping requests

If you wish, you can print the timestamp during which the ping packets are sent. This is achieved using the -D option. The timestamp is a combination of Unix time and microseconds.

Sample output
Print Timestamp In Ping

Conclusion

As we have seen, ping is a very useful command for network troubleshooting and helps System administrators and regular users to diagnose network problems involving connectivity between systems. You can run “man ping” command to check out all the options ping command provides.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages