$_ CLI In Depth
Learning progress 0%
lesson networking sockets ssh

Routes & DNS Resolution

4 min read Module 16: Networking Tools Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:18

The routing table

The kernel keeps a routing table that maps destination networks to the interface and gateway that should carry the packet. The most specific match wins. A route for a /24 beats a route for a /8 that covers the same address, and the default route, written 0.0.0.0/0, matches everything that no other route matches. The default route usually points at a router, which is the gateway for traffic that leaves the local network.

BASH1 lines
ip route

On a typical laptop, this prints a default route through the Wi-Fi interface and a route for the local subnet. On the lab machine there is no default route, so the command prints nothing. That is an accurate reading of the situation: the machine has no path to any other network, only to itself through loopback. A program that tries to reach an outside address will fail with a network unreachable error, which is what you would expect in this environment.

Name resolution order

The C library resolves names in an order set by /etc/nsswitch.conf. On most Linux systems the hosts line checks /etc/hosts first and then DNS. The /etc/hosts file is a plain table of addresses and names. Local names such as localhost come from it, and it overrides DNS when both have an entry.

BASH1 lines
getent hosts localhost

The getent command performs the same lookup a program would perform, using the configured order, so it is a more honest test than reading a file. The output shows the address or addresses for localhost, which can include the IPv6 loopback ::1 as well as 127.0.0.1.

Tip

When a name does not resolve, run getent hosts name first. If getent fails too, the problem is in the system resolver or the network, not in the tool you were using.

The resolver configuration

DNS queries go to the servers listed in /etc/resolv.conf, with nameserver lines, and optional search domains that are appended to short names. A short name such as db is first tried as db with each search domain added. This explains why a name works on one network and fails on another when the search list differs.

BASH1 lines
cat /etc/resolv.conf

If the file is absent or has no nameserver line, DNS lookups fail. On the lab machine the file is not useful for real names, because the machine has no network. Learn the format anyway, because it is the first file to check on any machine where names do not resolve.

Reading the kernel routing table as a file

The same routing table is exposed by the kernel under /proc/net/route. The file has a header line and one line per route, with fields in hexadecimal. The header is the most useful part for a reader, because it names the columns. Reading it is a good way to confirm that the ip tool is not hiding something.

BASH1 lines
head -1 /proc/net/route

The first line names the columns: Iface, Destination, Gateway, and so on. Fields in this file are in hexadecimal and in the byte order of the host, so do not read addresses from it by eye. Use ip route for that.

Failures and what they mean

Three failure types are worth separating. A name that does not resolve points to resolution. A name that resolves to an address but times out points to routing or a firewall. A connection refused error means the address was reached and nothing listens on the port. The next units cover the ports and sockets involved in the last case.

Hands-on: read the name and route data

Run the lookup and the route listing below. Notice which one has output and which one is empty on this machine, and think about what each result says about the network.

BASH3 lines
getent hosts localhost
ip route
head -1 /proc/net/route

The first command prints the loopback addresses. The second prints nothing, because there is no route. The third prints the column names. The lab asks you to save the lookup and the header line, so the results are in files you can read again.

From a name to an address, then to a route

Visualizer

Two decisions come before any packet leaves: the name lookup picks an IP address, and the routing table picks the interface and next hop for that address.

1 / 4
getaddrinfo("localhost")
C library
name sources
address

A program asks the C library to turn a name into addresses, through getaddrinfo(). curl, ssh and ping all do this. No packet has been sent yet.

cat /etc/hosts; cat /etc/resolv.conf 2>/dev/null || echo "no resolv.conf"
Terminal lab · 2 steps · real Linux in your browser

Save a name lookup and the route header

Record the local name lookup and the column names of the kernel route table.

01
Save the localhost lookup
Create lookup.txt in /workspace with the result of looking up localhost through the system resolver, using getent hosts: ``bash getent hosts localhost > lookup.txt `` Open the file. It shows the loopback address or addresses for localhost.
Show hint for step 1
getent uses the same name service order that programs use, so it is the honest test. The file must contain the word localhost.
02
Save the route column names
Create route-header.txt in /workspace with the first line of the kernel route table, which is the header that names the columns: ``bash head -1 /proc/net/route > route-header.txt ``
Show hint for step 2
The first line of /proc/net/route is the column header. It is the only line you need.
Check yourself Question 1 of 4
Concept Check Default route recall

Which route matches every destination that no more specific route matches?

Recap & Key Takeaways

The kernel routing table picks the most specific matching route, and the default route 0.0.0.0/0 handles traffic that no other route matches.
Name resolution follows nsswitch order, usually /etc/hosts before DNS, and getent hosts performs the same lookup a program would do.
/etc/resolv.conf lists the DNS servers and search domains that short names use.
An empty ip route on this lab machine is accurate, because the machine has no route beyond loopback.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue