ping command in Linux with Examples



Name

ping - send ICMP ECHO_REQUEST to network hosts

Synopsis

ping [-aAbBdDfhLnOqrRUvV] [-c  count] [-F  flowlabel] [-i interval] [-I interface] [-l preload] [-m mark] [-M pmtudisc_option] [-N nodeinfo_option] [-w deadline] [-W  timeout] [-p  pattern] [-Q  tos]  [-s packetsize] [-S sndbuf] [-t ttl] [-T timestamp option] [hop ...] destination

Description

ping uses the ICMP protocol's mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway. ECHO_REQUEST datagrams 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.

Ping command help you to multiple things. It allows you to test your internet connection. It can check if a remote machine is online. It can analyze if there are network issues, such as dropped packages or high latency.

Options

The options for ping commands are:

-a   Audible ping.

-A   Adaptive ping. Interpacket interval adapts to round-trip time, so that effectively not more than one (or more, if preload is set) unanswered probe is present in the network. Minimal interval is 200msec for not super-user. On networks with low rtt this mode is essentially equivalent to flood mode.

-b   Allow pinging a broadcast address.

-B   Do not allow ping to change source address of probes. The address is bound to one selected when ping starts.

-c   count
     Stop after sending count ECHO_REQUEST packets. With deadline option, ping waits for count ECHO_REPLY packets, until the timeout expires.

-d   Set the SO_DEBUG option on the socket being used. Essentially, this socket option is not used by Linux kernel.

-D   Print timestamp (unix time + microseconds as in gettimeofday) before each line.

-f   Flood ping. For every ECHO_REQUEST sent a period ``.'' is printed, while for ever ECHO_REPLY received a backspace is printed. This provides a rapid display of how many packets are being dropped. If interval is not given, it sets interval to zero and outputs packets as fast as they come back or one hundred times per second, whichever is more. Only the super-user may use this option with zero inter val.

-F   flow label
     ping6 only. Allocate and set 20 bit flow label (in hex) on echo request packets. If value is zero, kernel allocates random flow label.

-h   Show help.

-i   interval
     Wait interval seconds between sending each packet. The default is to wait for one second between each packet normally, or not to wait in flood mode. Only super-user may set interval to values less 0.2 seconds.

-I   interface
     interface is either an address, or an interface name. If interface is an address, it sets source address to specified interface address. If interface in an interface name, it sets source interface to specified interface. For ping6, when doing ping to a link-local scope address, link specification (by the '%'-notation in destination, or by this option) is required.

-l   preload
     If preload is specified, ping sends that many packets not waiting for reply. Only the super-user may select preload more than 3.

-L   Suppress loopback of multicast packets. This flag only applies if the ping desti nation is a multicast address.

-m   mark
     use mark to tag the packets going out. This is useful for variety of reasons within the kernel such as using policy routing to select specific outbound processing.

-n   Numeric output only. No attempt will be made to lookup symbolic names for host addresses.

-O   Report outstanding ICMP ECHO reply before sending next packet.   This is useful together with the timestamp -D to log output to a diagnostic file and search for missing answers.

-p   pattern
     You may specify up to 16 ``pad'' bytes to fill out the packet you send. This is useful for diagnosing data-dependent problems in a network. For example, -p ff will cause the sent packet to be filled with all ones.

-q   Quiet output. Nothing is displayed except the summary lines at startup time and when finished.

-Q   tos Set Quality of Service -related bits in ICMP datagrams. tos can be decimal (ping only) or hex number.

-s   packetsize
     Specifies the number of data bytes to be sent. The default is 56, which trans lates into 64 ICMP data bytes when combined with the 8 bytes of ICMP header data.

-S   sndbuf
     Set socket sndbuf. If not specified, it is selected to buffer not  more than one packet.

-t   ttl ping only. Set the IP Time to Live.

-U   Print full user-to-user latency (the old behaviour). Normally ping prints network round trip time, which can be different f.e. due to DNS failures.

-v   Verbose output.

-V   Show version and exit.

-w   deadline
     Specify a timeout, in seconds, before ping exits regardless of  how  many packets have been sent or received. In this case ping does not stop after count packet are sent, it waits either for deadline expire or until count probes are answered or for some error notification from network.

-W   timeout
     Time to wait for a response, in seconds. The option affects only timeout in absence of any responses, otherwise ping waits for two RTTs.

Examples

1. We can run ping command to check if the target host www.google.com is reachable or not. We can also use an IP address instead of domain name.

$ ping google.com
PING google.com(del11s13-in-x0e.1e100.net (2404:6800:4002:818::200e)) 56 data bytes
64 bytes from del11s13-in-x0e.1e100.net (2404:6800:4002:818::200e): icmp_seq=1 ttl=115 time=52.8 ms
64 bytes from del11s13-in-x0e.1e100.net (2404:6800:4002:818::200e): icmp_seq=2 ttl=115 time=50.9 ms
64 bytes from del11s13-in-x0e.1e100.net (2404:6800:4002:818::200e): icmp_seq=3 ttl=115 time=69.9 ms
64 bytes from del11s13-in-x0e.1e100.net (2404:6800:4002:818::200e): icmp_seq=4 ttl=115 time=68.1 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 50.887/60.429/69.943/8.648 ms

The above command shows the IP address in IPv6 format (as eight groups of four hexadecimal digits). Below we show an example, where IP address is in IPv4 format.

$ ping yahoo.com
PING yahoo.com (74.6.231.21) 56(84) bytes of data.
64 bytes from media-router-fp74.prod.media.vip.ne1.yahoo.com (74.6.231.21): icmp_seq=1 ttl=48 time=243 ms
64 bytes from media-router-fp74.prod.media.vip.ne1.yahoo.com (74.6.231.21): icmp_seq=2 ttl=48 time=247 ms
64 bytes from media-router-fp74.prod.media.vip.ne1.yahoo.com (74.6.231.21): icmp_seq=3 ttl=48 time=241 ms
64 bytes from media-router-fp74.prod.media.vip.ne1.yahoo.com (74.6.231.21): icmp_seq=4 ttl=48 time=237 ms
^C
--- yahoo.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3000ms
rtt min/avg/max/mdev = 237.399/242.439/247.046/3.525 ms

The ping command resolves the domain name into an IP address and starts sending ICMP packages to the destination IP, in this case google.com. If the destination IP is reachable it will respond back and the ping command prints a line that includes the following fields:

  • The number of data bytes. The default is 56, which translates into 64 ICMP data bytes - 64 bytes.
  • The IP address of the destination - from media-router-fp74.prod.media.vip.ne1.yahoo.com (74.6.231.21).
  • The ICMP sequence number for each packet. icmp_seq=1.
  • The Time to Live. i.e., ttl=48
  • The ping time, measured in milliseconds which is the round trip time for the packet to reach the host, and the response to return to the sender. - time=243 ms.
  • By default, the interval between sending a new packet is one second.

2. To force ping command to use IPv4 address, we can use the -4 option, or use its alias ping4. This option is not supported on older versions.

$ ping -4 google.com
PING google.com (172.217.167.46) 56(84) bytes of data.
64 bytes from del03s16-in-f14.1e100.net (172.217.167.46): icmp_seq=1 ttl=110 time=40.4 ms
64 bytes from del03s16-in-f14.1e100.net (172.217.167.46): icmp_seq=2 ttl=110 time=56.4 ms
64 bytes from del03s16-in-f14.1e100.net (172.217.167.46): icmp_seq=3 ttl=110 time=45.7 ms
64 bytes from del03s16-in-f14.1e100.net (172.217.167.46): icmp_seq=4 ttl=110 time=52.7 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 40.354/48.800/56.360/6.191 ms

3. To define the number of Echo Request to be sent after which ping will exit, we can use the -c option. You can see below that we do not need to stop the ping command, it exists after it has sent specified number of packets.

$ ping -c 5 google.com
PING google.com(del11s12-in-x0e.1e100.net (2404:6800:4002:817::200e)) 56 data bytes
64 bytes from del11s12-in-x0e.1e100.net (2404:6800:4002:817::200e): icmp_seq=1 ttl=115 time=71.0 ms
64 bytes from del11s12-in-x0e.1e100.net (2404:6800:4002:817::200e): icmp_seq=2 ttl=115 time=59.0 ms
64 bytes from del11s12-in-x0e.1e100.net (2404:6800:4002:817::200e): icmp_seq=3 ttl=115 time=63.1 ms
64 bytes from del11s12-in-x0e.1e100.net (2404:6800:4002:817::200e): icmp_seq=4 ttl=115 time=55.1 ms
64 bytes from del11s12-in-x0e.1e100.net (2404:6800:4002:817::200e): icmp_seq=5 ttl=115 time=48.1 ms

--- google.com ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4005ms
rtt min/avg/max/mdev = 48.115/59.267/70.968/7.663 ms

4. By default interval between each ping request is one second. We can increase or decrease that time using the -i option.

$ ping -i .5 google.com
PING google.com (142.250.194.238) 56(84) bytes of data.
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=1 ttl=117 time=3.00 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=2 ttl=117 time=2.58 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=3 ttl=117 time=2.54 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 2.548/2.710/3.002/0.206 ms

5. We can define the packet size of the payload, in number of bytes using the -s option, which will result in a total packet size of value provided plus 8 extra bytes for the ICMP header.

$ ping -s 60 google.com
PING google.com (142.250.194.238) 60(88) bytes of data.
68 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=1 ttl=117 time=4.73 ms
68 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=2 ttl=117 time=6.49 ms
68 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=3 ttl=117 time=7.12 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 4.735/6.116/7.121/1.013 ms

6. We can use -q option to supress the output display (called quiet option), it prints one line with the regular ping information and then provides the summary at the end, when finished.

$ ping -q ebhashasetu.com
PING ebhashasetu.com (166.62.28.105) 56(84) bytes of data.
^C
--- ebhashasetu.com ping statistics ---
5 packets transmitted, 4 received, 20% packet loss, time 4002ms
rtt min/avg/max/mdev = 83.762/86.837/93.530/3.975 ms

7. To specify a deadline for ping command output we can use -w option to specify a deadline. This option specifies a timeout, in seconds. After this timeout has expired ping command exits regardless of how many packets have been sent or received. In this case ping does not stop after count packet are sent, it waits either for deadline expire or until count probes are answered or for some error notification from network.

$ ping -i 0.5 -w 3 google.com
PING google.com (142.250.194.238) 56(84) bytes of data.
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=1 ttl=117 time=1.87 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=2 ttl=117 time=1.86 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=3 ttl=117 time=1.69 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=4 ttl=117 time=1.90 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=5 ttl=117 time=1.75 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=6 ttl=117 time=1.68 ms

--- google.com ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 2506ms
rtt min/avg/max/mdev = 1.683/1.794/1.906/0.101 ms

8. The default behavior of the ping command is to send ICMP packages via the default route. If we have multiple interfaces on our machine we can specify the source interface using -I option. The following command will ping google.com using enp0s25 as a source interface:

$ ping -I enp0s25 google.com
PING google.com (142.250.194.238) from 192.168.0.230 enp0s25: 56(84) bytes of data.
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=1 ttl=117 time=2.39 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=2 ttl=117 time=1.91 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=3 ttl=117 time=1.66 ms
64 bytes from del12s08-in-f14.1e100.net (142.250.194.238): icmp_seq=4 ttl=117 time=1.74 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 1.664/1.930/2.392/0.281 ms
Advertisements