Skip to content

It’s always DNS: dig, /etc/hosts and resolv.conf

NinjaLesson 59 of 669 min

The network works but no website loads. Prove it is DNS with ping and dig, find the wrong nameserver in /etc/resolv.conf, and fix it with sudo tee.

There's an old joke among people who run servers: when something on the network breaks, it's DNS. It isn't always, but it's often enough that it's worth ruling out first.

Is it DNS?

A name has to turn into an address before anything else happens. So try the same machine by address and by name:

ping -c 1 10.0.0.1
ping -c 1 example.com

If the address works and the name gets "Temporary failure in name resolution", the network is fine and the names aren't. (Your laptop, the router and example.com here are pretend machines inside this page.)

How Linux turns a name into an address

  1. /etc/hosts first: a short list of names and addresses on your own machine. That's why ping web works even though DNS has never heard of web.
  2. Then the DNS server named on the nameserver line of /etc/resolv.conf.

getent hosts example.com asks the way programs do, hosts file and all.

Ask DNS yourself: dig

dig asks a DNS server and shows you the whole conversation: the question, the answer, which server replied and how long it took.

dig example.com

"no servers could be reached" means the question never got an answer. Which server was that? @ asks a server of your choice, here the router:

dig @10.0.0.1 example.com +short

+short prints only the answer. If the router answers and the configured server doesn't, you know exactly what to fix.

Fixing a root-owned file

/etc/resolv.conf belongs to root, and this doesn't work, even with sudo:

sudo echo "nameserver 10.0.0.1" > /etc/resolv.conf

sudo runs echo as root, but the > is done by your shell before sudo even starts. Pipe into sudo tee instead, which opens the file as root (tee -a to add rather than replace), or edit it with sudo vim.

Tip:On a desktop or a cloud server, /etc/resolv.conf is often written by systemd-resolved or NetworkManager, and an edit by hand is undone later; resolvectl status shows what they use. The practice laptop keeps it simple: the file is the truth.

Your turn

Your laptop can’t load https://example.com. Prove it is DNS by asking the router’s DNS server (10.0.0.1) directly, point your laptop at it, and load the page.

Next