Editing the hosts file

4

2

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...)?

Maxim Zaslavsky

Posted 2009-10-04T01:09:44.877

Reputation: 1 750

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 – 2011-09-15T12:37:04.577

1I'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 – 2009-10-04T01:27:23.090

oopsey, i was confusing myself because im on a vista comp right now. thanks @quack – Maxim Zaslavsky – 2009-10-04T01:33:20.057

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 – 2009-10-04T04:56:56.573

Answers

9

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.

therefromhere

Posted 2009-10-04T01:09:44.877

Reputation: 7 294

+1 Nice program will keep away a lot of difficulties – aibk01 – 2011-09-15T14:29:19.500

2

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:

111.111.111.111   a.com
111.111.111.111   www.a.com
111.111.111.111   222.222.222.222

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.

John T

Posted 2009-10-04T01:09:44.877

Reputation: 149 037

1nslookup is probably more straightforward than ping for this task and it allows you to use another DNS server if you need it. – Joey – 2009-10-04T09:01:54.737

2I doubt the 3rd line would work. (And you can also add the combinations on a single line.) – Arjan – 2009-10-04T09:46:36.643

You only need the IP address...no need to type an extra 4 characters. Ping works fine. – John T – 2009-10-04T15:18:01.727

1

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.

i-g

Posted 2009-10-04T01:09:44.877

Reputation: 980

0

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.

NT.

Posted 2009-10-04T01:09:44.877

Reputation: 1 677

0

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.

mikedopp

Posted 2009-10-04T01:09:44.877

Reputation: 41