Fair warning - this is a pretty "101" post so it might be a tad ho-hum for some of you. It's surprising to me how many developers I meet who stare at me blankly when I suggest that the use their HOSTS file for one purpose or another. The HOSTS file has been around since the first networks - although it's gone by a few names over the years. In its current version HOSTS has been around largely unchanged since ARPANET and is in fact the predecessor to DNS (which has reached a venerable age in its own right). As a web developer, learning some easy fundamentals about the use of this file is a practical and fundamental skill - so let's explore it a bit shall we?
The basic purpose of a HOSTS file is to map an IP address to a friendly string. Take note that I said "string" and not "domain name". Domain names along with their hierarchy are a convention of the Internet writ large. While you may not think of it this way, a fully qualified domain name like www.example.com contains within it enough information to drill down into the Internet and find a specific DNS server which will yield up an IP address - but a HOSTS file is much simpler than that. Yes you can use it to map an actual fully qualified domain name but you can also use to map just about any old string to an IP address. In fact HOSTS short circuits DNS and kicks in before the IP stack even queries a name server at all.
On a windows box (any windows box going back to at least windows 95) the HOSTS file is located in <%systemroot%>\system32\drivers\etc. On Linux (or a MAC) HOSTS is located in the /etc folder (as in /etc/hosts).
Hosts is just a text file and can be edited by any text editor. Hosts has no file extension - which means you can't map it to be opened by default by a particular program. What I usually do is right-click and drag it to my desktop, then choose "Create shortcut here", then edit the shortcut target to be notepad.exe "C:\WINDOWS\system32\drivers\etc\hosts. This gives me a shortcut that will automatically open the hosts file in notepad for editing.
Host File entries aren't usually fancy or difficult. A typical entry uses simply Ip-address string as in 127.0.0.1 localhost. If you are looking at your hosts file for the first time that example (localhost) probably arleady exists - it's put there by default. In fact, if you have ever run a web server or product using a web based interface on your desktop and seen a URL that looked like "http://localhost:XXXX" you were using your hostsfile to resolve "localhost" to 127.0.0.1 (the sort of "local" IP address for your NIC card).
If you want to map a single IP to more than one string you can add them separated by a space. For example, if you added
127.0.0.1 localhost local localhosts bob samyou would be able to do ping bob or ping sam from a command line and see 127.0.0.1 return for either one..
If you are a web developer or designer a much better question is, how have you gotten by without using it up til now? You use it to develop web sites and to test web sites. Here's an example - let's say you are developing a site called www.habanaro-coldweathergear.com (Tag line: For when it's a little chilly). In some places perhaps you want to add absolute links or use a certificate or whatever. But when you are developing locally (on your desktop) you are using apache and http://127.0.0.1 instead. Ahah! But using the hosts file you can add:
127.0.0.1 www.habanaro-coldweathergear.comNow you can open your browser and type in www.habanaro-coldweathergear.com and Voila! Your are serving the actual site name off of your local desktop server. Pretty neat huh?
Or let's say your customer tasks you with moving his site from one hosting company to another. You call the fabulous folks at Edge Web and set your customer up with a smashing new server. Edgeweb gives you an IP address of 18.104.22.168 and permissions to the server. You load up your server with the new code and set up your data source. But how do you test this production code without changing the DNS? Perhaps the shopping cart software has (as it should) redirects to insure that SSL is used on checkout. That means that even if you start out using the IP address to test the code will redirect you back to the real "live" server right?
But with a host file entry for 22.214.171.124 www.habanaro-coldweathergear.com you can browse the new server and test your shopping card code exactly as if DNS was already changed.
Ok, there were few gotchas to using a host file. First, when you make a host file entry that changes an IP for a domain you have already visited you may need to "clear your resolver cache". The resolver cache is a part of the IP stack that stores mappings of ip addresses and strings it has already recently resolved. So if you have already visited www.habanaro-coldweathergear.com you will already have the DNS provided IP address in the cache. To make the change you will first have to clear the cache and then re-request the domain. On windows to clear the cache, make sure your browser is closed, open a command line prompt and type:
ipconfig /flushdnsOn a MAC I'm not sure how to do it but if you ask a MAC user he will probably just say "that sounds difficult... Mac's don't have problems like that" so I'm sure it "just works" by default. Or if it doesn't work it's because Bill Gates or some other Satan-like personage designed the IP stack. Meanwhile, after clearing the cache, ping your new host entry to make sure it is resolving to the new IP and then load it up in the browser.
The second caveat would have to do with hijacking. A common exploit is to hijack the HOSTS file and alter its contents. A malicious user could host his own "Google look-alike" page for example and if he had access to your host file he could point your browser to his own IP address instead of Google's - effectively hijacking all of your Google searches for his own nefarious purposes (probably sending them to your mom). In fact such hijacks are indeed a common form of malware. So if you see anything unusual in your hosts file that you didn't add you should immediately start scanning and searching your machine for malware - and start that long email to your mother on why exactly you were searching for "Russian women who own sheep". On the other side of things this hijack issue can make editing the host file difficult because security software and anti-malware software will attempt to protect the host file from alteration. So that's a hurdle you may have to get around as well.
Meanwhile, if you are serious web developer your skill set will be much improved by learning this simple trick. The Muse looks forward to hearing from all of you who are now going to tell me all the "much better ways" you have of doing this. Oh... and I was kidding Mac users. Some of my best developers are Mac users you know (some of my best friends too :).