I'm looking to edit the Windows TCP/IP hosts file (at c:\windows\system32\drivers\etc\hosts). I'm wondering, what is the best way to do this (on Windows XP)?

Let's say that I want to redirect, say, the site a.com to google.com. I can find the IP address of a.com, but what is the correct way to edit the HOSTS file? I'm thinking of using this syntax:

this.is.ip.address   google.com

However, two things:

  • Is this the correct syntax? If so, is there a way to do it without having to find the IP address of a.com? Would just putting a.com into where the IP address currently is work?
  • What is the best way to edit this file? I can't save to it because it is in use. Normally under these circumstances, I would go into Safe Mode and edit it from there, but is there an easier way (I'm thinking of killing some system processes...)?
  • 1
    I've never had a problem editing the HOSTS file from Notepad (Start > Run > notepad c:\windows\system32\drivers\etc\hosts). Are you running some AV program that's keeping it locked? – quack quixote Oct 4 '09 at 1:27
  • oopsey, i was confusing myself because im on a vista comp right now. thanks @quack – Maxim Zaslavsky Oct 4 '09 at 1:33
  • yeah, it only appears locked because you didn't try to edit it with admin privs. start notepad.exe as admin, then you can edit it fine – davr Oct 4 '09 at 4:56
  • One small addition as a comment, as it is not answering your question: In your sample, the user would call google.com from their browser, and would get redirected to a different IP address as specified in your hosts file. However, the browser will still send the Host header value as google.com - so the webserver behind the different IP address will need to be able to handle that. Just redirecting to e.g. Bing's IP might not work, as their webserver might not accept Host: google.com. – BennyInc Sep 15 '11 at 12:37

One easy way to edit the hosts file is using the freeware Windows program HostsMan.

HostsMan is a freeware application that lets you manage your Hosts file with ease

enter image description here

In Vista / Windows 7 it needs to run with administrative privileges, but it doesn't require going into safe mode.

| improve this answer | |
  • +1 Nice program will keep away a lot of difficulties – aibk01 Sep 15 '11 at 14:29

You can find the IP addresses of the sites using ping in command prompt or use a lookup tool. Also make sure you tackle all of the combinations:   a.com   www.a.com

The file is read only, you can edit it by right-clicking and going into properties, then turning off read-only temporarily until you're done editing.

| improve this answer | |
  • 1
    nslookup is probably more straightforward than ping for this task and it allows you to use another DNS server if you need it. – Joey Oct 4 '09 at 9:01
  • 2
    I doubt the 3rd line would work. (And you can also add the combinations on a single line.) – Arjan Oct 4 '09 at 9:46
  • You only need the IP address...no need to type an extra 4 characters. Ping works fine. – John T Oct 4 '09 at 15:18

You should be able to edit the hosts file with Notepad, but it is a protected file by default in Windows Vista. You probably couldn't save the file because of permissions issues, not because it was in use.

To edit the file with Notepad, open it under the Administrator account, by right-clicking Notepad in your Start menu and choosing the Run as Administrator command.

| improve this answer | |

when I need to edit my hosts file I am copying my hosts file to my desktop then edit it as I want.After editing it I move it to original hosts file's folder and replace the old file.I normally have admin rights while doing this.

| improve this answer | |

Disable AV and edit with notepad. The one thing I find annoying is you are unable to choose ports for those IP address's added in the host. Incase I want to change my web traffic from 80 to 8080 or 4444 for some unknown reason.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.