Linux ping command


The ping command in Linux is a network diagnostic tool used to test the reachability of a host on an IP network and to measure the round-trip time it takes for messages to be sent from the originating host to the destination and back. It uses ICMP (Internet Control Message Protocol) Echo Request and Echo Reply messages.

Basic Syntax of ping:

ping [options] destination
  • destination: The hostname or IP address of the target system you want to ping.

Common Use of the ping Command:

  • Test network connectivity: Check if a remote host is reachable on the network.
  • Measure round-trip time: Determine the time it takes for a message to travel to a remote system and back.

Basic Example of ping:

ping google.com
  • This will send ICMP Echo Requests to google.com and wait for Echo Replies. The output will show the round-trip time (RTT) for each packet.

Sample Output:

PING google.com (142.250.72.14) 56(84) bytes of data. 64 bytes from 142.250.72.14: icmp_seq=1 ttl=56 time=14.3 ms 64 bytes from 142.250.72.14: icmp_seq=2 ttl=56 time=13.7 ms 64 bytes from 142.250.72.14: icmp_seq=3 ttl=56 time=14.1 ms
  • icmp_seq=1: The sequence number of the ICMP packet.
  • ttl=56: The "time to live" value, indicating how many hops the packet can take before being discarded.
  • time=14.3 ms: The round-trip time (RTT) in milliseconds.

Commonly Used Options with ping

1. -c (Count): Send a specific number of packets

  • By default, ping sends an infinite number of packets unless stopped manually. To send a specific number of packets, use the -c option:
ping -c 5 google.com
  • Explanation: This sends 5 packets to google.com and then stops.
  • Sample Output:
    PING google.com (142.250.72.14) 56(84) bytes of data. 64 bytes from 142.250.72.14: icmp_seq=1 ttl=56 time=14.2 ms 64 bytes from 142.250.72.14: icmp_seq=2 ttl=56 time=13.7 ms 64 bytes from 142.250.72.14: icmp_seq=3 ttl=56 time=14.1 ms 64 bytes from 142.250.72.14: icmp_seq=4 ttl=56 time=14.0 ms 64 bytes from 142.250.72.14: icmp_seq=5 ttl=56 time=14.3 ms --- google.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4004ms rtt min/avg/max/mdev = 13.737/14.004/14.343/0.216 ms

2. -i (Interval): Set the interval between sending each packet

  • The default interval is 1 second. You can change this with the -i option to send packets more frequently or at a different interval:
ping -i 0.5 google.com
  • Explanation: This sends a packet every 0.5 seconds.

3. -t (Time to Live): Set the TTL (Time to Live)

  • The TTL value determines how many hops a packet can make before being discarded. This can be useful for tracing network routes or troubleshooting network issues:
ping -t 64 google.com
  • Explanation: This sets the TTL to 64 for the packets.

4. -s (Packet Size): Specify the packet size

  • The default packet size is 56 bytes of data (64 bytes including the ICMP header). You can change this to send larger or smaller packets:
ping -s 1000 google.com
  • Explanation: This sends 1000-byte packets to the target.

5. -a (Audible): Make a sound when a reply is received

  • This option causes the terminal to make a sound whenever a reply is received:
ping -a google.com

6. -q (Quiet mode): Only show summary output

  • By default, ping shows output for each packet sent and received. The -q option suppresses the output, showing only a summary when the command completes:
ping -q -c 5 google.com
  • Sample Output:
    --- google.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4004ms rtt min/avg/max/mdev = 13.737/14.004/14.343/0.216 ms

7. -W (Timeout): Set the timeout for each reply

  • The -W option specifies the time to wait for a response in seconds. The default is typically 5 seconds.
ping -W 2 google.com
  • Explanation: This sets a 2-second timeout for waiting for a response.

8. -n (Numeric): Show numeric IP addresses instead of resolving hostnames

  • By default, ping tries to resolve hostnames. If you want to show numeric IP addresses instead, use -n:
ping -n google.com
  • Sample Output:
    PING 142.250.72.14 (142.250.72.14) 56(84) bytes of data.

9. -v (Verbose): Enable verbose output

  • This increases the level of detail in the output of ping. It can provide additional diagnostic information:
ping -v google.com

10. -R (Record Route): Record the route of the packet

  • This option records the route that the packet takes to the destination. This is useful for tracing the network path:
ping -R google.com

Summary of Common ping Options:

OptionDescription
-c [count]Send a specific number of packets (e.g., ping -c 5 google.com)
-i [interval]Set interval between packets (in seconds)
-t [ttl]Set the TTL (Time to Live) value for packets
-s [size]Set packet size (in bytes)
-aMake a sound when a reply is received
-qQuiet mode, show summary output only
-W [timeout]Set the timeout in seconds for each reply
-nShow numeric IP addresses, no DNS resolution
-vEnable verbose output
-RRecord the route of the packets

Example Scenarios Using ping:

  1. Test if a host is reachable:

    ping google.com
  2. Send 10 packets and show only the summary:

    ping -c 10 -q google.com
  3. Send packets with a custom packet size:

    ping -s 1500 google.com
  4. Test connectivity with a smaller interval:

    ping -i 0.2 google.com

The ping command is a simple yet essential tool for network troubleshooting, helping to identify if a network host is reachable and measuring network performance based on round-trip times. It is often used to diagnose network issues and verify the status of a network connection.