Question

I made a small batch file to edit the hosts file:

@echo off
title Edit Hosts
color 0A

echo Warning: Please ensure you are running this program as an administrator.
set /p admin=Type Y or N and hit enter to continue.
echo.
IF '%admin%'=='Y' goto :edithosts
IF '%admin%'=='N' exit

:edithosts

cls

set /p block=Enter website to block:

echo 127.0.0.1    %block% > C:\WINDOWS\system32\drivers\etc\hosts

pause

The file does its job and adds whatever the user types in to the hosts file in the format

127.0.0.1 website

I checked to see if the websites were listed, and the hosts file is updating correctly. However, my browser can still connect. Is this somehow due to a cached copy of the site, or is there a flaw in the code?

Was it helpful?

Solution

While changes to the hosts file are immediate on Windows for quite a while, applications may or may not be affected immediately by it. Web browsers in particular will usually cache DNS lookups to save time for further requests. This cache is most easily cleared by simply restarting the browser. After you have done that, and the DNS lookup happens, it should pick up your modified IP instead.

OTHER TIPS

Newer versions of windows have default setting in Windows Defender to protect the hosts file. If you are on W8 or higher, you may also need to open Defender and add the hosts file to your exclusion list.

It's on the settings tab, click Excluded Files and Locations. Disregard red arrow, re-purposed an image from bing.

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top