The What
Thankfully this can be done by modifying the ‘hosts’ file on your local system. The hosts file is the first place your system will look when trying to resolve a domain name to an IP address. This lets us supersede the information in the world wide DNS network for a single computer. The exact steps to modify the file do vary slightly based on operating system, but they do all share a few similarity.
1) As it’s a system file you’ll need to be a privileged user to modify the file. On windows this means using the ‘open as administrator’ feature. In linux or on a Mac you’ll need root or ‘super user’ privileges.
2) Each sub-domain you’d like to resolve to the specified IP address must be present in the hosts file (ie. domain.tld shop.domain.tld and www.domain.tld would each need to be explicitly specified).
3) Each line should start with an IP address followed by the domain or domains that should resolve. For example:
321.456.987.789 domain.tld www.domain.tld shop.domain.tld otherdomain.tld
4) If you have multiple domains your adding you can put them all on a single line, or create a new line for each domain. Just be sure that every line starts with an IP address.
The Why
Generally the DNS system of name servers does a great job translating domain names to IP addresses for us. However there can come a time when you’d like http://domain.tld to resolve to xyz.zyx.yzx.zxy instead of abc.bca.cba.acb This is often the case when testing a site in a new environment, but there are certainly other situations where you might want this behavior.
The How
The exact steps needed to modify the file based on operating system.
Windows 7 / Vista / XP / NT
1) Open Notepad as with elevated privilages. To do this click Start and look for the notepad entry. Right click on Notepad and select ‘open as administrator’
2) Click onto File -> Open and navigate to the hosts file. It should be in %systemroot%\system32\drivers\etc\ which generally works out to be C:\windows\system32\drivers\etc\
3) Once the file modifications have been made click Save
Windows 95 / 98 / ME
1) Open Notepad in the normal fashion, no privilege escalation is needed.
2) Open c:\windows\hosts If the file does not exist you can create a new file of that name.
3) Once the additions to the file have been made simply save the file.
Linux
1) Open /etc/hosts in the text editor of your choice. From command line the below should do the trick
sudo vim /etc/hosts
2) After the modifications have been made save the file. In VIM this would be “:wq”
OSX - 10.2 and later
1) Open the Terminal App
2) Open the hosts file with escalated privilages. The below should do the trick
sudo nano /private/etc/hosts
3) After modifying the file use ‘control+0′ to save the file (press enter when prompted for the file name) and then ‘control+x’ to quit
After you’ve saved the changes to the hosts file we’ll need to make those changes active. This means we’ll need to clear out the systems DNS cache. The simplest way to do this is to just restart your computer but this has always struck me as a bit heavy handed.
If you’ve got access to a FireFox browser you can use an addon like DNS Flusher which will clear your DNS cache with the click of a button and also displays the IP of a site as you view it.
If command line is more you’re style try one of the following:
Windows
From the command line run “ipconfig /flushdns”
OSX 10.2 and on
From a terminal run “lookupd -flushcache”
Linux
Restart your dns service. Likely this is nscd, but bind, dnsmasq and others could be present on your system. /etc/init.d/ and/or service restart will be your friend.
Once the DNS cache has been purged the domain name will resolve to the new IP address for your system. When you no longer want this behavior simply remove or comment out the line (add a # to the start of a line to comment it out), save, and re-flush your DNS cache