One of the most powerful tools available to anyone working on their site during a migration is a 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
- Your site’s visitors won’t be affected by any potential issues related to different server environments before you’ve had a chance to resolve them
It’s actually a very simple process. Let’s take a look at an example hosts file:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
123.123.123.123 alpineweb.com www.alpineweb.com
In this case, the first three entries are defaults used to configure the local network interface. You may have more or fewer such local entries in your hosts file; you don’t need to worry about them other than to note their presence. Any custom entries will go at the bottom of the file, and in this case, you can see that I have added a custom entry to the end of the file already:
123.123.123.123 alpineweb.com www.alpineweb.com
My custom entry specifies that any request made from my computer (via web browser or SSH, email, or FTP client) for alpineweb.com or www.alpineweb.com will be directed to the IP address I’ve specified: 123.123.123.123. You will add your own custom entry to the end of your file using the same format.
The line for your custom entry will consist of three elements:
- The IP address of the server to which you want the domain name to resolve on your computer
- A tab or space
- The domain name(s) meant to resolve to the specified IP address
If you’re migrating to an AlpineWeb server, your migration technician will supply you with the line to add; you will just copy and paste it into your hosts file. If your migration involves multiple IP addresses, you will have one line for each IP address, regardless of how many domain names share it.
Step #1: Edit Your Hosts File
The location of your computer’s hosts file depends on your operating system. Because it is a protected file which must be edited with administrative privileges, the procedure for editing also varies by operating system.
Click a link below to skip ahead to the specific instructions for your operating system. If you experience difficulties editing your hosts file or are not seeing the sites on the new server after you’ve followed the steps below, check out the Bonus: If All Else Fails section at the end of this article.
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.
- Click Windows button and type Notepad in the search
- Right click on Notepad and then Run as Administrator
- Choose Yes on the User Account Control popup
- In Notepad, choose File then Open
- Click the address bar at the top and paste
Next, choose EnterC:\Windows\System32\drivers\etc\hosts
- Choose Notepad from the list and then OK
- Add the appropriate IP and hostname (give by the Migration team) at the end of your hosts’ file, select save and close the file
- Finally, 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:
Open an elevated command prompt as above, and enter the following command: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.
- 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.
- To edit the file manually:
- Enter the following command in Terminal:
sudo nano /private/etc/hosts
- Enter your password when prompted and press Enter to authenticate and open the file.
- Now add the appropriate line and save the file:
- 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.
- Press Control+O to save (Write Out) the file.
- Press Enter to overwrite the existing file.
- Finally, press Control+X to exit.
- Enter the following command in Terminal:
- 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:
echo "1.1.1.1 test.com www.test.com" | sudo tee -a /private/etc/hosts >/dev/null
and enter your password when prompted.
- To edit the file manually:
- 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
Note: On the first few releases of Mac OS X Yosemite (versions 10.10 through 10.10.3), the command needed to flush the cache is:sudo discoveryutil mdnsflushcache; sudo discoveryutil udnsflushcaches
.
For version-specific instructions in older versions of Mac OS X, see How To Flush Your Local DNS cache.
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.
- To use vim, open a terminal and enter the command:
sudo vim /etc/hosts
followed by return, and enter your password to authenticate if prompted. After adding the new entry at the end of the file, type:
:wq
to save and close the file.
Note: In vim, you can press “i” or “a” to enter text insertion mode; pressing the escape key (Esc) on your keyboard returns you to command mode. For a refresher on editing files with vim, see Vim Text Editor. - 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:
echo "1.1.1.1 test.com www.test.com" | sudo tee -a /etc/hosts >/dev/null
and enter your password.
- To use vim, open a terminal and enter the command:
- While you’re still in a terminal, you will want to flush your DNS cache. That 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:
sudo service nscd restart
,sudo systemctl restart nscd.service
, ornscd -I hosts
.
- Many Ubuntu and Debian-derived distributions use:
Step #2: View the Site on its New Server
At this point, your website should resolve on your computer from 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 that case, you can:
- Manually clear your browser’s cache, typically:
For browser-specific instructions, see Clearing Your Browser Cache.Control+Shift+Delete
orCommand+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
If you’re uncertain whether you are seeing the new site or the old, you can confirm the IP address of the site you’re viewing using a browser add-on. There is no shortage of such extensions, most of which will display a site’s IP address right in the browser’s menu bar. For your convenience, a few are listed below:
- Firefox: Show IP, IP Check
- Chrome: Server IP, Website IP, Show IP
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:
- 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. If AlpineWeb is handling the migration, simply paste that information into your migration ticket to ensure that the proper adjustments are made as quickly as possible.
Bonus: If All Else Fails
If, for whatever reason, you’ve been unable to successfully modify your hosts file to point your website to a new IP address, there remains one nearly foolproof option: View the site through an external service.
Hosts.cx is a free web-based service that allows you to preview and test your website on a different IP address. The site currently does not charge for its service, nor does it require you to register or provide any personal information.
When visiting https://hosts.cx, you will be prompted to enter your Server address and Website name. Note that you can only use one domain name, so choose the version you’re using on your site (e.g., www.yourdomainname.com or yourdomainname.com, but not both). Once you click the Get My Testing URL link, you’ll be presented with a shortened URL (in the format: abcde.hosts.cx) which you can click to view and test your site on the new server.
This method can be quite helpful for viewing your site on a new server, but it is not a perfect substitute for editing your hosts file. For instance, your pages will not load over a secure connection (https://). To prevent any possible security risk, you must not transmit sensitive data such as login information or passwords when testing via an external service. Additionally, certain site features, such as some CAPTCHAs, may not function as expected when requests are routed through a web service. Typically it does not indicate a problem with your site, simply a limitation (or security feature) of the code or plugin itself.