Introduction

Being able to edit the hosts file gives an ability to assign an IP address to a domain name (local only), without having to perform a DNS update. The hosts file is useful when you want to access a version of your hosted website on a server whose IP address is different from the IP address of the production server.

What is the Hosts File for?

It is a file that is queried first before making a DNS query on the web address servers. So, for each name configured in this file with an IP address after a space, once will open a connection to the indicated IP address. In short, it works like a phone book.

How does the Hosts File Work?

The hosts file is accessed by your computer before accessing a website. If an IP address is entered for the domain name that you want to access, your computer will associate that IP address with that domain name without querying the Internet Service Provider’s (ISP) DNS servers.

Example

www.eukhost.com hosted at the “X” server and IP address of the server is suppose: 10.10.10.10. If we want to revamp the website, we’ll simply transfer all the files of a new website on another server such as “Y” servers to test the accessibility of the site to make sure that there is no bug. Hence, all the visitors of eUKhost will continue to access the current version of the site which is hosted on the server “X” while we are performing our tests and configurations of the newer version of the site.

Where to find the Hosts File?

Windows (10, 8, and 7): access >> C:\Windows\System32\drivers\etc\hosts or right click on “notepad” and run it as administrator:

 notepad c:\windows\system32\drivers\etc\hosts

Linux

 Open a terminal window and open the hosts file with your desired text editor by typing the following line:

sudo nano /etc/hosts

Enter your “domain user password” >> make the required changes to the file >> press “Control-x” >> you will get a prompting message whether you want to save your changes >> answer “y”.

Mac OS X 10.0 through 10.1.5

  • Open Applications > Utilities > NetInfo Manager.
  • Click the padlock in the lower left corner of the window to enable editing of the NetInfo database
  • Type in your domain user password and select OK.
  • Select the node named machines in the second column of the browser view
  • The third column contains entries for -DHCP-, broadcasthost, & localhost
  • Select localhost in the third column
  • Select Duplicate from the Edit menu
  • You will get a confirmation alert
  • Now, click on Duplicate
  • A new entry called localhost copy will appear along with the properties listed in the browser view.
  • After Double-clicking on the value of the ip_address property, type the IP address of the other computer.
  • Again, Double-click the value of the name property and type in the hostname you want for the other computer.
  • Select the serves property and then click on Delete from the Edit menu.
  • Go to “File” menu and select Save.
  • You will get a confirmation alert box.
  • Select “Update” this copy.
  • Repeat steps 6 through 12 for each additional host entry that you want to add.
  • Select Quit from the NetInfo Manager menu.
  • You do not need to restart the computer.

Mac OS X 10.6 through 10.12

  • Applications > Utilities > Terminal and open the hosts file by typing the following line in the terminal window:
  • sudo nano /private/etc/hosts
  • Enter your domain user password when prompted
  • Edit the hosts file
  • The file contains some comments (lines starting with the # symbol), and some default hostname mappings (for example, 127.0.0.1 – local host). After the default mappings add your new mappings.
  • Press “Control+x” to save the hosts and answer y.
  • Flush the DNS cache by entering the following command to make changes:
  • dscacheutil -flushcache
  • The new mappings should now take effect