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
| Option | What it does |
|---|
-c N | send N packets and exit |
-i sec | interval between packets (default 1 s; fractions allowed: -i 0.2). For a regular user — at least 0.002 |
-w sec | deadline: exit after this many seconds, no matter how many packets were sent |
-W sec | how long to wait for a reply if there have been no replies at all yet (default 10 s; 0 — forever) |
-f | flood: send packets as fast as replies come back (root only) |
What to print
| Option | What it does |
|---|
-q | quiet: only the final statistics |
-n | don't resolve addresses back to names |
-D | Unix timestamp before each line |
-O | print "no answer yet" for unanswered packets (gaps become visible) |
-a | audible beep on each reply |
Packet and addressing
| Option | What it does |
|---|
-s bytes | data size (default 56; plus 8 bytes of ICMP header and 20 bytes of IP) |
-M do | prohibit fragmentation — used to find the path MTU |
-t TTL | set the TTL of outgoing packets |
-I interface|address | which interface or address to send from |
-4 / -6 | IPv4 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
| Code | Meaning |
|---|
0 | replies arrived (and with -c together with -w — exactly -c replies arrived) |
1 | not a single reply, or with -c + -w fewer replies than -c arrived by the deadline |
2 | another 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