Anthony Corletti
Published on

How DNS Works


It's easy to take for granted the ability to type into a web browser, press Enter, and be served a powerful search engine.

The DNS, or Domain Name System, is mostly responsible for getting that data from somewhere on earth to your screen.

So how does it work?

The idea is that we want to try and resolve IP addresses to domain names.

For example I go to then at some point I'm going to have to work out what the IP address of that server is.

So to do that there needs to be some system in place because me just guessing an IP could take quite a long time.

So I'm going to type in a URL bar in my browser or on the command line with ping or something and then that URL is going to have to be resolved to an IP so I can actually communicate with that server to do whatever it was I want to do.

So let's imagine that I've gone to my browser and I've typed in or something like that.

My computer will have a cache of lots of domain names that I've visited recently, but they will last maybe one or two hours for ones on my computer.

So let's assume that we've just turned our computer on after a week or something like that and so it doesn't know where is.

It will have to make a DNS query to find out what it is now

The first point of contact will usually be a name server that belongs either to you your organization or ISP, say or, something like that; or you might have configured something like Open DNS yourself!

But let's assume that for the sake of argument that we're using our ISP's domain name server.

Now, we don't know what the IP address of is; so we're going to have to ask some other computer that might know.

What would normally happen here is we would ask a name server that we've either configured ourselves or is given to us by our ISP. In this case let's say I'm going to connect to my ISP's server and I'm going to request a web service by asking for

Now this name server will either know because it has a little cache of records that it can look through. If someone else has been to recently, which is quite likely, it would give that information back quickly, but let's assume that it doesn't and let's also assume that this is set up to be what we would call a recursive resolver; which means it can not only answer queries about DNS, but it can also ask queries about DNS too. So it's got to ask another machine that it thinks might know the answer.

Now our name server hasn't got the darndest idea, because there are a lot of different IP addresses. So what it's going to do is it's going to pick from a list of hard coded root name servers that all computers have access to.

So let's say we ask a root name server, and it says something like, "I don't know what's IP address is this day" - because who knows maybe Google turned their computer off and on again and it's changed โ€“ย so the root name server doesn't know what IP address to find.

So what happens? Well, first there are global top-level domain name servers (TLDs) to help us by sending us to a .com name server.

So now we're at a .com name server โ€“ we're kind of working right to left through the domain name.

So next we will put in a request to this .com name server and it will say I don't actually know ... but I do know that the next place you should ask; it should be kind of the name server or servers that are responsible for this zone or region of domains; let's say something like,,, and so on. Now we're getting closer, right?

Let's let's say hypothetically that these name servers are run by Google and that they know what their own IPs are. If they don't then it isn't going to work because those specific name servers handle the management of records like CNAME, A, AAAA, MX, SPF, and more that are pointing to

So we actually put in a request to and they're going to send back what we've actually queried for and it's what the IP of is.

So we finally have an IP address! Now we can actually send an HTTP GET request or ping or whatever it is we want to do to that IP and then we can get a response. So that's how DNS works in a nutshell!

Now the DNS service is a little bit more clever than this sort of overview because of some caching mechanisms.

So each server has a cache and a recursive resolver which will also have a cache, and any other resolvers involved in a request will also have a cache.

Let's suppose that an ISPs name server is serving 10,000 customers, right, all of whom were going to; doing what we talked about is a bit of a waste of time right? Especially given that Google are probably not changing their IP every hour.

So the first person in the morning that gets up and goes to is going to have to wait fractions of a second longer because it's going to be doing what I've said.

But then will be put into this cache with a time to live, or TTL, and then for that amount of time this will just serve that straight back.

Which is quite powerful; being able to serve all the ISPs customers really really quickly.

This is basically a kind of distributed database. This name server is going to be putting in lots and lots of requests right to hundreds of servers probably per second because there will be people all on the internet asking I want to go to,,, and such.

So if DNS is going to be doing all this. How does it know what to do with any of this all this gibberish of information that's coming back from resolvers?

Well, what DNS does is it has a query ID, which it sends out whenever it sends out a request to a name server, and the name server that responds will respond with the same query ID.

So all dns requests are labeled and connected.

So that allows DNS to sort through the mess of requests a little bit better โ€“ but it leads to a slightly interesting quirk, which is that if I send a response to a name server, that it didn't want but I get the query ID, it will accept it, which is for a different post on DNS cache poisoning. For the most part that poisoning doesn't happen because the DNS resolver does actually query an authoritative nameserver, attackers have only a few milliseconds to properly send the fake reply before the real reply from the authoritative nameserver arrives. Never discount speed! ๐ŸŽ๐Ÿ’จ