Module 8 · Archives, compression and the network
ping and reachability
Send ICMP echo requests with ping, read its replies and statistics, use its exit status in scripts, and learn why a reply in this sandbox proves less than it seems.
What you will learn
- Run ping with a count and a timeout, and read time, ttl and packet loss.
- Use ping's exit status to test reachability from a script.
- Interpret 'Network is unreachable', silence, and replies that are too good to be true.
ping is the first tool anyone reaches for when *the network is down*. It sends small ICMP echo request packets to a host and waits for echo replies. Every reply proves that your packet got there, that the host answered, and that the answer found its way back. It also measures the round-trip time.
~% ping -c 3 127.0.0.1
PING 127.0.0.1 (127.0.0.1): 56 data bytes
64 bytes from 127.0.0.1: seq=0 ttl=64 time=1.284 ms
64 bytes from 127.0.0.1: seq=1 ttl=64 time=0.541 ms
64 bytes from 127.0.0.1: seq=2 ttl=64 time=0.613 ms
--- 127.0.0.1 ping statistics ---
3 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max = 0.541/0.812/1.284 ms
Each line is one reply: seq numbers the packets so you notice gaps, ttl is the packet's remaining hop budget (lesson 80), time the round trip. The summary at the end, printed when ping finishes or when you press Ctrl-C, gives the loss percentage and min/avg/max times. Without -c Linux's ping never stops on its own, so in scripts and exercises always give a count.
| Option | Meaning |
|---|---|
-c N | Send N packets, then stop |
-W SEC | Wait at most SEC for a reply after the last packet |
-w SEC | Stop after SEC seconds whatever happens |
-q | Quiet: only the summary |
-s SIZE | Payload size (default 56 bytes) |
The exit status
ping exits with 0 if at least one reply arrived and non-zero otherwise. That makes it a building block for scripts: ping -c 1 -W 1 HOST >/dev/null 2>&1 && echo up || echo down. Throw away the output, keep the verdict. Add -W 1 so a dead host costs you one second, not ten.
Reading failures
Failures come in different flavours, and each points somewhere else. ping: sendto: Network is unreachable appears instantly: your own machine has no route for that address. Try it after ifconfig lo down and even 127.0.0.1 is unreachable; bring lo up and it answers. ping: bad address 'name' means the name could not be resolved, a DNS problem (next lesson). Lines that never come, ending in 100% packet loss, mean the packets left but nothing answered: the host is down, or a firewall drops ICMP, which many servers do. No reply does not prove a host is down.
ping inside TempMV
The live part of this lesson needs the machine's network switched on (Built-in mode) and an address from udhcpc -i eth0 (lesson 75). Then try ping -c 2 192.168.86.1, the virtual router. Now try ping -c 2 8.8.8.8: it answers in under a millisecond. That is impossible for a real trip across the internet, and it is the giveaway: a web page cannot send ICMP, so the emulator's network stack answers every echo request itself. Here a reply proves that your NIC, address and route work, nothing more. To check that a real site is reachable you will use HTTP, with wget, in lesson 78.
Commands in this lesson
| Command | What it does |
|---|---|
ping -c 3 HOST | Send three echo requests and print a summary. |
ping -c 1 -W 1 HOST | One quick probe, one second of patience. |
ping -q -c 5 HOST | Only the statistics. |
ping -c 1 HOST >/dev/null && echo up | Use the exit status in a script. |
Quiz
Which ping option sends exactly four packets and stops?
- -n 4
- -c 4
- -s 4
- -t 4
ping prints 'Network is unreachable' immediately. Where is the problem?
- The remote host is switched off
- On this machine: no interface or route for that address
- DNS cannot resolve the name
- A firewall on the far side
A server answers web requests but `ping` shows 100% packet loss. Most likely?
- The server is down
- ICMP is being filtered by a firewall
- Your DNS is wrong
- ping needs root
In TempMV's Built-in network, 8.8.8.8 replies in 0.6 ms. What does that prove?
- That the internet is very fast today
- Only that the guest's own network setup works; the emulator answers every ping
- That Google's DNS is up
- Nothing, ping is broken
What is ping's exit status when one of three packets got a reply?
- 0
- 1
- 2
- 3
Practice
The loopback interface is down. Bring it up, then ping `127.0.0.1` exactly 3 times and save the whole output in `/root/lab/l76/lo.txt`.
Write a script `/root/lab/l76/isup.sh` that pings the host given as `$1` once (wait at most 1 second) and prints `HOST up` or `HOST down` depending on ping's exit status, e.g. `127.0.0.1 up`.
Network needed (Built-in mode). Ping the virtual router `192.168.86.1` twice and save the output in `/root/lab/l76/gw.txt`. If eth0 has no address, run `udhcpc -i eth0` first.
Open this lesson in the app to do the tasks in a real Linux machine and have them checked.