ping

ping

The ping command is a fundamental network utility used to test the reachability of a host on an IP network and to measure the round-trip time for messages sent from the originating host to a destination computer. It is an essential tool for network troubleshooting and diagnostics.

How ping Works

ping uses the Internet Control Message Protocol (ICMP) Echo Request and Echo Reply messages. When you issue a ping command, the following steps occur:

  1. ICMP Echo Request: The source computer sends an ICMP Echo Request message to the target host.

  2. ICMP Echo Reply: If the target host is reachable, it responds with an ICMP Echo Reply message.

  3. Round-Trip Time: The ping command measures the time between sending the Echo Request and receiving the Echo Reply, providing the round-trip time (RTT).

Basic Syntax

The basic syntax of the ping command is:

ping [options] destination
  • destination: The IP address or hostname of the target host.

Common Options

  1. Basic Ping Command:

    ping google.com

    This command sends ICMP Echo Requests to google.com indefinitely until stopped with Ctrl+C.

  2. Specify Number of Echo Requests:

    ping -c 4 google.com

    This sends exactly 4 ICMP Echo Requests to google.com.

  3. Specify Interval Between Pings:

    ping -i 0.5 google.com

    This sends ICMP Echo Requests every 0.5 seconds instead of the default 1-second interval.

  4. Specify Packet Size:

    ping -s 100 google.com

    This sends ICMP Echo Requests with 100 bytes of data.

  5. Flood Ping (Warning: High Network Load):

    sudo ping -f google.com

    This sends packets as fast as possible and requires root privileges. It is useful for stress testing but can create high network load.

  6. Ping a Specific Network Interface:

    ping -I eth0 google.com

    This sends ICMP Echo Requests using the specified network interface (eth0).

  7. Timeout for Ping Command:

    ping -w 5 google.com

    This sets a timeout of 5 seconds for the ping command.

Example Usage

  1. Basic Connectivity Test:

    ping 8.8.8.8

    This tests connectivity to Google's public DNS server.

  2. Ping a Local Host:

    ping localhost

    This tests the local network interface.

  3. Test with Detailed Output:

    ping -v google.com

    This provides verbose output, including detailed diagnostic information.

Analyzing Output

The typical output of a ping command looks like this:

PING google.com (172.217.11.142): 56 data bytes
64 bytes from 172.217.11.142: icmp_seq=0 ttl=54 time=10.4 ms
64 bytes from 172.217.11.142: icmp_seq=1 ttl=54 time=10.1 ms
64 bytes from 172.217.11.142: icmp_seq=2 ttl=54 time=10.2 ms
64 bytes from 172.217.11.142: icmp_seq=3 ttl=54 time=10.3 ms

--- google.com ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/stddev = 10.1/10.3/10.4/0.1 ms
  • icmp_seq: Sequence number of the ICMP Echo Request.

  • ttl: Time to Live, indicating the remaining number of hops.

  • time: Round-trip time in milliseconds.

  • statistics: Summary of packets transmitted, received, packet loss, and round-trip time statistics (minimum, average, maximum, and standard deviation).

Conclusion

The ping command is an invaluable tool for network troubleshooting, allowing you to verify the reachability of hosts, measure network latency, and diagnose connectivity issues. By mastering the use of ping with various options, you can effectively diagnose and resolve a wide range of network problems. For more advanced usage, consult the ping man page:

man ping

help

ping: invalid option -- '-'

Usage
  ping [options] <destination>

Options:
  <destination>      dns name or ip address
  -a                 use audible ping
  -A                 use adaptive ping
  -B                 sticky source address
  -c <count>         stop after <count> replies
  -D                 print timestamps
  -d                 use SO_DEBUG socket option
  -f                 flood ping
  -h                 print help and exit
  -I <interface>     either interface name or address
  -i <interval>      seconds between sending each packet
  -L                 suppress loopback of multicast packets
  -l <preload>       send <preload> number of packages while waiting replies
  -m <mark>          tag the packets going out
  -M <pmtud opt>     define mtu discovery, can be one of <do|dont|want>
  -n                 no dns name resolution
  -O                 report outstanding replies
  -p <pattern>       contents of padding byte
  -q                 quiet output
  -Q <tclass>        use quality of service <tclass> bits
  -s <size>          use <size> as number of data bytes to be sent
  -S <size>          use <size> as SO_SNDBUF socket option value
  -t <ttl>           define time to live
  -U                 print user-to-user latency
  -v                 verbose output
  -V                 print version and exit
  -w <deadline>      reply wait <deadline> in seconds
  -W <timeout>       time to wait for response

IPv4 options:
  -4                 use IPv4
  -b                 allow pinging broadcast
  -R                 record route
  -T <timestamp>     define timestamp, can be one of <tsonly|tsandaddr|tsprespec>

IPv6 options:
  -6                 use IPv6
  -F <flowlabel>     define flow label, default is random
  -N <nodeinfo opt>  use icmp6 node info query, try <help> as argument

For more details see ping(8).

Last updated