ping Command in Linux



The ping command in Linux checks the network connectivity between devices by sending ICMP Echo Request packets. ECHO_REQUESTS are ICMP (Internet Control Message Protocol) packets used for network diagnostics. It helps diagnose network issues, measure latency, and determine packet loss.

Table of Contents

Here is a comprehensive guide to the options available with the ping command −

Syntax of ping Command

The syntax of the ping command in Linux is as follows −

ping [options] [destination]

In the above syntax, the [options] field is used to specify various options to change the output response. The [destination] field is used to specify the IP address or hostname.

ping Command Options

The options of the ping command are listed below −

OptionDescription
-aAudible ping.
-AAdaptive ping adjusts interval based on round-trip time.
-bAllow pinging broadcast addresses.
-BPrevents the source address from changing.
-c countStop after sending the specified number of packets.
-DPrint timestamps.
-dEnable socket debug option (Linux only).
-e identifierDefine identifier for ping session, default is random for SOCK_RAW, and kernel defined for SOCK_DGRAM.
-fFlood ping, sending packets as fast as possible.
-hDisplay help.
-HForce reverse DNS name resolution.
-i intervalSet the interval between packets (default: 1s).
-I interfaceSpecify the network interface or source address.
-l preloadSend a specified number of packets before waiting for replies.
-LSuppress loopback of multicast packets.
-nNumeric output only, no hostname resolution.
-p patternDefine a custom packet payload pattern (up to 16 bytes).
-Q tosSet Quality of Service bits in ICMP packets.
-qQuiet mode, showing only summary output.
-rBypass routing tables, sending directly to a host.
-s packetsizeSet the number of data bytes in each packet.
-S sndbufSet the socket to send buffer size.
-t ttlSet Time-to-Live (TTL) for packets.
-T timestampSet timestamp options (tsonly, tsandaddr, tsprespec).
-M hintSet Path MTU Discovery strategy (do, want, dont).
-vVerbose output.
-VShow version information and exit.
-w deadlineSet a timeout for ping to exit, in seconds.
-W timeoutTime to wait for a response before timing out.

Examples of ping Command in Linux

This section provides examples of how to use the ping command in Linux.

Pinging an IPv4

To ping an IPv4 address, use the ping command in the following way −

ping 74.125.195.102
ping Command in Linux1

To stop the ping command, press the CTRL+C buttons.

Pinging a Hostname

To ping an IPv4-enabled domain, use the following command −

ping www.google.com
ping Command in Linux2

To stop the ping command, press the CTRL+C buttons.

Making Ping Response Audible

To make the ping output audible, use the -a option −

ping -a 74.125.195.102

The system emits a sound whenever a response is received.

Pinging Local Host

To check the localhost network, there are multiple ways −

ping 0
ping Command in Linux3

Or specifying the localhost IPv4 address −

ping 127.0.0.1

Or mentioning the hostname −

ping localhost

Sending a Specific Number of Packets

To send a specific number of packets, use the -c option. For example, to send 3 packets, use the following command −

ping -c 3 74.125.195.102
ping Command in Linux4

Sending Packets after a Specific Interval

To send packets after a specific interval, use the -i option. For example, to send a packet after every 2 seconds, use the following command −

ping -i 2 74.125.195.102

Executing Flood Ping

To flood the packets as fast as possible, use the -f option −

sudo ping -f 74.125.195.102
ping Command in Linux5

Executing the above command requires superuser access.

The above command keeps flooding the destination with requests, to stop it, press the CTRL+C keys.

Pinging in a Quiet Mode

To ping in the quiet mode, use the -q option −

ping -q -c 3 74.125.195.102
ping Command in Linux6

The above command will display the result summary only.

Adding Timestamps before Each Ping

To add timestamps before each ping output, use the -D option with the ping command −

ping -D 74.125.195.102
ping Command in Linux7

Specifying the Packet Size

To specify the payload size, use the -s option. For example, to send a payload of 1024 bytes, use the ping command in the following way −

ping -s 1024 74.125.195.102
ping Command in Linux8

Setting Timeout

To set a time after which the ping stops, use the -w option. For example, to stop pinging after 5 seconds, use the following command −

ping -w 5 74.125.195.102
ping Command in Linux9

Setting Wait Time for Response

To set the waiting time for response, use the -W option.

ping -c 3 -W 3 74.125.195.102

The above command waits up to 3 seconds for a reply before considering it lost.

Displaying Usage Help

To display the usage help of the ping command, use the -h option −

ping -h

Conclusion

The ping command in Linux is a useful tool for checking network connectivity, diagnosing issues, measuring latency, and detecting packet loss. It sends ICMP Echo Request packets to a specified destination and waits for a response.

Various options allow customization, such as setting the number of packets, adjusting intervals, making responses audible, or running in quiet mode. Practical examples demonstrate how to use these options effectively.