It’s always DNS: dig, /etc/hosts and resolv.conf
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.1ping -c 1 example.comIf 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
/etc/hostsfirst: a short list of names and addresses on your own machine. That's whyping webworks even though DNS has never heard of web.- Then the DNS server named on the
nameserverline 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.
Your task
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.
- Ask the router’s DNS server directly (not done yet)
- Your laptop asks a DNS server that works (not done yet)
- example.com loads (not done yet)
The checklist ticks itself off as you work in the terminal: any way that gets the result counts.
Hints come one at a time, then one way to do it. Try each before the next.
What this lesson uses, in one place.
dig- Ask a DNS server about a name More on dig
dig @server- Asks one DNS server directly, skipping the one /etc/resolv.conf names More on dig
/etc/hosts- Names your machine looks up before asking DNS: an address and a name per line
/etc/resolv.conf- Which DNS server your machine asks (the nameserver line)
tee- Save what passes through a pipe to a file, and pass it on More on tee
Every everyday command on one page: the Linux commands cheat sheet.