Introduction
Local DNS (Domain Name System) mapping is a wonderful function that is built into your operating system. With a hosts file, you can dictate the resolution of hostnames to IP addresses. Instead of your local computer reaching out to a DNS server to resolve a domain name, this process can be short-circuited using your computer’s hosts file.
The ability to bypass public DNS records allows you to perform a variety of functions. The most useful implementation is for local development. You can take your website at {{{domain:example.com}}}
and have it route to your localhost. You can do this by editing your hosts file.
You can customize this article to suit your needs by editing the information that is underlined and colored in red. This article will assume that the goal is to point {{{domain:example.com}}}
and {{{wwwdomain:www.example.com}}}
to {{{ip:10.1.10.100}}}
.
Windows
1. Open Notepad as administrator
- Hit the Windows key
- Type
Notepad
in the search field - Right click Notepad and select Run as administrator
- If prompted, click Yes on the UAC dialog
2. Open the Hosts file
- Go to File > Open
- Navigate to
C:\Windows\System32\Drivers\etc
- If it is not visible, select all files instead of text documents in the file type drop-down menu
- Select and open the file named hosts
3. Add the new entries
Implement the subsequent lines to the bottom of the document while following the format below:
{{{ip:10.1.10.100}}} {{{wwwdomain:www.example.com}}}
{{{ip:10.1.10.100}}} {{{domain:example.com}}}
Every line consists of an IP address and a domain name. Your desired target IP address goes first, then the domain name goes second.
It is common practice to use a
[TAB]
between the IP address and domain name when working inside a hosts file.
4. Save changes
To save your changes, simply click the File dropdown menu, and click Save.
Mac OSX
First, you will want to open up the Terminal application. It can be found in Applications > Utilities > Terminal, or by simply searching for it in Spotlight. Next, you will want to open the hosts file with your favorite text editor. We will be using Vim.
$ sudo vim /private/etc/hosts
Editing the hosts file requires privileged permissions. When you are prompted, please enter your password and hit
ENTER
.
Edit the hosts file by adding the following record at the bottom of the file:
{{{ip:10.1.10.100}}} {{{domain:example.com}}} {{{wwwdomain:www.example.com}}}
In the example above, we want the domain names {{{domain:example.com}}}
and {{{wwwdomain:www.example.com}}}
to point to {{{ip:10.1.10.100}}}
. Save the file by typing :wq
followed by enter.
Finally, to ensure that no DNS records are cached type the following in your terminal:
$ dscacheutil -flushcache
Linux
1. Access Terminal
First, open the Terminal application. This can be done by searching for it or it can be achieved with the following shortcut: CTRL
+ALT
+T
. Next, edit /etc/hosts
.
$ sudo vim /etc/hosts
Editing the hosts file requires privileged permissions. When you are prompted, please enter your password and hit
ENTER
.
Press i
to enter insert mode. Navigate to the bottom of the document, and add the following line in this format:
{{{ip:10.1.10.100}}} {{{domain:example.com}}} {{{wwwdomain:www.example.com}}}
Exit insert mode by pressing ESC
. Type :wq
followed by the ENTER
key to write changes to the file and quit the text editor.
Conclusion
That’s it! You’re ready to start testing your new server without having to publically change DNS records. The instructions above walked you through making the desired DNS changes. After you’re done testing your new server, you’ll want to reverse the changes to the hosts file. Follow the same steps, but remove the entries you previously created.