How come hosts file redirection fails?
Solution 1
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
.
Solution 2
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:
Edit
/etc/hosts
as @Renan described.Go to
chrome://chrome/settings/clearBrowserData
.Choose
since the beginning of time
.Check
Empty the cache
, but uncheck everything else.Click
Clear browsing data
and wait for it to finish.Restart Chrome.
Chrome should respect your hosts
file now.
Solution 3
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.
- Go to
chrome://chrome/settings/clearBrowserData
. - Choose since the beginning of time.
- Check Empty the cache, but uncheck everything else.
- Click Clear browsing data and wait for it to finish.
- Restart Chrome.
Chrome should respect your hosts file now.
Related videos on Youtube
David Faux
Updated on September 18, 2022Comments
-
David Faux almost 2 years
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:
- Type
sudo vim /etc/hosts
in terminal. - Inputted
127.0.0.1 www.google.com
into the hosts file. - Saved and quit vim.
- Type
-
David Faux about 12 yearsThank you. I just changed the /etc/hosts file on my Mac to
127.0.0.1 www.google.com
. However, google.com still leads to the Google home page. Do I need to refresh anything? Should I restart my computer? -
Renan about 12 yearsYou shouldn't need to. Try adding
127.0.0.1 google.com
(without the www) after this line and check whether it works. -
David Faux about 12 years
127.0.0.1 google.com
isn't working either. I'm not sure what's going on ... I know that/etc/hosts
is a soft link to/private/etc/hosts
on a Mac, but I don't think that helps at all. -
Renan about 12 yearsDoes
127.0.0.1 www.example.com
, then acessing www.example.com, works? -
David Faux about 12 yearsNo, example.com still oddly leads to iana.org/domains/example on my Mac with
127.0.0.1 www.example.com
as my /etc/hosts file. -
Dennis about 12 yearsA technicality:
hosts
maps hostnames to IP addresses, not vice versa. -
Synetech about 12 years@Dennis, that’s what I was going to say, but then again, it really does work both ways when you think about it, because it creates an association between two items.
-
Synetech about 12 yearsI learned this a long time ago with IE6. If I made a change to the HOSTS file while IE was still open, I would have to specifically close it and run it again for it to pick up the changes. This goes for most programs. Some programs however (like versions of Chrome from the past year or two) will actually re-read the HOSTS file periodically, so if you make a change, just wait a minute or two, then refresh (no need to clear the cache). (This re-reading policy is handy sometimes like this, but usually pretty bad in reality since it is wasteful, and particularly bad if the HOSTS file is large.)
-
Dennis about 12 years@Synetech: Chrome picks up changes in
hosts
instantly, as long as the entry isn't a hostname that's already cached. I assume that it checkshosts
only if the TTL has past. With big web sites, that can take a while. -
Synetech about 12 yearsI don’t know the specifics about how different versions go about re-reading the HOSTS file, but you can use Filemon/ProcMon to see it in action;
chrome.exe
will occasionally read the entire file. -
hakunin over 11 yearsThere is a less brutal way of flushing chrome's DNS cache superuser.com/questions/203674/…
-
Dennis over 11 years@hakunin: Neat. On Chromium 25.0.1364.160 Ubuntu 12.04, this isn't even necessary anymore. I checked in
net-internals/#dns
, and the host cache gets cleared automatically when/etc/hosts
is altered.