For small networks dependent on an ISP for DNS services, heterogeneous networks where you can't count on every machine having a NetBIOS name, or when slow DNS propagation is an issue for remote workers, the traditional Berkeley-style names file has always been useful for UNIX hosts. By creating a line in the hosts file like
192.168.1.8 devserverusers can reference the machine at 192.168.1.8 as 'devserver' instead of using its IP address.
The same functionality exists in Windows operating systems as well, though it's buried a few levels deep in the file system. Where UNIX hosts typically place the file in /etc/hosts, Windows 2000 and XP place it in c:\WINNT\system32\drivers\etc (for Win2k), or c:\WINDOWS\system32\drivers\etc (for WinXP).
An example hosts file for several local hosts and a remote server might look like:
127.0.0.1 localhost localhost.localdomain 192.168.1.2 ix 192.168.1.3 caladan #department nfs server 192.168.1.4 giedi 192.168.1.5 salusa 18.104.22.168 sandcrawler production.duneco.com
Nothing other than a simple text editor (like Notepad) is needed to modify the file, which is plain text.
Note that you can give a host more than one name in the hosts file: just place the official name of the machine first, then put a space between each of its other names (called 'aliases'). Once you edit and save the file, the changes are instantaneous (no reboot required) and your assorted clients and tools will use the addresses mapped in the hosts file in preference to normal DNS lookups. You can also include comments by using the traditional '#', making it easier to annotate the hosts file for others. See the third entry in our sample above.
Some scenarios where modifying the hosts file might come in handy include:
- dealing with troublesome routing issues if a local host doesn't have an internally referrable name. In some cases, the latency routing out of a LAN induces can be substantial: this solution allows for direct access across the LAN.
- conveniently referencing a host that recently had its DNS records updated without having to use its numeric IP until the changes propagate
- allowing a small, heterogeneous network to operate without benefit of a DNS server.
- providing a table of hosts for a net-booting workstation
As with any shortcut, there are some caveats to using the hosts file:
- If you have a lot of machines and need to relate frequent changes in network addresses to them, it's best to consider investing the time and energy in deploying a modest internal DNS server.
- Since the hosts file takes precedence over DNS lookups, if a host's address changes, the hosts file will need to be changed or clients will continue to reference the old address.