Where to start First steps track · 15 stages
Linux Labs

ping — check whether packets reach a host

6 min Ubuntu 24.04 · iputils 20240117

The ping command sends ICMP echo requests to a host and prints replies, latency and loss — the first network check.

Cheat sheet

8 commands Printable version
  • ping -c 3 192.0.2.1 three requests and a summary; else Ctrl+C
  • ping -c 3 -W 1 192.0.2.1 wait at most a second for each reply
  • ping -c 3 -n 192.0.2.1 no name lookups — faster and cleaner
  • ping -c 3 -I lab0 192.0.2.1 through a given interface
  • ping -c 10 -q 192.0.2.1 only the final statistics
  • ping -c 3 -O 192.0.2.1 report every request left unanswered
  • ping -4 -c 3 example.com IPv4 only; -6 for IPv6 only
  • sudo ip netns exec lab-client ping -c 2 192.0.2.1 from a namespace

Overview

ping sends ICMP echo requests to a host and prints each reply. Without -c it runs until you press Ctrl+C, then prints a summary:

text
student@ubuntu:~$ ping -c 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.041 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.052 ms

--- 127.0.0.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1013ms
rtt min/avg/max/mdev = 0.041/0.046/0.052/0.005 ms

icmp_seq is the request number, time how long the reply took, packet loss the share of requests without a reply. A reply proves one thing: the packet reached the host and the reply came back. Whether a website or SSH works on the host, ping doesn't check — that's done by ports (see ss).

Exit status: 0 — there were replies, 1 — no replies at all, 2 — another error. Script checks are built on it — and only with -c or -w, otherwise ping never ends.

Options

10

Without -c, Linux ping runs forever — unlike Windows, which sends four requests by default. Full list — man ping.

Option What it does Example
-c count Stop after this many requests; without it ping runs forever. ping -c 3 192.0.2.1
-W seconds How long to wait for each reply. ping -c 3 -W 1 192.0.2.1
-w seconds The overall time limit for ping in seconds. ping -w 5 192.0.2.1
-I interface Send through the given interface or from the given address. ping -c 3 -I lab0 192.0.2.1
-n No reverse name lookups — numeric output only. ping -c 3 -n 192.0.2.1
-q Don't print each reply — only the summary. ping -c 10 -q 192.0.2.1
-O Report every request that has no reply yet. ping -c 3 -O 192.0.2.1
-i seconds The pause between requests (1 second by default). ping -c 20 -i 0.5 192.0.2.1
-4 IPv4 only; -6 for IPv6 only. ping -4 -c 3 example.com
-s bytes The data size of a request (56 by default). ping -c 3 -s 1472 -M do 192.0.2.1

Nothing found. Try a shorter query.

Recipes

4

Tell "no path" from "no reply"

text
student@ubuntu:~$ ping -c 2 192.0.2.1
ping: connect: Network is unreachable

Network is unreachable is printed at once: the kernel has no route to the address, and the request never left. Check addresses and routes: ip -br a, ip r (see ip).

If there is a path but no reply, there are no reply lines at all, and the summary shows 100% packet loss: the requests went out and nothing came back. The host doesn't exist, is off, or a firewall drops the replies.

Destination Host Unreachable

text
From 192.0.3.10 icmp_seq=1 Destination Host Unreachable

A host in your subnet didn't answer ARP — it's missing or off. The line comes from your machine (From is your address) or a gateway on the way. This is not a timeout: an error reply did arrive.

A check in a script

bash
if ping -c 1 -W 2 -q 192.0.2.1 >/dev/null; then
  echo reachable
else
  echo no reply
fi

Inside the training network

bash
sudo ip netns exec lab-client ping -c 2 192.0.2.1

The training machine has no network except lo: the client and server live in network namespaces joined by a veth pair. ping without ip netns exec looks for the address in the machine's own network and gets Network is unreachable.

Troubleshooting

5

ping: connect: Network is unreachable

There's no route to the address: no address on the interface, an address in another subnet, or no default route. Check ip -br a and ip r. On the training machine any outside address answers like this — it's cut off from external networks.

Temporary failure in name resolution

The name didn't turn into an address: no DNS or no network. Try ping by IP address; if that works, the problem is DNS (see dns-resolv).

100% packet loss, though the host surely works

A firewall drops ICMP — on the host or on the way. A drop rule gives silence and loss; reject gives an error line at once, such as Destination Port Unreachable. Look at the rules: sudo nft list ruleset (see firewall). Check the service itself by its port — nc -zv host port.

ping doesn't stop

There's no -c: Linux ping keeps sending until you press Ctrl+C. In scripts always give -c or -w (an overall deadline in seconds).

10–30 % loss, replies all over the place

The path is overloaded or unstable. Send more requests (ping -c 100 -q) and find which hop loses packets with mtr or tracepath — see network-diagnostics.

See also

  • network-diagnostics — the connectivity ladder: address, route, DNS, port.
  • ip — the addresses, subnets and routes that make ping fail.
  • veth — the veth pair and namespaces of the training network.
  • firewall — the rules that drop ICMP.
  • dns-resolv — when ping by name fails but by address works.
  • ss — check whether a service listens on a port: ping won't tell.
Loading…