A Hosts file is a local file that is used to map an easy to remember name to an IP address. Pretty much the same concept as using a DNS. Computers in general, rely on DNS servers for address lookups but operating systems of a computer offer an override in the form of the hosts file. You can specify the IP address for a given host name in your computer's hosts file and that address overrides any information coming back (or failing to come back) from a DNS server.
The fileThe file is simply named hosts.
The table below provides a quick reference on where to find your hosts file on different computer types.
|Operating System||OS Version||Location|
File StructureThe format of the file is the same across machines and Operating Systems. In each case, all you need to do is identify the IP address you want requests to go to and add it to your hosts file using the formats in the example below. Windows and most Unix/Linux systems will recognize the change immediately. From OS X 10.10.4 onwards, Apple has discarded discoveryd and has substituted it with mDNSResponder. As a result, to flush DNS caches in OS X Yosemite and Mac OS X El Capitan, and most likely future releases, following is the command string: sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder; say cache flushed
Example# My Sample Hosts File
192.168.1.1 localrouter # local home router
192.168.1.3 mediaserver # local Media server
126.96.36.199 site1.com www.site1.com
- First Line: # character is a comment. A hash character causes the computer to stop reading a given line.
- Second and Third Line: represents the standard way to enter the IP address and name for a device or website. Note: the # denotes the start of a comment or description. Will not be processed by the computer
IP address goes in the first column
Name of the device goes in the second column (use a space or tab between columns)
Followed by a descrtion (note the # character)
- Fourth Line: provides an example for providing an IP address, a hostname and an alternate hostname. A host that has multiple host names will require and entry for every subdomain such as "www," "ftp" or "dev,".
Hosts file are a convenient way to eliminate the need to constantly remember and type IP addresses. The best practice to limit its use to local devices and clean up temeporary entries. The convenience of local hosts file is a recipe for countless hours troubleshooting access problems. As an example, a website that has an entry in your local website moves to a different server. Accessing the website from your local PC will not work because the local hosts file continues to point to the old IP address. You could likely spend hours trying to determine why it works on other machines except this one.