ping From Zero: One Packet Tells You If It Is Alive — the Exit Code Matters More Than the Output
ping does exactly one thing: send an ICMP echo request, wait for the echo reply, and report the round-trip time and packet loss. This guide runs every command for real on iputils ping 20240117 — packet count, interval, quiet mode, packet size, timestamps, IPv4 vs IPv6, the three failure modes, exit codes 0/1/2 — with output pasted verbatim.

When the network breaks, the first command most people type is ping. A service will not connect, a page will not load, a deploy stalls halfway — ping first, see whether the other side is alive, then decide where to look next. ping does exactly one thing: it sends an ICMP ECHO_REQUEST, waits for an ECHO_RESPONSE, and tells you how long the round trip took and how many packets were lost. Mike Muuss wrote the first ping in 1983. More than forty years later it is still step one of network troubleshooting, because it tests the lowest layer: can packets get there, and is the path open.
Every command in this guide was run for real on iputils ping 20240117, and the output is pasted verbatim. One honest note about the environment first: this demo ran in a sandbox that blocks raw ICMP sockets in the main namespace, so a bare ping fails with "Operation not permitted". Every ping in the demo script is therefore wrapped in unshare -Urn, which creates a user and network namespace, brings the loopback interface up, and then runs ping. On your own machine, VM, or server you do not need the wrapper — just type ping ... and the output is the same. The demo script says so at the top, and every line in the transcript is a real run.
Check the version, then start:
$ ping -V ping from iputils 20240117 libcap: yes, IDN: yes, NLS: no, error.h: yes, getrandom(): yes, __fpending(): yes exit=0
20240117 is the iputils version. The number differs between distributions, but the options below work the same on any Linux. Now the classic first run:
$ ping -c 4 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.090 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.043 ms 64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.054 ms 64 bytes from 127.0.0.1: icmp_seq=4 ttl=64 time=0.045 ms --- 127.0.0.1 ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3087ms rtt min/avg/max/mdev = 0.043/0.058/0.090/0.018 ms exit=0
Read this output line by line and you know ping. The first line, PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data., tells you the target and the packet size: 56 data bytes, plus an 8-byte ICMP header and a 20-byte IP header, 84 bytes in total. The four middle lines are the replies: 64 bytes from 127.0.0.1 is the size of the answer, icmp_seq=1 is the sequence number — counting from 1, so a missing number shows exactly which packet was lost; ttl=64 is the time to live, the number of hops the packet may still cross, and 64 is the Linux default for locally generated packets; time=0.090 ms is the round-trip time — 0.09 milliseconds, which is how fast loopback is. The last two lines are the summary: 4 sent, 4 received, 0% loss, and an rtt line with minimum, average, maximum, and mdev — mdev is the population standard deviation, a measure of how much the round-trip time varies. The more it varies, the less stable the network.
One detail: time 3087ms is the total time for all four packets, not one packet. By default ping waits one second between packets, so four packets take a little over three seconds. Change the interval with -i:
$ ping -c 2 -i 0.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.115 ms 64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.045 ms --- 127.0.0.1 ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 208ms exit=0
-i 0.2 sends one packet every 0.2 seconds, so two packets take 208 milliseconds. The manual states that intervals below 2 milliseconds require super-user privileges. And without -c, ping runs forever until you press Ctrl+C — always add -c in scripts, or the script will sit there waiting.
Just the result, no play-by-play: -q
In a script you usually do not care about each reply line, only the final statistics. That is what -q (quiet) is for:
$ ping -c 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 --- 2 packets transmitted, 2 received, 0% packet loss, time 1057ms exit=0
The reply lines are gone; only the header and the summary remain. Combined with the exit code, -q is the standard scripting form: ping -c 1 -q target && echo alive || echo dead. Note that -q only hides the reply lines — the packets still go out and the time still passes. Two packets still took 1057 milliseconds.
Changing the packet size: -s
The default packet carries 56 bytes of data. When you suspect large packets are not getting through, or want to see how a link reacts to them, use -s:
$ ping -c 1 -s 100 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 100(128) bytes of data. 108 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.162 ms --- 127.0.0.1 ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms exit=0
-s 100 means 100 bytes of data; the 128 in brackets is the total with the ICMP and IP headers, and the reply comes back as 108 bytes (100 data plus the 8-byte ICMP header). The manual says the maximum for IPv4 is 65507, though most systems limit it lower. A common real-world use is ping -s 1472 with -M do (do not fragment) to probe MTU: 1472 plus 28 bytes of headers is exactly 1500, the standard Ethernet MTU. If a packet that size does not get through, the path MTU is smaller than 1500. This guide only demonstrates -s itself; MTU probing is for another day.
Timestamps on every line: -D
When you run ping for a long time to catch intermittent loss, sequence numbers are not enough — you also need to know when the loss happened. -D prints a Unix timestamp with microseconds before each line:
$ ping -c 1 -D 127.0.0.1 PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data. [1796130258.737109] 64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.111 ms exit=0
The number in brackets, 1796130258.737109, is seconds plus microseconds — 1 December 2026 in this run. It is different every time, which is expected. Together with -O (report an outstanding reply before sending the next packet), -D is the standard combination for writing diagnostic logs: redirect the output to a file and match the losses against timestamps later.
Names and addresses: why localhost is ::1
The easiest trap when pinging a name is IPv6. Look at this:
$ ping -c 1 localhost PING localhost (::1) 56 data bytes 64 bytes from localhost (::1): icmp_seq=1 ttl=64 time=0.091 ms --- localhost ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 0ms exit=0
localhost resolved to ::1, the IPv6 loopback address — not 127.0.0.1 as you might expect. The resolver returned the IPv6 address first. Force IPv4 with -4:
$ ping -4 -c 1 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.120 ms exit=0
And -6 forces IPv6. The distinction matters in troubleshooting: sometimes IPv4 works and IPv6 does not, or the other way around, and without -4 or -6 you have no idea which path you just tested.
A related option is -n: numeric output only, no reverse DNS lookup. When you ping an IP address, ping normally tries to resolve it back to a name, and a slow DNS server stalls every line. -n skips that step:
$ ping -c 1 -n 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.084 ms exit=0
The three ways it fails
ping fails in three different ways, each with its own exit code and message. Telling them apart is the basic skill of troubleshooting.
First, no route. The target is not in any routing table, so the packet cannot even leave:
$ ping -c 1 -W 1 192.0.2.1 ping: connect: Network is unreachable exit=2
192.0.2.1 is TEST-NET-1, an address range reserved for documentation that does not exist in reality. -W 1 means wait at most one second. Note the exit code is 2, not 1 — the manual is explicit: no replies at all means 1, other errors mean 2. No route is an "other error".
Second, the name does not resolve:
$ ping -c 1 -W 2 nonexistent.invalid ping: nonexistent.invalid: Temporary failure in name resolution exit=2
.invalid is a reserved top-level domain for names that do not exist. This failure happens at the DNS step, before any packet is sent. When you see "Temporary failure in name resolution", check DNS configuration and the network first, not the target machine.
Third, sent but never answered. This is the classic packet loss, exit code 1. The manual says: "If ping does not receive any reply packets at all it will exit with code 1." When does that happen? The host is up but its firewall drops ICMP, a link in the middle is down, or the host is off while a stale ARP entry still points at it. One honest limitation of this demo sandbox: it only allows ICMP to loopback addresses, so the "sent, no reply" case cannot be reproduced here — non-loopback destinations are blocked at connect time. It is described here from the manual instead. On your own machine, run ping -c 2 -W 1 against a real host that blocks ping and you will see 100% packet loss and exit=1.
The exit code is for scripts
Put the three outcomes together and the rule is simple: 0 means everything answered, 1 means nothing answered, 2 means some other error. The design exists so scripts can test whether a host is alive:
$ ping -c 1 -q 127.0.0.1 && echo alive || echo dead alive
But never equate "ping works" with "the service works". A successful ping only proves that ICMP gets through — the network card, routing, and IP layer are fine. The web service on the target can be down, the port unlistened, the application crashed, and ping will still answer. The reverse is equally true: many servers and cloud providers block inbound ICMP by default, so ping never answers while SSH and HTTPS work perfectly. The correct use of ping is layered fault isolation: ping the local host (127.0.0.1) to confirm the local stack, then the gateway to confirm the LAN, then a remote host to confirm the outside world. The manual recommends exactly this order: local first, then hosts and gateways further and further away. Whichever step breaks, the problem is in that segment.
Two questions beginners always ask. What is ttl? It is the number of router hops a packet may cross before being discarded, decremented by one at each router. Its job is to stop packets from looping forever. Linux sends with ttl 64 by default, Windows with 128, so a reply from a Windows machine usually shows ttl=128. A small ttl, say 52, means the packet crossed a dozen hops. ttl is not latency — a small ttl does not mean slow. The other is -f (flood mode): it sends packets as fast as possible, printing a dot per request and erasing one per reply, to show the drop rate at a glance. The manual is clear that only the super-user may use it with a zero interval, and that "it is unwise to use ping during normal operations or from automated scripts". Do not flood someone else's server. That is not testing, it is an attack.
ttl in detail, and the two timeouts
ttl also lets you estimate how many hops a packet crossed. If the other side is Linux and sends with 64, and the reply you receive shows ttl 52, the return path crossed about 12 hops. The number is approximate — the other side may not start at 64 — but the order of magnitude is right. -t sets the ttl of the packets you send:
$ ping -c 1 -t 1 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.117 ms exit=0
-t 1 allows one hop. Loopback is one hop away, so it works. But note the reply shows ttl=64 — that is the ttl of the reply packet the other side built, not the one you sent. Do not confuse the two. The practical use of -t is finding routing loops by raising ttl one hop at a time — though that is really traceroute's job.
Then there are the two timeouts, -W and -w, which are easy to confuse. -W is "how long to wait for one reply". ping -c 1 -W 1 target sends one packet and gives up after one second without an answer. The manual notes that -W only affects the timeout when there are no responses at all; once replies arrive, ping waits for two RTTs. -W accepts decimals — -W 0.5 waits half a second, verified working. -w is the deadline for the whole run: ping -c 100 -w 2 target would send 100 packets, but stops after 2 seconds no matter how many went out. In the test run it sent just 2 packets before the deadline hit — "2 packets transmitted, 2 received, 0% packet loss" — because the default interval is one second. In scripts, use -c 1 -W 2 to test "is the host alive", and -w when you want "sample for N seconds". Both can be combined; whichever limit is reached first wins.
Why ping needs special privileges
Why does ping need root at all? Because it sends raw ICMP packets, which requires opening a SOCK_RAW socket, and Linux demands the CAP_NET_RAW capability for that — normally root, or the ping binary carrying a setuid bit and file capabilities. On your own machine ping works because the distribution set those permissions at install time.
This demo sandbox is the counterexample: it blocks raw sockets in the main namespace, so even root gets "Operation not permitted", and adding cap_net_raw to the binary does not help. The fix is a user namespace (unshare -Urn), where the process regains CAP_NET_RAW inside the new network namespace and ping runs. That is why the demo wraps every ping in unshare — not for show, but because the sandbox demands it. On your machine this is not an issue, but understanding the permission layer tells you where to look the next time a container says "ping: Operation not permitted": capabilities first, then seccomp, then the network namespace.
One more nuance: modern iputils ping prefers ICMP datagram sockets (SOCK_DGRAM) when it can, which unprivileged users may open without special rights. Raw sockets are only required for privileged features such as -f flood or preload above 3. So "ping needs root" is no longer quite true: the basics run as a normal user, and only a few advanced options need privileges.
Putting ping into a monitoring script
The most common automation for ping is checking whether a machine is alive once a minute and logging when it is not. The core is the exit code:
#!/usr/bin/env bash
# Check the gateway once a minute; log after 3 consecutive failures
host=192.168.1.1
fail=0
while true; do
if ping -c 1 -q -W 2 "$host" >/dev/null 2>&1; then
fail=0
else
fail=$((fail + 1))
echo "$(date '+%F %T') $host unreachable ($fail/3)" >> /tmp/ping-watch.log
[ "$fail" -ge 3 ] && echo "ALERT: $host down" >> /tmp/ping-watch.log
fi
sleep 60
done
Three details matter here. First, >/dev/null 2>&1 throws away all output and keeps only the exit code — scripts do not need the statistics lines. Second, -W 2 caps each check at two seconds so the loop never stalls. Third, it takes three consecutive failures to raise the alarm: a single lost packet is far too common on real networks — the other side hiccuped, or your own card blinked — and alerting on one loss means crying wolf all day. Hand this script to a systemd timer or to crontab and you have the simplest availability monitor there is. crontab itself is the subject of the next guide in this series.
Common questions
ping works, but curl fails — what now? The most common combination of all. ping proves the IP layer works; curl failing means TCP or the application is broken: the service is not running, nothing listens on the port, the firewall allows ICMP but blocks TCP, or DNS resolved to the wrong address. The next step is not more pinging — it is reading curl's error, checking the port with ss or lsof, and testing TCP with telnet or nc. ping has done its job: the road is open.
The time value jumps around — is the network broken? Look at mdev first. Loopback mdev is usually 0.0x ms, a LAN is under 1 ms, and public internet across carriers can be tens of ms. If mdev stays large for a long time — say an average of 20 ms with mdev of 15 ms — latency is jittery, video calls will stutter and bulk transfers will slow down. An occasional spike is usually one busy router and nothing to worry about. For judging network quality, mdev is more useful than the average.
What does "Destination Host Unreachable" mean? Your machine does not know how to deliver the packet — it cannot find even the next hop. Usually the target is on a different subnet with no default gateway configured, or ARP resolution failed because the host is simply not on the LAN. It resembles "Network is unreachable" but sits at a different layer: the first means "I know the road but nobody answers", the second means "there is no road at all".
Can ping measure bandwidth? No. ping measures latency and loss, not throughput. A 56-byte packet travelling fast says nothing about whether the link can sustain a gigabit. Use iperf3 for bandwidth, and -s with -M do for MTU probing. Let each tool do what it is good at.
Scope and summary
How this guide relates to the site's earlier ones: the lsof guide covers "which process holds the port, which process locks the file", the curl guide covers "sending HTTP requests and reading status codes", the wget guide covers "downloading files". ping touches none of those — it answers one question only: can packets get there. Need to know who holds a port, read lsof; need to see what a service returns, read curl; need to know whether two machines can reach each other, that is ping's job.
Five things to remember: -c sets how many packets to send, and without it ping runs forever; the exit code is 0 for reachable, 1 for no reply, 2 for other errors; localhost resolves to IPv6 ::1 by default, add -4 to test IPv4; -q keeps only the summary and belongs in scripts; and reachable does not mean the service is healthy, just as unreachable does not mean the machine is dead. The next network guide in this series will cover traceroute and ss, where the "reachable" and "unreachable" from ping will line up with every hop of the route. Before that, run today's ten commands on your own machine — reading the manual ten times teaches less than one real ping -c 4.
Comments (0)
Sources
Related articles
cron is the scheduling daemon that has run on Linux for decades: write a crontab file saying 'run this command at this time', and it handles the rest. This guide runs everything for real on Debian cron 3.0pl1-162 — installing a job, waiting one minute for the trigger, verifying the log, removing the job — plus special strings, five classic mistakes, and a real 3 a.m. log-archiving job.
cut does one thing: split each line into fields on a delimiter and output the fields you ask for. This guide runs every command for real on GNU coreutils 9.4 — field lists and ranges, CSV and output delimiters, character slicing, --complement, the -s rule for lines without a delimiter, log-column statistics, and the quoted-comma limit — with the output pasted verbatim.
grep does one thing: find lines in text that match a pattern. This guide runs every command for real on GNU grep 3.11 — line numbers, case-insensitive search, recursion, context lines, whole-word matching, the three pattern modes (BRE, -E, -F), anchors and alternation, quiet mode for scripts, and binary files — with the output pasted verbatim.