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 −

Option Description
-a Audible ping.
-A Adaptive ping adjusts interval based on round-trip time.
-b Allow pinging broadcast addresses.
-B Prevents the source address from changing.
-c count Stop after sending the specified number of packets.
-D Print timestamps.
-d Enable socket debug option (Linux only).
-e identifier Define identifier for ping session, default is random for SOCK_RAW, and kernel defined for SOCK_DGRAM.
-f Flood ping, sending packets as fast as possible.
-h Display help.
-H Force reverse DNS name resolution.
-i interval Set the interval between packets (default: 1s).
-I interface Specify the network interface or source address.
-l preload Send a specified number of packets before waiting for replies.
-L Suppress loopback of multicast packets.
-n Numeric output only, no hostname resolution.
-p pattern Define a custom packet payload pattern (up to 16 bytes).
-Q tos Set Quality of Service bits in ICMP packets.
-q Quiet mode, showing only summary output.
-r Bypass routing tables, sending directly to a host.
-s packetsize Set the number of data bytes in each packet.
-S sndbuf Set the socket to send buffer size.
-t ttl Set Time-to-Live (TTL) for packets.
-T timestamp Set timestamp options (tsonly, tsandaddr, tsprespec).
-M hint Set Path MTU Discovery strategy (do, want, dont).
-v Verbose output.
-V Show version information and exit.
-w deadline Set a timeout for ping to exit, in seconds.
-W timeout Time to wait for a response before timing out.

Examples 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.

Advertisements