DNS: from names to addresses with dig
See how a name becomes an address: ask DNS with dig for A, MX and reverse records, find out why /etc/hosts can win, and give a server a test name of your own.
Computers connect to addresses, people type names. DNS (the Domain Name System) turns one into the other: your machine asks a DNS server "what's the address of shop.example.com?" and gets a number back. The machines here (your laptop, web, db and the router) are pretend ones inside your browser, so nothing you type reaches a real network.
dig
dig asks the DNS server and shows the whole answer. +short gives just the result:
dig example.comdig +short example.comIn the full answer, the ANSWER SECTION has the record: the name, how many seconds it may be remembered (the TTL), and the address. SERVER says which DNS server answered.
Record types
| Type | Holds | Ask with |
|---|---|---|
| A | an IPv4 address | dig +short example.com |
| AAAA | an IPv6 address | dig +short example.com AAAA |
| CNAME | "this name is another name" | dig +short www.example.com |
| MX | the mail server for a domain | dig +short example.com MX |
| PTR | the name for an address (reverse) | dig +short -x 10.0.0.20 |
dig @10.0.0.1 example.com asks one particular server, which helps when two servers disagree.
Where your machine looks
Programs don't ask DNS first. They read /etc/hosts, a plain list of names and addresses, then ask the DNS server named in /etc/resolv.conf:
cat /etc/hostscat /etc/resolv.confgetent hosts NAME shows what programs actually get. dig skips /etc/hosts, which is why dig web finds nothing here while ping web works: web is only in /etc/hosts. On the DNS server it's web.lan.
Testing with /etc/hosts
A line in /etc/hosts beats DNS, which is handy for trying a new server before the real DNS changes. It's a system file, so it needs root. sudo echo … >> /etc/hosts fails, because the >> is done by your own shell, not by sudo. tee -a (append) run with sudo does the writing instead:
echo "10.0.0.10 staging" | sudo tee -a /etc/hostsgetent hosts stagingYour turn
Answer three questions with dig: (1) the address of shop.example.com, (2) the mail server for example.com, (3) the name of the machine at 10.0.0.20. Then (4) make the name api point to web (10.0.0.10) on your laptop, so that curl http://api/ gets the shop’s page.
Your task
Answer three questions with dig: (1) the address of shop.example.com, (2) the mail server for example.com, (3) the name of the machine at 10.0.0.20. Then (4) make the name api point to web (10.0.0.10) on your laptop, so that curl http://api/ gets the shop’s page.
- The address of shop.example.com (not done yet)
- The mail server for example.com (not done yet)
- The name of 10.0.0.20 (not done yet)
- api leads to web (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
getent- Look up hosts, users or groups the way programs do
nslookup- Look up a name in DNS
Every everyday command on one page: the Linux commands cheat sheet.