There’s been several times where I had an Apache server running locally on my computer as a development (or dev) server. Now if this is on my home network, or if your system administrator won’t give you your own custom host name then you have to go the DIY (do it yourself) route. But, often times, I have to hard code host names, or I need several different websites hosted on my personal machine. Well that’s where the hosts file comes into play.
Another example of needing a custom ip address is at my company, I’ve needed to check the status of a printer and if I want to try the access it via its hostname http://printername it wont work because the SysAdmin has shared the printer on the network without adding a custom dns host name, even though it has a static IP address on the network. So I added the name of each printer to my hosts file so now I have no problem accessing any printer via its name, without having to first look up its IP address.
Also, if you intend to block a website by its hostname then this is also the solution for you.
You can find the hosts file in the following folder…
Windows 95, 98, and ME in the %WinDir%\ directory
NT, 2000, XP, 2003, Vista, 7 in the %SystemRoot%\system32\drivers\etc\ directory (%SystemRoot% is almost always C:\Windows)
iPhone and iTouch in the /private/etc/hosts directory
Linux in the /etc/hosts directory
Also, a note for 0.1% of Windows NT, 2000, XP, 2003, Vista, and Windows 7 users, the actual location may differ, to find the real location you’ll need to dig into the registry key, give the following a look up
and look for the following string key DataBasePath. On my computer it was declared as %SystemRoot%\System32\drivers\etc I know that’s what I said it would be above, I just want all you SysAdmins to be aware of that 0.1% chance.
Lastly once, you finish making any modifications you’ll need to flush your locally cached DNS on your computer.
In WinXP you can edit the c:\windows\system32\drivers\etc\hosts file by opening it in notepad or any ascii text editor. By default the last line you’ll see is
but other programs can change this. The IP address 127.0.0.1 is always the ip address of the local computer. It doesn’t matter if the computer has a different IP address on the network, 127.0.0.1 is considered a loop back address, in other words, it always points to your computer.
you can add a new hostname followed by the ip address in the text file that’s it, it’s just that simple! Let’s add example.com to our local host names, now if we had apache installed, this ip address would always map to our computer just by adding the following line to the end of your hosts file…
Now what if you want to block a website like MySpace? Add the following line
The problem with this approach is it only blocks the hostname myspace.com, it leaves www.myspace.com and login.myspace.com completely accessible, and other hostname that I don’t know about (or worse yet, any future host names that they decide to add). So let’s block all we we can by pointing them to a null IP address
0.0.0.0 myspace.com 0.0.0.0 www.myspace.com 0.0.0.0 login.myspace.com
Ok great, I added my login.myspace.com to the hosts file, but I can still access it, what gives?!? There is two possible fixes for this solution. First close all of your browsers, for example, if you can still access the domain in Internet Explorer or FireFox it’s because the IP address has been cached, close the browser so that the browser can "forget" the IP address. Now before you reopen the application we also need to clear the DNS entry from Windows. We can clear a DNS entry from windows by clicking Start, Run typing ipconfig /flushdns then clicking ok. This will flush the ip address address stored by the operating system. You should now by able to open FireFox and Internet Explorer and verify that they no longer have the IP address cached.