Linux commands
Browse topics

Testing reachability with ping

Version
unversioned
Review
Draft

Summary#

ping sends ICMP echo requests to a host and reports replies, round-trip times and loss.

Syntax#

sh
ping -c 4 example.com
ping -c 4 -W 2 192.0.2.1
ping -4 -c 1 localhost
ping -n -i 0.5 -c 10 192.0.2.1

-c stops after that many requests, -W sets how long to wait for a reply, -i sets the interval and -n skips name lookups. Without -c, press Ctrl+C to stop and see the summary.

Example#

sh
(
  set -eu
  ping -n -c 2 -W 1 127.0.0.1 | grep -o '2 packets transmitted, 2 received'
)

Expected output#

Text
2 packets transmitted, 2 received

Common mistakes#

No reply does not prove that a host is down: firewalls often drop ICMP while a service still accepts TCP, so test the port with nc -vz host port or curl. Destination Host Unreachable means the sender or a router could not deliver the packet, for example because nothing answered on the local network; check the address, the link and the route with ip route get. A name error happens before any packet is sent, so test with an address to separate DNS from connectivity. Use traceroute or tracepath to see where replies stop.

Version applicability#

This entry describes ping from iputils. Option letters differ on BSD, macOS and Windows. Some systems restrict ICMP sockets for ordinary users.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0