How come hosts file redirection fails?

The hosts file doesn't work this way. You can only use it to map an hostnames to IP addresses, not to localhost.

For your case you'd use 127.0.0.1 www.google.com, i.e. map www.google.com to 127.0.0.1.

If you want to map more hostnames to a singe IP, you just add those hostnames in the same line, e.g. 127.0.0.1 www.a.com www.b.com.


A web browser isn't the best way to check if your hosts syntax is correct. Try executing

ping www.google.com

and verify that it pings 127.0.0.1.

The reason why Chrome appears to ignore your hosts file is caching:

If Chrome has already queried the IP lately (the definition of lately most likely depends on the time to live (TTL) returned by the DNS server), it will bypass the hosts file, since it already knows the correct IP. This is done to speed up web browsing.

To make Chrome respect the new entry, do the follwing:

  1. Edit /etc/hosts as @Renan described.

  2. Go to chrome://chrome/settings/clearBrowserData.

  3. Choose since the beginning of time.

  4. Check Empty the cache, but uncheck everything else.

  5. Click Clear browsing data and wait for it to finish.

  6. Restart Chrome.

Chrome should respect your hosts file now.