Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ping Command Guide for Testing Whether a Host Is Reachable

ping sends small test packets to another machine and reports whether it answered and how long it took — the fastest way to ask “is this host up?”


What is ping, and Why Use It?

ping sends an ICMP “echo request” packet to a hostname or IP address. If the machine is reachable and allowing ping, it replies with an “echo response”, and ping prints each round trip with its timing. The name comes from sonar — you send a “ping” and listen for the “pong”. It is the first tool almost every Linux user reaches for when a network problem appears, because it separates two very different failures: the network path is broken versus the service on the other end is broken.

Why Learn ping?

  • It answers the first troubleshooting question instantly — one command tells you whether a machine exists and answers at all.
  • It measures latency — the time= field shows how long replies take, which is how you spot slow or flaky connections.
  • It works almost everywhere — firewalls sometimes block it, but when it does work, the result is trustworthy; every Linux install ships it.

Ping is a question about the network, not about a service. A host that refuses to ping may still be serving a website perfectly fine; a host that pings may have nothing useful listening on it.


1. Pinging a Host by Name or Address

The most basic use — name a target and let ping run forever:

ping example.com
PING example.com (93.184.216.34) 56(84) bytes of data.
64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=12.3 ms
64 bytes from 93.184.216.34: icmp_seq=2 ttl=56 time=12.1 ms
64 bytes from 93.184.216.34: icmp_seq=3 ttl=56 time=12.4 ms

Press Ctrl+C to stop. Each line shows the sequence number, the reply source, the TTL (how many routers the reply crossed), and the round-trip time. The first two lines also show what the name resolved to — useful confirmation that DNS works.

2. Limiting the Number of Pings

By default ping never stops. On most systems you add -c (count) to send a fixed number and exit:

ping -c 4 example.com
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 12.145/12.320/12.480/0.142 ms

The final statistics block is the part to read: packets sent vs received (loss), and min/avg/max round-trip time. Four packets is the common default; ping -c 1 is handy in scripts, and ping -c 50 gives a better latency picture.

On some systems the count flag differs — macOS BSD ping also uses -c, but always check man ping if a flag behaves oddly.

3. Reading the Results: Loss and Timing

Interpreting what comes back:

  • 0% packet loss, stable times — the path is healthy.
  • Some loss (e.g. 12% packet loss) — the path is degraded; investigate with traceroute.
  • 100% loss but the host is actually up — something is filtering ICMP; try the service itself with curl or nc.
  • 100% packet loss on ping 127.0.0.1 — your own TCP/IP stack is broken; this is a serious (and rare) local problem.

Timing varies by distance: under ~20 ms is usually local/regional, 50–100 ms is typical for continental connections, and 200 ms+ means far away or congested.

4. Pinging With a Count and Interval

For slow monitoring, space the packets out:

ping -c 10 -i 2 example.com

-i 2 waits 2 seconds between packets (default is 1). Changing the interval to under 0.2 seconds requires root and is the classic “flood ping” — use it responsibly, if at all.

A more realistic monitoring loop — one packet every 5 seconds until you stop it:

ping -i 5 example.com

5. Pinging By Raw Address (Skipping DNS)

When you suspect DNS itself, skip the name:

ping -c 3 93.184.216.34

If the name fails but the address answers, the problem is name resolution — move to dig. If both fail, the problem is the network path or the host.

ping also accepts other address families:

ping -6 ipv6.example.com     # force IPv6
ping -4 example.com          # force IPv4

6. Fuzzy Ping: Testing a Whole Range

ping accepts some non-standard tricks on Linux. To sweep a subnet looking for live machines:

for i in $(seq 1 254); do
  ping -c 1 -W 1 192.168.1.$i > /dev/null 2>&1 && echo "192.168.1.$i is up"
done

Each live address prints once; dead ones time out silently (-W 1 waits at most one second per probe). This is a quick map of who is on your local network.


Common Pitfalls

  • Treating “no ping answer” as “host is down” — many firewalls and cloud providers block ICMP by default. The host may be perfectly healthy; test the actual port with nc.
  • Forgetting -c — ping without a count never stops, which surprises newcomers and clogs scripts.
  • Blaming the network for a DNS failure — if you see ping: example.com: Temporary failure in name resolution, the network may be fine and only DNS is broken; use dig to confirm.
  • Using ping to measure real-world speed — round-trip time is not bandwidth. For throughput, use tools like iperf3 instead.
PageRelationship
tracerouteShows where along the path packets stop when ping fails
digThe detailed DNS debugging tool to use when ping fails on names only
nslookupQuick name lookups that often precede pinging a resolved address
ncTest whether a specific port answers when ping is blocked

ping Cheatsheet

CommandWhat it does
ping example.comPing a host by name (runs until stopped)
ping -c 4 example.comSend exactly four pings and show statistics
ping -c 3 93.184.216.34Ping by raw address, skipping DNS
ping -i 5 example.comSend one ping every 5 seconds
ping -W 1 192.168.1.10Wait at most 1 second for each reply
ping -4 example.comForce IPv4
ping -6 ipv6.example.comForce IPv6
ping -c 1 -W 1 HOST > /dev/null && echo upOne-shot reachability check for scripts