How to locally override website domain (or hostname) to IP mapping

When we browse a web site, the browser first do a DNS lookup to find the IP address of the domain name. We can override this IP by an IP of our choice so that browser hits the IP we want instead of the IP obtained from DNS server. Now why would someone want to override the IP mapped to a specific domain? Here are some reasons:

  1. As a developer you may want to hit a specific server to debug. If you have multiple server behind a load balancer or multiple aname records for a domain name, you may hit any of the IP addresses. Overriding it lets you hit a specific server.
  2. If you are migrating your site, you may want to set it up on a new ip address first and then change the mapping locally to test the new site before permanently migrating.

Steps on Mac or Linux using hosts file

  1. Edit /etc/hosts file using sudo vi /etc/hosts and add this entry:

    Here replace aaa.bbb.ccc.ddd with the desired IP address. You can also put multiple host entries separated by space.

  2. Now ping (or whatever domain name you have setup) using:

    This should show some like this:

    PING (aaa.bbb.ccc.ddd): 56 data bytes

    Ensure that the ip address matches with what you entered in the hosts file.

  3. If you are running a web server at the ip address mentioned in the hosts file,  then access in your browser and you will hit the desired ip address.

Steps on Windows 7 using hosts file

These are the instructions for window 7. For other window versions steps may be slightly different.

  1. We can modify hosts file using notepad running as administrator. Open Notepad as administrator by right clicking on Notepad program and selecting “Run as administrator” as shown below:
  2. Edit the hosts file using notepad. The file location is c:\windows\System32\drivers\etc\hosts
    windows-hosts-file-pathMake sure that “All Files” is selected instead of “text documents” as this file does not have .txt extension.
  3. Edit the file and put desired ip address and domain name (hostname) as mentioned above. e.g.

    While saving ensure that it is saved as hosts file and not hosts.txt. To do this make sure “All files” is selected instead of “Text Documents”. Otherwise windows may put a .txt extension automatically while saving. If that happens override won’t work.

  4. Ping the hostname to verify if points to correct entry. You may have to enable the command line utility ping on windows in case it is not enabled.
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit


Click here to write/view comments