How to edit hosts file in Windows 8? Defender says malware detected

I've tried editing it in the same way as in Windows 7. E.g. opening Notepad/Notepad++ as Administrator and then opening the file and save it.

Right after saving it Windows Defender will say Malware Detected. Closing the hosts file and opening it again will give me the default hosts file, with my changes removed.

Malware Detected, Windows Defender is taking action to clean detected malware

How can I modify the hosts file and have my changes kept?

There are several reasons to edit the hosts file. To block web sites, but mainly my reason for modifying it is when developing web services. Mocking a server and have the program believe it's talking to the real server makes testing much easier.


Solution 1:

Windows Defender is treating these changes as malware. This is because changing the hosts file is something malware does. It's an effective way to trick people, since they think they are visiting the real website, for instance facebook.com, but are in fact really talking to a malicious (phishing) server.

What we need to do is disable Windows Defender's protection of the file. Note that this may open you up for the attack mentioned above, so it shouldn't be done unless needed.

Exclude the hosts file from Windows Defender

  1. Open up Windows Defender.
    • Press Win and write Defender and start the program.
  2. Go to the Settings tab.
  3. Select Excluded files and locations.
  4. Press browse
  5. Find and select the hosts file.
    • Normally located at C:\Windows\System32\Drivers\etc\hosts
  6. Press Ok.
  7. Press Add.
  8. Press Save changes.

enter image description here

Now the hosts file is not protected anymore, so we can finally:

Edit the hosts file

You need Administrator privileges to edit the hosts file, so open up your text editor as admin. How to do it with Notepad:

  1. Search for Notepad in your apps.
  2. Right click it and select Run as administrator.
  3. Press Yes if UAC appears.
  4. Press File -> Open and navigate to the hosts file.
    • Again, normally in C:\Windows\System32\Drivers\etc\hosts
    • You may have to select All files instead of Text Documents in the lower right corner.
  5. Edit the file to your liking and save it.