Can updating the hosts file in Windows via batch file cause harm?

1

I have this script that I have saved as a .bat file. Can it hurt a computer?

echo # >> %Windir%\System32\drivers\etc\hosts
echo 0.0.0.0    www.facebook.com >> %Windir%\System32\drivers\etc\hosts

I was just wondering if this can have any side effects on the computer at all like a corruption of something? I don't think it is possible. All it does is make Facebook show a DNS error by changing the hosts file.

Megaeverything

Posted 2013-03-06T01:13:50.890

Reputation: 1 437

If you want to get rid of Facebook there are better alternatives (addins/extensions) for the various browsers. This will work, but facebook.com has so many subdomains it's almost pointless. Also a proxy (Privoxy etc) can filter web sites and will therefore remove any links to facebook, for example. Anyway, without fuzzy matching this makes not much sense and the hosts file doesn't allow for that. – 0xC0000022L – 2013-03-06T01:22:47.360

1This will have to be run as administrator to work, in case you didn't know. Also, they could just use facebook.com, the ip address, facebook.us, etc... I would recommend another method. – cutrightjm – 2013-03-06T01:26:22.200

Answers

1

Normally I've seen people use localhost instead of 0.0.0.0. I don't know the reason for this; but it shouldn't be a big deal.

Other than that; this script is completely okay. Note that you will not be able to use Facebook after running this script but I think that's what you're trying to accomplish.

danielcg

Posted 2013-03-06T01:13:50.890

Reputation: 560

2The difference between 0.0.0.0 and localhost is that the first is not a valid IP address, and the second is the IP address of the local PC. Using localhost will redirect packets to the local PC rather than nowhere. If, for instance, you have a web server on the local PC then it will serve up a page rather than the request going nowhere. Either is valid. It depends on what you want. – Wayne Johnston – 2013-03-06T01:23:21.620

0

Its pretty safe. If you are worried, the only thing you need to do is back up your %Windir%\System32\drivers\etc\hosts file, and then restore it if things don't work as expected.

You might also want to add a line

echo "127.0.0.1    localhost" >> %Windir%\System32\drivers\etc\hosts

To the bottom of the file as its good to have that defined.

It is possible that if any other file adjusts your "hosts" file (eg an "ad blocker", this script would revert that change)

davidgo

Posted 2013-03-06T01:13:50.890

Reputation: 49 152

0

The script works well as long as the last line of the hosts file has been terminated.

If the last line is (no linebreak at the end)

127.0.0.1    localhost

it will read

127.0.0.1    localhost#

afterwards. This only works since # is the comment character; any other character would break a couple of things, but the "damage" would be easily reversible.

To insert a linebreak at the end of the hosts file, execute

echo.>> %Windir%\System32\drivers\etc\hosts

before the other commands.

Dennis

Posted 2013-03-06T01:13:50.890

Reputation: 42 934

1Thanks the reason i put the # in there was for that exact reason. some hosts files dont have another line so i put # there to make it start a new line for the actual remapping. becasue that file dosnt count the # as anything. so does the "." make a new line in the file? – Megaeverything – 2013-03-06T01:27:53.307

1Yes, echo. (no space between echo and .) prints a newline. – Dennis – 2013-03-06T01:28:38.873

I checked and the # does no harm, but I still think the newline in the cleaner approach. – Dennis – 2013-03-06T01:34:57.527

0

No side effects at all. It just redirect the Facebook to your local loopback 0.0.0.0. Simple and effective way to make the facebook dns error.

Zhengyang Liu

Posted 2013-03-06T01:13:50.890

Reputation: 173