What is a local hosts file, and why its important to edit this file?

January 11, 2016

There are times when you may wish to test a web site you have designed before the actual domain name’s DNS (Domain Name System) entry is updated.

Have you found yourself looking to access a server through an IP address and unable to access the site because it is bound by a domain name that points to another servers IP? Sometimes the domain name is not pointing to that particular machine and you need access for development or testing. For this purpose we will explain why you would want to edit your local machines hosts file. All computers have a hosts file that exists on the system; Windows, Linux, and Mac. The Hosts file is like an address book. When you type an address like “www.snapfast.com” into your browser, the Hosts file is examined to see if you have the IP (Internet Protocol) address for that site. If you do, then your computer will “call it” and the site will open in your default browser of choice. If not, your computer will ask your ISP’s (internet service provider) computer for the IP address before it can “call” that site.

 

Edit hosts file with Terminal on Mac

What is the Hosts File On My Computer?

A hosts file is similar to an address book. Exactly like the example above, when you type an address like “www.snapfast.com” into your web browser, the Hosts file on your own computer is referenced and accessed to see if you have the IP address or “telephone number” for that web site. If you do, then your computer will use that number it has on file locally to “call” and the corresponding web site opens. If not, your computer will ask the DNS Server belonging to your Internet Service Provider for the associated IP address for the corresponding web site and connects you to that web site. The majority of the time, you will not have all the IP Addresses of all the web sites from the entire Internet in your “address book”. You will probably have very few (if any) entries within your Local Hosts file. Therefore, most of the time your computer will ask for the IP addresses of web sites you wish to visit from your ISP.

Why Would I Want to Edit The Host Files On My Computer?

Sometimes when designing a new web site, you may need to test certain aspects of the site before launching the site live to the general public. By editing your local Hosts file, you can affect what happens when you type in a URL on your own system by redirecting the web browsers on your computer to a different IP address to view that particular site than the rest of the world would see. The general public will see the DNS propagated site that exist for that site.  So while the rest of the users of the Internet type in “www.snapfast.com” into their web browsers and get redirected to the web server at the IP address 209.90.81.140, visiting the same “www.snapfast.com” web address on YOUR own computer only could bring you to the IP address of say 123.123.23.23 (your testing web server, for example). Once you are done testing your site, you could then edit your local Host file again to connect to the site as a regular Internet user would.

To test your web site using your own domain name BEFORE DNS propagation has completed, you can use your local computer’s HOSTS file. Your computer will use the entries in your HOSTS file FIRST before it tries to use your IPS to look up the DNS information for your domain.

REMEMBER: When you are finished testing, remember to remove the custom lines that you added to your Hosts file or add a comment (# sign) before the entry.

 

Illustration-Domain-Name-Resolution-Process-with-local-Hosts-file-and-DNS-Server-Flow-Chart

 

 

Example of hosts file entry

###################################################################
## This is an example HOSTS file created by			 ##
## www.snapfast.com					         ##
##								 ##
## All entries in a HOSTS file must be in the format of:	 ##
## 								 ##
## ipaddress	hostname					 ##
## 								 ##
## For example:							 ##
## 								 ##
## 192.168.1.1	mycomputer.mydomainname.com		         ##
##								 ##
## Notice that you must have a whitespace between the IP address ##
## and the hostname.  Also keep in mind that the hostname can 	 ##
## not contain any symbols like /,\,//, etc.		 ##
## 								 ##
## As a last note, you can the # symbol to make comments.  Any	 ##
## line that starts with the # symbol will not be parsed by 	 ##
## the operating system.  You can therefore use this # symbol	 ##
## to make comments as seen below.				 ##
## 								 ##
## Example valid entries found below.				 ##
###################################################################

# The localhost entry should be in every HOSTS file and is used
# to point back to yourself.

127.0.0.1	localhost

# My test server for the website

209.90.81.140	 snapfast.com
209.90.81.140    www.snapfast.com