Networking and remote
ping
Send ICMP echo requests to a host and report replies and round-trip times, to test reachability and latency.
ping [OPTION...] HOST
ping host sends a small ICMP "echo request" once a second and prints each "echo reply" with
its round-trip time. On Linux it keeps going until you press Ctrl-C, then prints a summary:
packets sent and received, the loss percentage, and the minimum, average, maximum and
deviation of the times. -c N stops after N packets, which is what you want in scripts and
usually at the prompt too.
A reply proves the host is up and the network path works in both directions. No reply proves
less: many firewalls drop ICMP, so a server can be serving web pages while ignoring ping. To
test a specific service, connect to its port with nc -z or curl.
The sandbox has no network beyond loopback, so these examples ping 127.0.0.1 and
localhost. The output format is the same for any host; real round trips are milliseconds
instead of microseconds.
Examples
Send a fixed number of pings
ping -c 3 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.016 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.035 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.036 ms --- 127.0.0.1 ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2032ms rtt min/avg/max/mdev = 0.016/0.029/0.036/0.009 ms
Each reply line shows the sequence number, the TTL and the round-trip time. After three
packets ping prints the summary: 0% loss, and rtt min/avg/max/mdev in milliseconds.
localhost may mean IPv6
ping -c 2 localhost
PING localhost (::1) 56 data bytes 64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.014 ms 64 bytes from localhost (::1): icmp_seq=2 ttl=64 time=0.047 ms --- localhost ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1056ms rtt min/avg/max/mdev = 0.014/0.030/0.047/0.016 ms
localhost resolved to ::1, the IPv6 loopback address, so ping used ICMPv6. Note the
header line looks different: IPv6 ping reports the payload size only.
Force IPv4 or IPv6
ping -4 -c 2 localhost
PING localhost (127.0.0.1) 56(84) bytes of data. 64 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.014 ms 64 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.028 ms --- localhost ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1021ms rtt min/avg/max/mdev = 0.014/0.021/0.028/0.007 ms
-4 restricts name resolution to IPv4 addresses (and -6 to IPv6), useful when a host has
both and only one path is broken.
Only the summary, sent quickly
ping -c 10 -i 0.2 -q 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. --- 127.0.0.1 ping statistics --- 10 packets transmitted, 10 received, 0% packet loss, time 1865ms rtt min/avg/max/mdev = 0.012/0.027/0.036/0.007 ms
-q suppresses the per-packet lines and -i 0.2 sends five packets a second instead of
one. Very short intervals are restricted for ordinary users; the exact limit depends on the
ping version.
Stop after a time limit
ping -w 2 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.017 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.031 ms --- 127.0.0.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1007ms rtt min/avg/max/mdev = 0.017/0.024/0.031/0.007 ms
-w 2 is a deadline: ping stops after two seconds however many packets that took, here
two. Combine -c and -w to say "N replies or give up".
Send larger packets
ping -c 2 -s 1400 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 1400(1428) bytes of data. 1408 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.018 ms 1408 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.040 ms --- 127.0.0.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1058ms rtt min/avg/max/mdev = 0.018/0.029/0.040/0.011 ms
-s 1400 sets the payload size; ping adds 8 bytes of ICMP header and 20 of IP header,
hence 1400(1428) bytes. Large pings help find links that drop big packets (MTU
problems).
Add a timestamp to each reply
ping -c 2 -D 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. [1793478028.507401] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.015 ms [1793478029.542808] 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.035 ms --- 127.0.0.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1035ms rtt min/avg/max/mdev = 0.015/0.025/0.035/0.010 ms
-D prefixes each line with the Unix time in brackets, handy when logging a long ping to
correlate drop-outs with other events.
Check reachability in a script
if ping -c 1 -W 1 127.0.0.1 > /dev/null; then echo "host is up"; else echo "host is down"; fi
host is up
ping exits 0 when at least one reply arrived. -c 1 -W 1 sends one packet and waits at
most one second for it, so the check is quick even when the host is down.
No route to the host
ping -c 1 -W 1 10.0.0.11
ping: connect: Network is unreachable
The sandbox has no route beyond loopback, so ping fails immediately with Network is
unreachable and exit status 2 (an error). A host that is routable but silent gives exit
status 1 instead, after the timeout.
A name that does not resolve
ping -c 1 web01.invalid
ping: web01.invalid: Temporary failure in name resolution
Resolution failed before any packet was sent, which is also exit status 2. The sandbox has
no DNS server, hence Temporary failure in name resolution; with working DNS, a name that
does not exist gives Name or service not known. Either way, check the name and DNS rather
than the network path.
Common pitfalls
Without
-c, Linux ping runs forever. In scripts, always pass-c(and-Wor-wfor a time limit).No reply does not mean the host is down: firewalls often block ICMP. Test the actual service port with
nc -z host portorcurl.-W(timeout per reply) and-w(deadline for the whole run) are easy to mix up.
GNU vs BSD and macOS
The examples above ran the GNU/Linux version. These are the differences that matter on macOS and the BSDs (written from their documentation, not run here).
On macOS and BSD,
-Wis in milliseconds for some versions and-tsets the overall timeout; Linux iputils uses-Wseconds and-wdeadline. Check the local man page before scripting timeouts.macOS uses a separate
ping6command for IPv6; modern Linuxpinghandles both.
Options worth knowing
A selection, described in our own words. For the full list, see ping(8) or ping --help.
| Option | What it does |
|---|---|
-c COUNT | Stop after sending COUNT packets. |
-i SECONDS | Wait SECONDS between packets (default 1). |
-w DEADLINE | Stop after DEADLINE seconds regardless of packet count. |
-W TIMEOUT | Wait at most TIMEOUT seconds for each reply. |
-q | Quiet: print only the summary. |
-s SIZE | Payload size in bytes (default 56). |
-t TTL | Set the IP time-to-live. |
-4 | Use IPv4 only. |
-6 | Use IPv6 only. |
-D | Print a Unix timestamp before each line. |
-n | Numeric output: do not look up names for addresses. |
-I INTERFACE | Send from this interface or source address. |
-M HINT | Path MTU discovery: do, want, dont or probe. |
-O | Report replies that are still outstanding when the next packet is sent. |
-a | Audible ping: beep on each reply. |
-f | Flood ping (root only): send as fast as replies come back. |
Related
ipShow and change network interfaces, addresses, routes and neighbors on Linux (iproute2).ssList network sockets on Linux; above all, which ports are listening and which connections are open.ncOpen raw TCP or UDP connections, or listen for them; for testing ports, talking to services by hand and piping data between hosts.curlTransfer data to or from a URL; the standard tool for HTTP requests, API calls and downloads from the shell.hostnamePrint the system's host name, its short or fully qualified form, or its IP addresses.