Using the hosts file for DNS testing and development
Sometimes you want to test a virtual host or service but cannot (or will not) be able to create live DNS records. This could either be because you don't have a Name Server yet, or because simply you are migrating nad need to test it. In either case here's a solution that'll come in handy.
What is it
The hosts file is basically a local reference for the machine to resolve domain names. Normally it uses a name server to do this, however you might have noticed sometimes a special address called
localhost is used. This is not actually a recognised domain on the internet - instead most computers come with this configured inside their hosts file, however it's not the only use and more records can be added. In this way, the machine will check the hosts file first, so even if there's already a public DNS for example
google.com, any local record in the hosts file would override it. This means that on your machine only the IP address would be whatever you set in the hosts file (it goes without saying that anyone else on another machine wouldn't see this).
The hosts file is located in different places depending on your OS.
As you can see the file is named the same and follows the same format for most operating systems, and you can add multiple hosts per IP address as well.
A simple example
Before we start fiddling you might want to verify the addresses (if applicable) of the domain names you want to override.
➜ ~ ping google.com PING google.com (126.96.36.199): 56 data bytes
Of course this IP returned from google is just one of many they use, but what if we wanted to test requests to google by using our local machine first? We can make our machine pretend that google (or any site) is our local machine. Append the following line to hosts file:
Now if you ping google.com, you should see it return localhost.
➜ ~ ping google.com PING google.com (127.0.0.1): 56 data bytes
NB. Sometimes applications may cache their own DNS irrespective of the system, so if your application doesn't immediately update check that out.
Something more useful
So that's all well and good, we can fool our applications into thinking google is located at localhost (127.0.0.1), but what will that get us, aside from most likely breaking our web browser? (Aside, this is a useful technique to stop nefarious apps from accessing their servers - by rerouting them to localhost).
Imagine you have a server compnay.com, and you've been tasked to migrate it to a new server (this example works best if you imagine you only have one server - not a redundancy). You need to test the new server before you deploy it, but due to requiring virtual host setup (e.g. listening for www.mycompany.com and maybe web mail on mail.mycompany.com) you can't test it without the domain names pointing there.
Hosts file comes to your rescue:
- Find the IP address of the new server - e.g.
- Add the entry like:
A.B.C.D first.mycompany.com second.mycompany.com
- Check your web browser and do your testing, e.g.
- Remove the entry once deployment finished
This provides a way to access the server as if it were already deployed without exposing the not quite ready for production setup to the rest of the world and disrupting your day to day business operations. ]