Ping is a diagnosing tool which allows to check the connectivity of any IP address by sending ICMP packets designed to report errors and operational information. This tool is present in all OS (Operating Systems) and it is perhaps the most basic command to interact with a network or devices through it.

Ping uses ranges from device availability check to more complex security auditing being the basis of many methods available on different security scanners such as Nmap, Angry IP Scanner, etc.

This tutorial explains briefly how this command works and how to use it applying flags to customize the “communication” between devices.

Contrary to Windows under Linux the ping is continuous (like ping -t in Windows), this tutorial will show examples for ping changing packet sizes and intervals to accelerate or slow the packets, how to add sound when replies are received,

Introduction to the Internet Protocol and ICMP ( Internet Control Message Protocol)

The following text extract belongs to the ping command man page:

“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.”

(Source: https://linux.die.net/man/8/ping)

What we call ping is an ECHO_REQUEST, or a ECHO_RESPONSE packet.

These are packets whose datagram contains IP and ICMP packets in the header.

The ECHO_REQUEST will seek a reply, the ECHO_RESPONSE to confirm the destination is online.

“…the protocol ICMP (Internet Control Message Protocol) also belongs to the Internet Layer to diagnose and report errors in the communication. Despite the ICMP protocol belongs to the Internet Layer the reliability of the connection relies on the third level layer, the Transport layer.” (Source https://linuxhint.com/nmap_basics_tutorial/#a1)

All About Linux Ping Command

A simple ping can be executed without flags defining only the target, the following example shows a ping to Google’s DNS, pinging Google DNS is one of the most popular ways to check if you have internet connection. If you can ping the 8.8.8.8 IP address but you can’t ping domain names, you should probably add the “nameserver 8.8.8.8” line within your /etc/resolv.conf file.

As you can see in the output, the device pinging is receiving 64 bytes from the IP 8.8.8.8, with the sequence of ICMP packets increase, Time to Live and time to reach the destination in milliseconds (ms).

You can specify the size in bytes of the packets with the -s flag and you can also ping targets by using a domain name instead of the IP address as in the example below:

The -a flag will produce a sound (beep) when getting a reply from the destination Gigopen.

The option above may be  good when the host is unreachable and you want to keep it pinging until it becomes online.

The -i flag allows to set intervals in seconds, the following example shows pings each 7 seconds.

While the example above shows how to define a 7 seconds interval, you can speed up pings by running:

# ping -i 0.5 gigopen.com

Since under Linux the ping tool runs continuously (like “ping -t” in Windows) you can specify a deadline by adding the flags -w and -W.

The following example with low case -w determines an unconditional deadline for the ping process:

# ping -w 2 linuxhint.com

The big case -W will instruct ping to timeout only if replies are not being received, then the -W instructs the time to wait for a reply, in the following example there are replies, therefore the timeout should not be functional:

# ping -W 2 linuxhint.com

As you see the operation was interrupted by a CTRL C.

The -r flag is used to ping directly the destination without checking the routing table. The routing table is information usually saved in routing devices such as routers or gateways containing information on the path packets should follow to reach the destination. The hop-by-hop routing way gives the path to the next routing device in a manner packets always must check for the routing table on each routing device to go to the next device (or hop). Using the -r flag the packet will go directly to the specified address regardless of the routing table, if used with a device out of our network it will return error, it may work within the same network as in the examples shown below:

As you can see the ping against linuxhint.com didn’t reach the destination while it worked for a local network IP  address.

Some of ping flags in Linux include:

[-aAbBdDfhLnOqrRUvV] [-c count] [-i interval] [-I interface] [-l preload] [-m mark] [-M pmtudisc_option] [-N nodeinfo_option] [-p pattern] [-Q tclass] [-s packetsize]

 [-S sndbuf] [-t ttl] [-T timestamp_option] [-w deadline] [-W timeout].

Note: flags for other OS (Operating Systems) such as Windows variate and are not the same.

You can get more information on the ping command at the man page (https://linux.die.net/man/8/ping).

I hope you found this brief article on All About Linux Ping Command useful.

Keep following LinuxHint for additional updates and tips on Linux and Networking.

About the author

Ivan Vanney

Ivan Vanney has over 2 years as writer for LinuxHint, he is co-founder of the freelance services marketplace GIGopen.com where he works as a sysadmin.