Linux › Section I · Your network and its configuration › Topic I.5

Basic ICMP and DNS tools

When a connection fails, two quick questions narrow the fault at once: can this machine reach the other host at all, and does the name turn into the right address? ping answers the first, and host, dig and getent answer the second.

Two protocols worth testing first

Most network faults show up in one of two places. Either packets do not get through, or a name does not translate into the right address. The Internet Control Message Protocol (ICMP) carries the network's error reports and its echo request and reply messages, as the Networking topic ICMP explains; it travels inside IP datagrams, with no application above it. The Domain Name System (DNS), covered in DNS: the internet's directory service, is an application-layer service that maps names to addresses. Linux provides a small tool for each, and they are the first commands to run when something breaks.

ping: is anyone there?

ping sends ICMP echo requests and reports each echo reply. ping 192.168.1.1 runs until you press Ctrl-C; ping -c 4 192.168.1.1 sends four requests and stops. Each reply line looks like this (illustrative): 64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.41 ms. icmp_seq numbers the requests, ttl is the time-to-live left in the reply, and time is the round-trip time. By default ping sends 56 data bytes, which with the 8-byte ICMP header gives the 64 bytes shown, one request per second. Older versions printed icmp_req instead of icmp_seq.

Reading ping's summary

When ping stops it prints a summary such as 4 packets transmitted, 4 received, 0% packet loss, then rtt min/avg/max/mdev followed by four times in milliseconds. mdev measures how much the round-trip times vary. A gap in the icmp_seq numbers means a request or its reply was lost. On a wired LAN you should see no loss and times of about a millisecond or less; Wi-Fi is slower and less steady. ping's exit status is 0 if at least one reply arrived and 1 if none did, which scripts can test, as Special variables and exit codes explains.

Useful ping options, and its limits

Some options worth knowing from the ping manual page: -c count stops after count requests; -i seconds changes the interval, and only root may go below 2 ms; -s bytes changes the data size; -W seconds sets how long to wait for a reply; -n skips reverse name lookups; -q prints only the summary; -4 and -6 force IPv4 or IPv6; and -I chooses the source interface or address. Remember the limit, though: many firewalls and servers block or rate-limit echo requests. No reply proves only that no reply came back, not that the host is down.

Worked example: a lossy link

ping -c 10 192.168.1.1 on a laptop shows replies for icmp_seq 1, 2, 4, 5, 6, 8, 9 and 10, then reports 10 packets transmitted, 8 received. 1. Missing sequence numbers: 3 and 7, so two requests or their replies were lost. 2. Loss: \(2 \div 10 = 20\%\), which ping prints as 20% packet loss. 3. Exit status: 0, because at least one reply arrived, so a script testing only the status would call this success. 4. Interpretation: losing a fifth of packets to the router on your own LAN points to the local link, often weak Wi-Fi, not the internet. Check: 8 received plus 2 lost makes the 10 transmitted.

Following the path: tracepath and traceroute

When ping to a distant host fails but ping to your router works, the next step is to see how far packets get. tracepath, from the same iputils package as ping, needs no special privileges and also reports the path's MTU. traceroute, often a separate package, is the classic tool: on Linux it sends UDP probes by default, -I switches to ICMP echo and -T to TCP. mtr combines ping and traceroute into a continuously updating display. All of them use rising TTL values and the ICMP time exceeded replies described in the Networking topic ICMP, so lines of asterisks may just mean a router ignores probes.

host and dig: asking DNS directly

host www.example.org prints the name's IPv4 and IPv6 addresses, and host followed by an address attempts a reverse lookup, which works only if the address's owner has set one up. dig shows the full DNS reply: dig www.example.org AAAA asks for IPv6 records, dig +short www.example.org prints just the answer, dig -x 192.0.2.10 performs a reverse lookup, and dig @192.0.2.53 www.example.org queries one particular server. Both programs come from the BIND utilities, packaged as bind9-dnsutils on Debian and Ubuntu and bind-utils on Fedora and RHEL. nslookup, from the same package, is an older interactive alternative.

getent: asking the way programs do

There is a catch with host and dig: they send queries straight to a DNS server, ignoring /etc/hosts and every other local source (dig also skips the search domains in /etc/resolv.conf unless given +search). Ordinary programs instead call the C library, which follows the order set in /etc/nsswitch.conf, as Resolving hostnames and localhost explains. getent uses that same path. getent hosts NAME returns what the library returns, and getent ahosts NAME lists every address the library's getaddrinfo function gives back, IPv4 and IPv6. So when dig and a browser disagree, getent tells you what the browser sees.

Worked example: two tools, two answers

A home user adds the line 192.168.1.50 nas.home.arpa to /etc/hosts for a storage box (home.arpa is a name reserved for home networks). 1. getent hosts nas.home.arpa prints 192.168.1.50 nas.home.arpa, because the library checks /etc/hosts first. 2. dig +short nas.home.arpa prints nothing, because dig asks the DNS server, which knows nothing of the local file. 3. ping nas.home.arpa works, since ping resolves names through the library. 4. Conclusion: dig tests DNS servers, while getent tests what applications actually use. Check: removing the /etc/hosts line would make step 1 fail too.

Common mistakes to avoid

First, concluding a host is down because it ignores ping; many block ICMP echo. Second, letting ping run forever in a script; use -c. Third, trusting a ping exit status of 0 to mean a healthy link; 0 means at least one reply. Fourth, using dig or host to check whether a program can resolve a name; they skip /etc/hosts, so use getent. Fifth, expecting every address to have a reverse name. Sixth, describing ICMP as running over TCP or UDP; it rides directly in IP datagrams.

Key takeaways

ping sends ICMP echo requests; read icmp_seq gaps, ttl, round-trip time and the final loss and rtt summary, and use -c to set a count. Its exit status is 0 when at least one reply arrives. tracepath, traceroute and mtr follow the path hop by hop. host and dig query DNS servers directly, with dig showing full replies and options such as +short, -x and @server; they come from the BIND utilities. getent hosts and getent ahosts resolve names exactly as applications do, including /etc/hosts. A silent ping or a missing reverse name is not proof of a fault.

Flashcards

Flashcard

ping HOST

Sends ICMP echo requests and reports replies

Runs until Ctrl-C unless you give -c.

Test your understanding

Question 1 of 5

ping -c 20 to a server reports 20 packets transmitted, 15 received. What is the packet loss?

Question 2 of 5

dig +short nas.home.arpa prints nothing, yet ping nas.home.arpa works. What is the most likely reason?

Question 3 of 5

Which tool resolves a name exactly as an ordinary application would?

Question 4 of 5

A ping to a public web server gets no replies, but the website loads in a browser. What should you conclude?

Question 5 of 5

ping -c 5 192.168.1.1 receives 3 of 5 replies. What exit status does ping return?

← Previous topicNext topic →