DNS is what allows us to type www.example.com instead of using an IP address. Think how difficult browsing the web would be if you had to remember IP addresses for all of the websites that you want to visit.
Hosts will usually have a file which specifies hostname - IP address pairings. This is usually checked first and then a query is made to a DNS server.
DNS servers are hierarchical so if the first server that you ask doesn’t have information for that exact domain, it will ask a more senior DNS server.
A host, acting as a DNS resolver, makes a recursive query. A recursive query is when you ask for a full answer to your query or an error but not a partial answer.
For example, if you want ‘blog.example.com’ you either want the IP address for ‘blog.example.com’ or an error message saying that it can’t be found. You don’t want the IP address for just ‘example.com’.
When you ask your DNS server to resolve an address, it will make iterative queries. Iterative DNS queries involve resolving parts of a domain name to get to the full answer.
DNS entries will be cached at different stages of this process to make lookups quicker in the future.
IANA (Internet Assigned Numbers Authority) maintains a list of the root servers. Today many people will use public DNS servers instead of relying on their ISP. For example Google's 22.214.171.124 service or Cloudflare's 126.96.36.199 service.
Common types of DNS record are given below.
Also known as a Host record, A Records map hostnames to 32 bit IPv4 addresses.
AAAA records map hostnames to 128 bit IPv6 addresses.
A Canonical Name (CNAME) record gives an alias for another name. A DNS lookup can then be performed against that. For example, ‘www.example.com’ might be an alias for ‘example.com’. Then if you ask for ‘www.example.com’ the DNS server will just tell you to get the IP address for ‘example.com’.
Mail eXchange (MX) records give the IP address(es) of mail servers for a given domain.
Name Server (NS) records give the IP address(es) for the authoritative name servers that service a domain.
Pointer (PTR) DNS records return a name, instead of trying to find an IP address. They are most commonly used for reverse DNS lookups (trying to find a hostname from an IP address).