How DNS Works

What is DNS?

DNS stands for Domain Name System. A domain name is the unique name that is assigned to a website. DNS is the system that enables this to work. When you build a website, you also register a domain name. Then, you point the domain name to your website.

In most cases this is done via the control panel of your web host or domain name registrar and you don't need to know what happens "behind the scenes". Having said this, once you've configured your domain name, there are some DNS tricks you can use when developing your website. This article explains how DNS works and how you can use it to assist in your web development.

What is a DNS Server?

For a domain name to be assigned to a website, it first needs to be added to a DNS server. A DNS (Domain Name System) server is a large database containing each domain name and its corresponding IP address. For example, if the domain name "google.com" resolves to the website at 64.233.167.99 the DNS server holds this information.

Furthermore, there are many thousands of DNS servers worldwide. These are located with hosting companies and other organizations. These DNS servers actually keep in synch with each other. This way, your hosting company only needs to add your domain name to its own DNS server. Then gradually, (usually within 48 hours), the DNS entry is "propogated" to the other DNS servers around the world. In most cases, you can add/update a DNS entry using the control panel of your domain name registrar/web host.

How does my Browser find the right Website?

When you type a URL/web address into your browser, several things occur behind the scenes to bring you the correct web page. The first thing your web browser needs to do is translate the domain name into the IP address of a website. Doing this enables it to locate the correct website. Once it's found the correct website, it can then download the web page, along with it's associated files (images, CSS files, etc).

When you type a URL into your browser, it uses the following steps to locate the correct website:

  1. Your browser checks it's cache (memory) to see if it knows which IP address the domain name resolves to. If it knows, it will resolve it and display the web page.
  2. If the domain name is unable to be resolved, the browser will check your hosts file for a DNS entry (more on hosts files below).
  3. If there's no entry in the hosts file, the browser will check the default DNS server (specified via your computer's network settings). This is usually your ISP's DNS server or your employer's.

    If the DNS server has an entry for that domain name, the browser will display the applicable website.

  4. If the default DNS server doesn't have an entry for that domain name, the browser will attempt to find a DNS server somewhere in the world that does. This is made easier by the hierarchical structure of DNS servers throughout the world.

    If the domain name can be resolved, the applicable website will be displayed.

  5. If the browser can't find a DNS entry anywhere, it won't be able to resolve the domain name (or URL). This is usually due to there being no entry for that domain name, or possible network problems between your computer and the DNS server. In this case, your browser will display an error page (something you've probably seen before). Here's a sample DNS error:
    Sample browser DNS error page

Overriding DNS Entries with your Hosts File

Occasionally, you might want your own browser to resolve a domain name to a different webiste to the real one.

You might ask "Why on earth would I want to do that?"

Well, this can sometimes be useful when redeveloping your website. You could upload your changes to a "staging" server so that you can test the new version before it goes live. This way, you could browse the new version using the real URL, while the rest of the world continues to see the old version.

The easiest way to this is to modify your hosts file. Your hosts file is a file that probably already exists on your computer. It acts as a mini-DNS server. Any changes to it only affect your computer. Here's an example of the hosts file that comes with Microsoft Windows:

In the above example, only one line actually does anything - the last line. The other lines are just comments.

The last line in this example maps the ip address 127.0.0.1 to localhost (the local computer). Therefore, whenever you type localhost into your browser, it will display the default local website on your computer (if any).

For a local website to display, it would have previously been configured via your local web server. To learn more about web servers, check out the web server tutorial.

Here's an example of a modified hosts file:

Notice how I've added one line. This adds a (fictitious) domain name to an IP address. The bit after the # is simply a comment.

The new line tells the browser to resolve "my-test-site.com" to "64.233.167.99". So even if that domain name really exists (and points to another IP address somewhere in the world), your browser will use 64.233.167.99 instead. This is because (as mentioned previously), your browser checks your hosts file first (before any DNS servers). If it finds an entry in your hosts file, it will use that and won't look any further.

Overriding DNS Entries with a Local DNS

Another way of overriding a DNS entry is to use a local DNS server (for example, your company's DNS server).

Doing this allows you (or your organization's Network Administrator) to override DNS settings for the whole company or selected employees. Because employees' browsers check the local DNS server before any others, employees will see the website as specified in the local DNS server.

If required, employees can override the local DNS setting by modifying their hosts files. Alternatively, the Network Administrator could control this at the network level.