13

I am trying to redirect google.com to my local machine for fun and learning with this /etc/hosts file on my Mac.

127.0.0.1 www.google.com

However, www.google.com still maps to the Google home page when I visit it in Chrome on my Mac. Why?

Basically, this is all I did:

  1. Type sudo vim /etc/hosts in terminal.
  2. Inputted 127.0.0.1 www.google.com into the hosts file.
  3. Saved and quit vim.
David Faux
  • 4,899

3 Answers3

12

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.

Renan
  • 8,062
10

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.

Dennis
  • 50,701
1

My answer is a combination of the above because I am connecting to my office via Fortinet SSL VPN for Ubuntu 16.04.

The first thing I had to do is bring up my terminal console and run the following command:

sudo nano /etc/resolvconf/resolv.conf.d/base

I added the following example:

searchdomain domain.local
nameserver xxx.xxx.xxx.xxx
nameserver xxx.xxx.xxx.xxx

Then I saved the file by pressing CTRL-O and closed nano by pressing CTRL-X.

I then did the following and it worked perfectly:

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

Edit /etc/hosts as @Renan described.

  1. Go to chrome://chrome/settings/clearBrowserData.
  2. Choose since the beginning of time.
  3. Check Empty the cache, but uncheck everything else.
  4. Click Clear browsing data and wait for it to finish.
  5. Restart Chrome.

Chrome should respect your hosts file now.

slhck
  • 235,242