What is the HOSTS file & when should I use it?

Editing HOSTS file on OS X via NanoWhat is the HOSTS file?

The HOSTS file is used by your computer’s Operating System (Windows, OS X, Linux, etc) to map hostnames to IP Addresses.  In layman’s terms your computer will check this file for any domain it needs to resolve to an IP Address prior to querying public DNS resolvers.  The HOSTS file can be used to point a domain name to an IP Address regardless of the DNS settings for the domain.

An example of a HOSTS entry to load “www.example.com” and “example.com” from the IP address “93.184.216.34“:

93.184.216.34    www.example.com example.com

Windows may require two separate lines like so:

93.184.216.34    www.example.com
93.184.216.34    example.com

When the above records are added to your HOSTS file the result is that if you try to load that domain in your browser it will connect to that IP to make the request.  If you try pinging the domain you will find that it pings that IP as well.  What this means is that you can use this to test your site on a new server before you point your world-wide DNS there.

Are there any risks to modifying the HOSTS file?

There is relatively little risk to modifying the HOSTS file.  One suggestion is simply to keep a copy of the original contents of the file safe just in case you feel your changes have caused any issues.  Most will find that their HOSTS file only has one or two entries when they open it up and we don’t suggest that you modify the pre-existing entries unless they are entries you created prior.

The largest risk I would have to say is modifying the HOSTS file to test a site and then forgetting that you modified the HOSTS file and not removing the modification.  We consider HOSTS file modifications temporary in that you really should rely on global DNS to load your site.  Issues arise when your HOSTS file to test your site, forget that you modified your hosts file, and then the IP address of your site changes for any reason.

There have been cases in the past where we have had customers that modified their HOSTS file and forgot and it ultimately caused them some grief down the line.  We’ve even had a customer or two over the last decade that canceled services because they claimed their site wasn’t working for it to end up not working with their new provider for the same reasons.  Ultimately the cause was determined that the users modified their HOSTS files and forgot about it.

If you modify your HOSTS file – it is suggested to remove the modification when you are done testing and/or DNS has updated.

When should I use the HOSTS file?

We have found internally that we use our HOSTS file extensively when we help new customers transfer their sites to our services.  Modifying our HOSTS file allows us to see the customer’s sites loading on our servers before the customer updates their DNS.  This allows us to test their site and ensure to the best of our abilities that it is working as expected from our services.

You can modify your HOSTS file as well in order to test your site just like we do.  All you need is the domain name you wish to test and the IP address of the account on the server you wish to test it on.  When it comes to cPanel you can find the IP address of your account on the left side under “Statistics” -> “General” -> “Server Information“.  You will either see “Dedicated IP Address” or “Shared IP Address” which will be the IP of your account.  If you are in doubt your provider should be able to tell you the proper IP address for your account.

How do I modify my HOSTS file?

How you modify your hosts file will vary from Operating System to Operating System.

Windows 7, 8, 8.1, 10

  1. Click on the Start button or press the Windows key.
  2. Type Notepad – don’t worry if you don’t see a search bar before you start typing so long as the start menu is open.
  3. In the search results right click on Notepad and choose Run as administrator.
  4. In Notepad click on the File menu and then Open.
  5. In the File name: section of the Open Dialog you will want to enter:
    c:\windows\system32\drivers\etc\hosts
  6. Make any changes you feel necessary and save the file.

OS X 10.6+

  1. Open Applications -> Utilities -> Terminal.
  2. Run the following command to open the HOSTS file in the text-based editor nano:
    sudo nano /etc/hosts
  3. You will be prompted to enter an administrator password, usually your password to sign into the computer.
  4. Make any changes you feel necessary.
  5. To save the file press CTRL+O and then press the Return key.
  6. To exit press CTRL+X.  If you are prompted to save the file you can press the Y key to save or the N key to discard changes.

 

One Reply to “What is the HOSTS file & when should I use it?”

  1. Just a warning to others reading this. Firefox and maybe other browsers now enable DoT (DNS over HTTPS) which will cause problems when trying to use your hosts file. What you will see is that pinging the domain will work properly but the browser will load your site from the old server. You need to go to the options page and then the network settings and uncheck the ‘Enable DNS over HTTPS’ option. I haven’t looked into Chrome, Opera etc. but I suspect it is or will be the same situation.

Leave a Reply

Your email address will not be published. Required fields are marked *