← Full reference

ping

Check host reachability over ICMP — packet loss, latency, MTU, exit codes for scripts

ping sends ICMP "echo request" packets and waits for "echo reply". The replies show whether the host is alive, how many packets are lost and what the latency (RTT) is. It's the first step in network troubleshooting: localhost first, then the gateway, then further out.

Syntax

ping [options] host

host is a name or an IP. Without -c and -w, ping runs forever until you press Ctrl+C.

Options

How many and how often

OptionWhat it does
-c Nsend N packets and exit
-i secinterval between packets (default 1 s; fractions allowed: -i 0.2). For a regular user — at least 0.002
-w secdeadline: exit after this many seconds, no matter how many packets were sent
-W sechow long to wait for a reply if there have been no replies at all yet (default 10 s; 0 — forever)
-fflood: send packets as fast as replies come back (root only)

What to print

OptionWhat it does
-qquiet: only the final statistics
-ndon't resolve addresses back to names
-DUnix timestamp before each line
-Oprint "no answer yet" for unanswered packets (gaps become visible)
-aaudible beep on each reply

Packet and addressing

OptionWhat it does
-s bytesdata size (default 56; plus 8 bytes of ICMP header and 20 bytes of IP)
-M doprohibit fragmentation — used to find the path MTU
-t TTLset the TTL of outgoing packets
-I interface|addresswhich interface or address to send from
-4 / -6IPv4 only / IPv6 only

Reading the output

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.353 ms
...
--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 201ms
rtt min/avg/max/mdev = 0.015/0.184/0.353/0.169 ms
  • 56(84) — 56 bytes of data, 84 — the whole IP packet;
  • icmp_seq — packet number: gaps in the numbering = loss;
  • ttl — how many more routers the packet could pass through;
  • time — round-trip latency;
  • summary: sent, received, loss percentage; mdev — latency spread (jitter).

Exit codes

CodeMeaning
0replies arrived (and with -c together with -w — exactly -c replies arrived)
1not a single reply, or with -c + -w fewer replies than -c arrived by the deadline
2another error: the name doesn't resolve, no permission, invalid option

Examples

# three packets and exit
ping -c 3 127.0.0.1

# quick check in a script: 1 packet, wait no more than 2 seconds for a reply
if ping -c 1 -W 2 -q 10.0.0.1 >/dev/null; then echo alive; else echo silent; fi

# only the final statistics for 20 packets at 0.2 s intervals
ping -q -c 20 -i 0.2 127.0.0.1

# run for exactly 5 seconds, no matter how many packets are sent
ping -w 5 127.0.0.1

# gaps are visible right away, with timestamps — handy for writing to a log
ping -D -O 10.0.0.1

# find the MTU: 1472 bytes of data + 28 of headers = 1500, no fragmentation
ping -c 2 -M do -s 1472 127.0.0.1

# force IPv4 (otherwise localhost may go to ::1)
ping -4 -c 1 localhost

Pitfalls

  • ICMP is often blocked. Firewalls and cloud networks drop ICMP while the service itself works fine. No reply to ping doesn't mean the host is down: check the port itself (curl, ss). In the IzzyLab sandbox outbound traffic is blocked too — practice on 127.0.0.1.
  • Without -c a script hangs forever. In scripts always set -c or -w.
  • Without -W a "dead" address holds you for 10 seconds. ping -c 1 unreachable_IP waits 10 s for a reply. For quick checks add -W 1 or -W 2.
  • -c together with -w is stricter than it seems. ping -c 5 -w 1 … ends after a second with code 1, even if every packet sent got a reply (0% packet loss): fewer than five arrived.
  • localhost may resolve to ::1 — ping shows PING localhost (::1). Need IPv4 — write 127.0.0.1 or -4.
  • Code 1 and code 2 are different troubles. 1 — packets went out but no replies came; 2 — ping couldn't even start (the name doesn't resolve, a typo in an option).
  • An interval under 2 ms and -f are root only. A regular user gets cannot flood.

See also

ip · ss · dig · getent · curl