Knowledgebase

How to Edit Your DNS Hosts File

One of the most powerful tools available to anyone working on their site during a migration is their computer's "hosts" file. The hosts file is used to map domain names to IP addresses, and can be used as an alternative to DNS. It also allows you to specify the IP address to which a website resolves on your computer, regardless of what may be published in the site's DNS zone file.

Why Edit Your Hosts File?

Modifying your hosts file lets you view and test a site on one server while the rest of the world continues to see the site on another. That makes it an essential tool when migrating your website. With this method, you're able to ensure that:

  • Everything on the site works as expected on the new server before you update the DNS records
  • The visitors to your existing website will not be affected by any potential issues related to different server environments before you've had a chance to resolve them.

Step 1.
Edit Your Hosts File

Windows:

In Windows, the hosts file is located at C:\Windows\System32\drivers\etc\hosts. You will need to edit the file with administrative privileges.

Step 1.
Click the Windows button and type 'Notepad' in the search box.

Step 2.
Right-click on 'Notepad' and then 'Run as Administrator'.

Step 3.
Choose 'Yes' on the User Account Control pop-up.

Step 4.
In Notepad, choose 'File' then 'Open'.

Step 5.
Click the address bar at the top and paste C:\Windows\System32\drivers\etc\hosts - then choose 'Enter'.

Step 6.
Choose Notepad from the list and then OK.

Step 7.
Add the appropriate IP and hostname (given by the Migration team) at the end of your hosts' file, select Save, and close the file.

Step 8.
Next, you will want to flush your DNS cache so you don't have to log out and back in for the changes to take effect.

Step 9.
Lastly, open an elevated command prompt as above, and enter the following command:

root@host:# ipconfig /flushdns

Mac OS X:

On Mac OS X, your hosts file is located at: /private/etc/hosts. You will need administrative privileges to edit the file, which you can do manually or by appending the new entry directly from the command line.

Step 1.
First, launch Terminal from Spotlight search (Command+Space, or click on the magnifying glass icon in your menu bar) or the Utilities folder in Applications on many versions of Mac OS X.

Step 2.
To edit the file manually, enter the following command in Terminal.

sudo nano /private/etc/hosts

Step 3.
Enter your password when prompted and press Enter to authenticate and open the file.

Step 4.
Now add the new line of information. Use your arrow keys to navigate to the bottom of the file. Type in (or paste) the IP address and website name you intend to redirect.

Step 5.
Press Control+O to save (Write Out) the file. Then press Enter to overwrite the existing file and finally, press Control+X to exit.

If you prefer to simply append the entry to the existing file, you can do so with one command, substituting your server’s IP address and domain name for the ones in this example below and then enter your password when prompted.

echo "1.1.1.1 test.com www.test.com" | sudo tee -a /private/etc/hosts >/dev/null

While you still are in Terminal, you should flush the DNS cache so you don't have to log out and then log back in for the changes to take effect. For the current version of Mac OS X, you can do that with this command.

dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Linux:

On Linux, you can find the hosts file at: /etc/hosts. Depending on your distribution, you likely will need administrative privileges to edit the file.

You can edit the file manually with vi, vim, or nano, or append the new entry directly from the command line.

Using Vim

Step 1.
Open a terminal and enter the following command followed by the return key, and then enter your password to authenticate if prompted.

sudo vim /etc/hosts

Step 2.
Add the new entry at the end of the file, then type :wq to save and close the file.

Command Line

Step 1.
If you prefer to simply append the entry to the existing file, you can do so with one command, substituting your server's IP address and domain name for the ones in this example below. If not logged in as the root user, you may need to enter the root password to complete the command.

echo "1.1.1.1 test.com www.test.com" | sudo tee -a /etc/hosts >/dev/null

Step 2.
After running the above command, we need to flush the DNS cache. This command can vary widely depending on your specific distribution and version.

  • Many Ubuntu and Debian-derived distributions use:
sudo service dns-clean restart.
  • Other Linux distributions using NSCD (Name Service Caching Daemon) may need to use one of the following commands.
sudo service nscd restart 
sudo systemctl restart nscd.service
nscd -I hosts

Step 2.
View the Site on its New Server:

At this point, your website should resolve on your local computer to the IP address specified in your hosts file instead of the IP address specified in the site's DNS record. If you're not seeing the site on the new server, it could be because your browser is serving a cached version of the page. In this case, we can use the following steps to clear your cache.

  • Manually clear your browser's cache (typically Control+Shift+Delete or Command+Shift+Delete).
  • Use a private browsing window to view the site
  • View the site in another browser
  • Log out of your computer and then log back in

Step 3.
Test the Site on its New Server:

Now that you can see the site on its new server, you must thoroughly test it to determine whether everything works as expected. It is common to see some issues and error messages when testing a migrated site. There's no cause for alarm — typically only minor adjustments to the server configuration, such as enabling an Apache module or adjusting a php directive, are needed to resolve them.

To ensure that all your site's software, scripts, and plugins work correctly on the new server, be sure to use the following information checklist to verify the website's info.

  • Visit each link on your home page and ensure that it loads without error
  • If your site runs a CMS such as WordPress or Magento, log into the administrative area
  • If your site has a shopping cart, add an item and test your checkout process
  • Test any forms on the site
  • Create a post
  • Comment on a post
  • Upload a file

Should you notice any issues when performing the above tests:

  • Note the full URL of the page
  • Note the specific error message or problem
  • Provide that information to the person performing your migration.

Conclusion:

Modifying your hosts file is an excellent method for previewing your website during any transitional modification. It is usually a simple procedure but should you have issues you can contact our support.

Should you have any questions regarding this information, we are always available to answer any inquiries with issues related to this article, 24 hours a day, 7 days a week 365 days a year.

Our Support Teams are filled with experienced Linux technicians and talented system administrators who have intimate knowledge of multiple web hosting technologies, especially those discussed in this article.

If you are a Fully Managed server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone , a chat or support ticket to assisting you with this process.

 

  • Apple, Windows, Linux, Mac, Name Servers, DNS, Tools, Hosts, Hosts File
  • 64 Users Found This Useful
Was this answer helpful?