How to get the Hosts file to work in Windows

What is the Windows “Hosts” file, and how do you get the hosts file to work properly? Windows Hosts file not working? Problem solved.

When you type a domain name into a browser and press Go, one of the first things a Windows PC does is check the “hosts” file to see if there is anything relevant to that domain in it. If not, then it will use the normal IP address it finds from whatever DNS server list your PC is using. However, if it finds a domain name in the hosts file, it will bypass all that and go straight to the specified IP address to try and find the website there instead. The Hosts file is a type of text file. It is normally empty, but you can modify it to force the computer to look at a very specific IP address for domains, ignoring the Internet-wide DNS (domain name system) settings.

Why might you want to do this? Perhaps you are migrating a website from one host to another and want to check the website is working properly before changing all the Domain’s settings.

If you are reading this, you probably know all that already and are just wondering how on earth to actually get the hosts file to work properly! I found figuring this out to be immensely frustrating, but I got there in the end and it now works every time. I’m going to describe the process, as it works on my standard Windows 10 (pro) computer.

Firstly, a note on checking success. If the hosts file changes are done correctly, you should see the results immediately. You won’t have to flush DNS, or reboot your browser (at least with Chrome), or reboot your PC, or any of that stuff. Firing up a command prompt and “ping domain.name” will show you which IP is being used. It should match what you put in the hosts file.

EDIT: EASIER STEPS: Install Notepad++. Right click on the hosts file in C:\Windows\System32\drivers\etc and choose “open with” and then choose Notepad++ (you might have to search for it the first time to get it to appear in the list of apps). Make the changes as described below. Press the save icon in notepad++ at which point it will ask you if you want to reopen in admin mode – say yes (you won’t lose your changes). Accept the windows prompt to let it run in admin mode and then press save. This saves faffing about with copying files, although if you want to keep the original info, use “#” at the beginning of a line to comment it out and then add new lines.

Here are the summary steps to get a working hosts file

  1. Be logged in as a normal admin user.
  2. Create a new hosts file somewhere like the Desktop or Documents.
  3. Edit (using the correct format) and save the hosts file with Windows Notepad.
  4. Go to the real hosts file location and rename it.
  5. Copy and paste the new hosts file to that location, accepting the permissions popup.
  6. Rejoice

Here are the details

If you only have one Windows account on your PC, it’s pretty certain that it’s an admin.

Create a new text file in one of your normal folders (NOT in the real hosts file location – that will definitely not work). Call the file “hosts.txt”. I suggest you keep it somewhere useful for later so you can easily modify it next time you need it. Rename the file to remove the .txt file extension – Windows will warn you it might stop working, but accept anyway. If you can’t see the file extensions, then in File Explorer, click view > options > view (tab), and uncheck the “Hide extensions for known file types” box.

Right-click on the new hosts file and choose the “Open with” option. Your aim is to open it with Windows Notepad. Not any other sort of advanced notepad app that you might have installed. Just Windows Notepad. You shouldn’t need to open it as an admin or anything like that because you are already an admin.

Now is the time to add your server repointing information. Type the target IP address for the new server, add a single space and then type the domain name, without any “https://” and without any trailing “/”. Then do a carriage return and repeat the line above, but add a “www.” to the domain name. Finally, add a carriage return so that there is a single empty line underneath the two redirect lines. Without that final carriage return, the file will not work. You should be able to put the cursor in it, but neither move it along the line, nor be able to move the cursor down any more lines. So it might look something like:

12.13.14.15 domainname.com
12.13.14.15 www.domainname.com
MAKE SURE THIS LINE EXISTS, IS EMPTY & NO FURTHER LINES

Note that you cannot use the hosts file for https, only for http, so you’ll need to test your new server without SSL and then switch to https once happy.

Save and close the new hosts file.

Navigate to C:\Windows\System32\drivers\etc

Here you should find a hosts file – it might have some comments in it, but it doesn’t really matter what’s in it. Rename it “hostsbak”

Copy and paste your new hosts file from wherever you saved it into C:\Windows\System32\drivers\etc accepting any prompts to “continue”.

You should NOT try and paste your new file over the top of an existing hosts file – it won’t work. Make sure you do the rename step above.

It should now be working

You are now done. The hosts file should be working. Open a command prompt and ping the domain name to check which IP address is returned. If you followed these instructions exactly, it should be the one from your new hosts file. If it’s not, you either did something wrong along the way or there’s something unusual about your Windows install, in which case you are on your own.

How to turn it off again

I find that the best way to prevent the redirect is to just rename the hosts file, because then it’s easy to switch it on and off while checking sites, without needing to recreate or edit etc.

Things that normally go wrong:

  • Trying to edit the hosts file in C:\Windows\System32\drivers\etc
  • Trying to paste over the hosts file in C:\Windows\System32\drivers\etc
  • Trying to edit the new hosts file with anything other than Windows Notepad
  • Leaving the new hosts file as a text file, without removing the extension
  • Forgetting to add the www. line, especially if the original site has www. but your dev version will be on the TLD (without www.)
  • Forgetting the final empty line in the hosts file
  • Expecting to be able to access an encrypted address (https://) with the hosts file
  • Performing any of this as a non admin user
  • Using a security app that blocks access to the hosts file, e.g. “Webroot”

If you finally succeed after reading this, please comment ๐Ÿ™‚

Better yet, please donate a dollar / pound to my running shoe fund

And if you know anyone that wants to learn to run, I have a website for that too.

25 thoughts on “How to get the Hosts file to work in Windows”

  1. stop the frustration

    Thank You, this worked! Had tried so many other attempts.

    I think needing a carriage return after each entry is needed… no copy n past cheats and repeat.

    The empty carriage return with nothing on the last line needed.

    And the rename the hosts file and then copying in a new hosts file as the last step. The steps helped make it work, thanks!

  2. It won’t let me edit original host file. I have have full access to it but it won’t let me do anything to it even as an administrator. It won’t let me replace it either with new host file. It says the system has it open somewhere else but it doesn’t. I’ve tried everything but nothing seems to work. Read -only is checked in properties and every time I try to uncheck it, it works but then I look at it again and it is checked again.. Any help would be great!

          1. I ended up heading over to reddit and confirmed that! I turned turned the feature off and it worked right away!
            Thanks!

  3. Ping request could not find host yale. Please check the name and try again.

    Still doesn’t work

    127.0.0.2 somename

    ping somename

  4. Right click Notepad and run as administrator. That lets you save your edits directly. I’m a web dev and use the hosts file quite extensively for offline development. This restriction introduced in W10 was a pain in the arse.

  5. Just FYI this article is misleading when it mentions https:// as domain/hostnames and IPs have NOTHING to do with http:// or https:// which respectively indicate port :80 or :443 and the client/server protocol to use for exchanges.

    1. I thought it was clear that you can’t use those. Please let me know which bit needs rewritten so I can adjust the article accordingly. Thanks.

  6. Thanks for writing this, Charles. It has saved Jake and I (the guy who commented above) a few headaches.

    I can confirm the Webroot issue โ€” it does lock access to the hosts file, but you can turn that feature off in the settings, then you’re good to go.

  7. Thank you!!! I have had a problem with this for years as sometimes it would work and sometimes it wouldn’t. I think the blank line at the end was the key.

  8. hi, i tried to edit my server hosts file [win server 2016] but it won’t take effect
    i still can open web url in the browser

    even it try to bypass to local IP [127.0.0.1 webname]
    i’ve tried many solutions found on the net, but persist

    any suggestion?

    thanks

  9. Lawrence Gardiner

    Thank you for the article Charles.

    Just a heads up for anybody still having issues. Make sure you copy the file using windows – as opposed to a program like TeraCopy. This caused an issue for me.

  10. I did just as you said and I tried the ping option. It did ping the correct URL that I added in the host file. However when I enter the url (in my case it is wf.com) in a browser it opens WellsFargo.com which is not where I want it to go. I am trying to direct wf.com to an IP on my local network. What am I missing?

  11. good stuff ty.
    had to reboot after changes to get it to work- it may be something in my system.
    ping and typing address in browser then gets the specified address in the hosts file.
    nslookup however will still go to the configured dns server and bypass the hosts file.
    this is a useful tool to redirect gets to advertising domains

    172.168.50.100 app.swiftpage.marketing for example, is one entry i use to redirect get requests to null. I have been working for some time to eliminate this like this from my NIDS, and the info on the page with a reboot helped ๐Ÿ™‚

    1. It works for me still. Used regularly, although I now use notepad++ which detects lack of permission in etc folder and restarts in admin mode, saving changes as it does it. Still works in notepad though

Leave a Comment

Your email address will not be published. Required fields are marked *

Previous post
I've experimented with a lot of different ways of improving…
Scroll to Top