Windows Hosts File
If you are trying to resolve an external name to a local machine - for example your web site, and you don't have your own internal DNS server, then you should create a "hosts" file for each machine.
A hosts file is like a local DNS server. It can have different information on what to resolve names to than is on the public Internet.
If you on a corporate network you should check with your network admin to ensure that hosts files are not already in use.
If you have an internal DNS server, then a Hosts file should NOT be used. Use split DNS instead.
A hosts file overrides DNS information, so can cause problems. You also cannot put MX records in to a hosts file, so their use with email delivery is limited.
To create or modify a hosts file, follow these easy instructions:
- Search your system for either "hosts" or "hosts.sam" (which is a sample file). You DO NOT want lmhosts or lmhosts.sam.
- If you don't have a hosts file, then you should rename the "hosts.sam" file removing the ".sam" suffix. A "hosts" file doesn't have an extension.
- Open the "hosts" file in notepad. The easiest way to do this is to hold down the shift key and then right click on it. You will then get an option "Open With..." where you can choose "Notepad".
- Once the file is open, you will see the format that you need to make entries. Enter the IP address first, then a tab, then the name. Once complete, save the file. Leave it open while you test it.
- To test your file, open a command prompt and enter "ping <name entered>" minus the quotes, where <name entered> is the name you just put in to the hosts file. You should find that it resolves to the local IP address instead.
- You can make additional entries, you can even have multiple names for the same IP addresses.
Here is an example hosts file with some entries already made:
# Copyright (c) 1993-1999 Microsoft Corp.
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
# For example:
# 220.127.116.11 rhino.acme.com # source server
# 18.104.22.168 x.acme.com # x client host
Using Hosts file to Override DNS for MX Records
One clever trick with a hosts file though is where you can use it to override where email goes based on the MX record.
When email is sent, two DNS lookups occur. The first is to the MX record. This tells the sending server which host is responsible for email. The second is to lookup that host. It is for this reason that you can have the MX record host in a completely different domain to the one it is receiving email for.
Therefore if your server is doing public DNS lookups for a host, but you need it to send to a local host, then you could use a hosts file to ensure that the second step of the above process resolves to a local IP address, rather than a public one. A web server in a DMZ is an ideal example of this need, because most firewalls will not allow the traffic to come back in again from an internal host.
However putting entries in to your own local DNS server via a split DNS system is still much more efficient and easier to maintain. A hosts file should only be used where local DNS is not possible.
If you do use a Hosts file, remember to document that has been done!