martes, 20 de julio de 2010

How to (easily) fool your host into thinking a name is mapped to a certain IP address

Hi!

Very recently I've been involved in setting up a site that uses Google Maps API. When you want to use the API you have to create a key that is generated according to the name of the site but, as of now, the DNSs of the name we want to use are not pointing to our hosting. I could use the IP address of the host where the site is, right? Well, no. It's a shared hosting so requests have to be made by name.

In this case I have to fool my host into thinking that the name we want to use is mapped to an IP without going through public DNS resolution. I could set up a DNS service just to serve this name and forward everything else but it sounds like an overkill, doesn't it?

In GNU/Linux (and I'd dare to say in any POSIX compliant OS) there's a simpler way to do this:

/etc/hosts

In this file we can map names to IP addresses at will. So this thing I want to do could be performed by adding a single line to my /etc/hosts (not a real example... IP address and name are bogus):

85.23.54.87 mydomain.com

Then I could go to http://mydomain.com and I'd be able to see the site that uses Google Maps, even if the name is not "officially" public.

Notice this trick (surprise, surprise) also works in Windows (which is POSIX compliant, right?), only the file that has to be edited is System32\drivers\etc\hosts

Additional information:
man hosts
man nsswitch.conf

2 comentarios:

  1. There is no trickery here. This is DNS 101.

    ResponderEliminar
  2. There's a Gotcha - if you later get a incorrect name resolution seemingly from DNS, be sure to check /etc/hosts to make sure something has not been hard-coded locally!

    ResponderEliminar