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.
ip routeOn 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.
getent hosts localhostThe 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.
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.
cat /etc/resolv.confIf 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.
head -1 /proc/net/routeThe 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.
getent hosts localhost
ip route
head -1 /proc/net/routeThe 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.