Why does `ping` not timeout in Linux?

Alex picture Alex · Dec 3, 2013 · Viewed 20.4k times · Source

I just figured out, that ping on a Linux platform (Ubuntu 13.10) does not timeout as described. I tried

ping -w 2 unreachable.com

and

ping -W 2 unreachable.com

but in neither case there was a timeout after 2 seconds. How can I use ping with a definite timeout? Is that possible at all? I want the command to stop after 2 seconds, regardless of any connection status.

Answer

Laszlo Valko picture Laszlo Valko · Dec 3, 2013

ping -c 5 -W 2 will send out 5 pings, waiting 2 seconds max for each of them (a total max of 10 seconds).

ping -w 5 will send out pings, but will stop after 5 seconds.

You have to be careful with name resolution: if you use a name instead of an IP address, the resolution of the name does not count into these timeouts & waits (pinging and time measurements start only after the name resolution has finished). If you use DNS, you can set DNS timeouts in /etc/resolv.conf - see its man page.