Ask Leo!

Can I fake the DNS ip lookup to test my website?

Home » Internet

Summary: In case you need to test your website, there are ways that you can fake the DNS IP lookup to be able to do so. We'll take a look at how that's done.

Can I fake the DNS ip lookup to test my website?

The situation is this: you have a working web site, and you're about to move it or make dramatic changes to it. So you make a copy of the web site at a different ip address, possibly with a different temporary domain name. You make all your changes and then want to test it out using the correct URL - the "real" domain name. How can you do that without actually changing the internet's DNS entry for your domain?

It's not hard at all.

Let's look at the example in more detail. Say you're the proud owner of http://example.com, and it lives on a server at IP address 1.2.3.4. You're about to make major changes to it, so you create a new domain, http://test.example.com on a different server with a different IP address - 1.2.3.5.

After you make your changes, you want (or in come cases you may need) to test the new server at the new address but use the "correct", real name of the site: example.com and not test.example.com.

In other words, you would like to change example.com to point to 1.2.3.5 instead of 1.2.3.4. Ideally, though, you would not want that to be true for everyone. In fact, you want that to be true only for you. While you're testing your new site, you want the world to continue to only see example.com at the old IP address 1.2.3.4.

Enter the "hosts" file in Windows. This text file contains IP-to-name mappings that override DNS. In fact, if Windows finds an entry in the DNS file, then the internet DNS is never even consulted.

The hosts file lives in c:\windows\system32\drivers\etc\hosts (where "c:\windows" is your Windows directory). It's just a text file, and each line that doesn't begin with a "#" character is of the form:

number space name

So for our example, we would edit this hosts file and add the following line:

1.2.3.5 example.com

Now, on our machine only "example.com" will reference the new IP address, 1.2.3.5, instead of whatever DNS has listed.

There are a couple of caveats:

  • You'll need an entry for every and any domain or subdomain you're testing. So for example if you needed to test both "example.com" and "www.example.com", you would create two entries.
  • You must remember to remove the entries from your hosts file after your testing is complete. Remember, the hosts file overrides DNS, so if the real IP address for example.com changes again sometime in the future, this machine will continue to believe it's at whatever IP address is listed in hosts.

If this approach sounds vaguely familiar, it's also a technique used by viruses and spyware. By placing entries into your hosts file, malware writers can redirect you to their sites or prevent you from accessing other sites. And that brings us to a final caveat:

  • A properly configured anti-spyware or anti-virus program may prevent you from making changes to your hosts file. It's safe to temporarily override these programs settings while you perform your test, but be sure to restore their real-rime protection when you're done.

Related:

Article 3137 | Posted March 8, 2005

Recent Comments
12 Comments

also useful for looking up the current ip of your domain is this online lookup tool: http://www.dnswatch.info

Posted by: workaholic at October 6, 2005 11:14 AM

Quote:

Remember, the hosts file overrides DNS, so if the real IP address for example.com changes again sometime in the future, this machine will continue to believe it's at whatever IP address is listed in hosts.

EndQuote

This is not true, all over the microsoft website it is said that the DNS is read FIRST and the HOSTS file read SECOND. This is a problem for me because I am trying to give a different IP for a hostname and I am unable to because the hostname is first resolved via DNS so Windows does not bother checking HOSTS.

Posted by: Karl at October 19, 2005 10:03 PM

Excellent article. I'm glad that I found it. Two thumbs up!

Posted by: Marci Weinberger at May 30, 2006 10:34 AM

Is there a way to forward the index page to the test site using an html tag?

Posted by: Chris at September 29, 2006 9:18 AM

hi i will to know how i can block a website..its meebo.com.

Posted by: Abegail at January 18, 2008 3:23 PM

I can't seem to redirect an IP to an IP in the hosts file. I have a desktop application which references an IP that is no longer valid and I don't have the source code. I want to fake the PC into thinking that the old IP is referring to the new IP.

Say my old IP is 1.2.3.4 and my new IP is 1.2.3.5, I want the host file to say something like this:

1.2.3.5 1.2.3.4

This doesn't work though, any ideas?

Posted by: Dave at April 23, 2008 11:37 PM

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

What you're trying to do isn't possible. An IP address can't
be mapped to another IP address in that way. The hosts file
is all about mapping *names* to addresses.

I don't know of a solution for your application, short of
trying to patch the binary.

Leo


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)

iD8DBQFIFNF1CMEe9B/8oqERAgUdAJ9AMJFBqHZ/Xx/aSnOQDHrY4ZmFrQCfQG0t
PHt65hGLxzrMIOZ08/tgZig=
=0JVy
-----END PGP SIGNATURE-----

Posted by: Leo at April 27, 2008 12:18 PM

Hi, I would like to know how to do the remotely from hometo office with dynamic IP address. Using RealVNC free edition.

Posted by: Lai at June 5, 2008 9:36 PM

so we can say in short that we can't change our website DNS unless YOU changing you hosting server. thanks leo for the useful article.
http://www.fosdir.com

Posted by: peter at July 20, 2008 10:18 PM

Karl wrote: (quote) This is not true, all over the microsoft website it is said that the DNS is read FIRST and the HOSTS file read SECOND. (end quote)

Never mind the documentation - a 2 minute test shows that the hosts file overrides your DNS server. Try putting microsoft.com or whatever other host name you like in your hosts file with a redirection to some other IP, and then do a NSLOOKUP on that host name. You will see that it goes to the IP you specified in the hosts file, not what your DNS server says. What is confusing though is that even though it got the answer from the Hosts file, NSLOOKUP will say that it got the answer from your DNS server - but it is lying.

Posted by: jinlye at October 6, 2008 4:22 AM

Post a comment on "Can I fake the DNS ip lookup to test my website?":






(Email Address will not be published.)

Remember Me?

By popular demand...
my tip jar
Cuppa Joe
Buy Leo a Latte!

(you may use HTML tags for style)

New!

RSS feed Subscribe to the RSS Feed specifically for comments on this article.

Before commenting, please...

Please wait. Your comment is being processed ...


Ask Your Question:


ask-leo.com
Web

Stay Informed

Weekly Newsletter

Archives

By Category
By Date

Advertisers

Advertise on Ask Leo!

««   »»

Question? - Ask Leo!
Who is Leo?
Link to Leo!

Terms, Conditions & Privacy