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.
Here are the summary steps to get a working hosts file
- Be logged in as a normal admin user.
- Create a new hosts file somewhere like the Desktop or Documents.
- Edit (using the correct format) and save the hosts file with Windows Notepad.
- Go to the real hosts file location and rename it.
- Copy and paste the new hosts file to that location, accepting the permissions popup.
- 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 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.
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!
Fantastic. Glad I could help!
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!
So it won’t let you rename it? And you are an admin?
Same thing happens to me. It won’t let me rename the original host file. Says it’s open in WRSVC.
Are you using Webroot? It locks access to the hosts file, but I think you can turn the feature off.
I ended up heading over to reddit and confirmed that! I turned turned the feature off and it worked right away!
Thanks!
Use Take Ownership and you will be able to edit it.
Still doesn’t work
127.0.0.2 somename
ping somename
Did you add the blank line in the file?
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.
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.
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.
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.
Excellent!