What is DNS? How it works?

What about hosts file and resolv.conf?

Betul Ince
6 min readAug 3, 2021
Image Author: Seobility (<a href=”https://www.seobility.net/en/wiki/DNS_Server">DNS-Server</a>), License: CC BY-SA 4.0

What happens when you type www.whatisdns.com on your computer and press enter? How your computer brings the site with all its content to your web browser even if it doesn’t understand this human-readable, text-based domain name?

First, let’s understand some of the terms that we will use when we try to understand what DNS is.

DNS is the technology that translates human-readable, text-based domain names into machine-readable, numerical-based IP addresses.

How DNS Works

For a brief example: Think about two DNS clients, one of them is computer and the other one is printer, my host which is computer wants to connect to my printer to print out.Host will run DNS query and my local DNS server will respond with the IP address of this printer and then it will be done.

What is domain name? All of the site names that you know, google.com, youtube.com, cambridge.org, thetimes.co.uk, are domain names. Domain names are used for identify the IP addresses. Imagine that you supposed to know and kept in your head all the sites’ IP addresses. That would be hard insomuch as impossible. Therefore some tech guys thought about giving human-readable domain names which are corresponding to that IP addresses.

Domain names have a hierarchical structure:

Domain - Name Structure
  • It starts with the machine or host name (such as “www” which is specify “world wide web”,or it can also called as sub-domain; to give an example from Google, Google has many services such as mail, drive, maps, news, docs; When we enter our g-mail account, we encounter https://mail.google.com/ in the url, here is subdomain “mail”),
  • followed by the name of the website itself,
  • and finally the TLD (check out the top-level domains below).
Top-Level Domain Names

What is IP address? As you know numerical values are the only language that computers understand. That means when you want to go to a web page, you need to tell your computer the IP address of the site, which is used for any type of client(computer, web browser, another server) to identify each other on the network. Every machine that’s connected to a network will have an IP address.

What is hosts file?

Hosts file is used as the first step in the DNS lookup process for DNS hostname resolution(check below the “How DNS Works? What are the stages?”, step 1)

This is my hosts file on my Ubuntu 20.04,it is located in /etc/hosts (to see i opened this file with vim /etc/hosts):

127.0.0.1       localhost
127.0.1.1 betulPC
# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

The format is: on a new blank line you need to enter the desired IP address with a corresponding hostname that you would like to assign.For example;

127.0.0.1 example.com

The hosts file is the file that contain the IP addresses needed to enter websites, it can be thought of as an alternative to DNS. Since there was no such system as DNS until 1984, this domain name-IP address resolution was done with the hosts file. There was a copy of this file on each of the computers on the Internet, and in order to keep the content always up to date, the original of the file was periodically copied from where it was stored. But as the number of computers increased, this file was reached huge dimensions.

How DNS Works? What are the stages?

When you type in www.whatisdns.com in your web browser,

1-) DNS server first looks in the hosts file (/etc/hosts) while converting this domain name to the corresponding IP address.

2-) It will check the DNS cache when it can’t find what it is looking for in the hosts file.

3-) If it can’t find the answer it needs to return in the cache, DNS starts to search for answers from the local machine, hence the DNS servers.

Let’s take it from third stage:

  • Your operating system or web browser couldn’t find the IP in its own cache memory,it will send a query to the next level to what is called the Resolver Server.Resolver Server designed to receive queries(hostnames) from web browsers and responsible for tracking down the IP address for that hostname.
  • When resolver receives the query, it will check on its own cache memory to find an IP address for www.whatisdns.com.
  • If it can’t find it will query to the Root Server.Root Servers are the first step in the name resolution of domain names.Root Server will not know what is the IP address, but it will direct the resolver to the TLD (Top-Level Domain) Server for the .com domain.
  • TLD Server stores the address info for a top level domains that i showed above. Also TLD will not know what the IP addres is but it will direct the resolver to the next level Authoritative Name Servers.Name servers are responsible for knowing everything about the domain which is including the IP address.
  • Once the resolver receives the IP, it will store it in its cache memory.

Look at this structure to fully understand this mechanism:

How DNS Works

How do we point our host to a DNS Server ? What is resolv.conf ?

It contains the local DNS servers that the system uses.

This is my resolv.conf file on my Ubuntu 20.04,it is located in /etc/resolv.conf (to see i opened this file with vim /etc/resolv.conf):

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "resolvectl status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 127.0.0.53
options edns0 trust-ad

We should signal all our hosts to look at the DNS server if they need to resolve from host name to ip address. This is how we do it; Each host has the DNS resolution configuration file in /etc/resolv.conf. In this file we add the line specifying the address of the DNS server.

What is the advantage of Distributed DNS Infrastructure?

DNS directory distributed all around the world to account for the millions of domain names listed and accessed every day. This means more than one person can search for the same website at the same time and a domain name correspond to more than one IP address. For example imagine that you searched for www.whatisdns.com, and at the same time i searched for it too. If the domain name didn’t have DNS servers and the corresponding IP address, i mean only one server had the IP address, we would wait for each other in line for our turn to access the site.

Thank you for reading!

References

https://www.cloudflare.com/learning/dns/what-is-a-dns-server/

--

--

Betul Ince

Software Engineer @ Deloitte Cloud Engineering, known to shed a tear or two while debugging