Categories

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna, tincidunt vitae molestie nec, molestie at mi. Nulla nulla lorem, suscipit in posuere in, interdum non magna.

Add or Map a Custom Hostnames to IP Address without a DNS Server

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

1
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters

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

127.0.0.1       localhost

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…

127.0.0.1    example.com

Now what if you want to block a website like MySpace? Add the following line

0.0.0.0       myspace.com

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.

4 comments to Add or Map a Custom Hostnames to IP Address without a DNS Server

  • [...] Xem cách điều chỉnh hosts file. – Đây là một số thí dụ địa số IP, tuy nhiên thay số 69.63.178.11 bằng số [...]

  • to domain name

    hi guys…

    hi guysI would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well and i have start my own blog now, , thanks for your effort…

  • Hester Vital

    Apple was not the first to release a new tablet computer, just as it was not the initial to release an MP3 player or cell phone, but it may well be the company that succeeds in making the tablet computer truly catch fire with the public.Apple has a tremendous head start with 140,000+ apps for the iPhone, and it wisely designed the iPad to take advantage of them. The iPad can run iPhone apps at their original size, or in pixel doubled mode” twice the size as on the iPhone, so that they nearly fill the iPad screen. Developers can easily rewrite their iPhone apps specifically for the iPad in such a way as to take advantage of its new features.

Leave a Reply