'Why is my hosts file entry being ignored by the browser? [closed]

I mapped a website with the IP of www.facebook.com to load Facebook when I request a different url (let's say www.x.lk) particular site. It worked well. But when I tried to map the inverse, which should load www.x.lk when I ask for Facebook, it doesn't work. The original Facebook site is being loaded. What is the problem? Can anyone help me out here?

    # Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#   127.0.0.1       localhost
#   ::1             localhost
104.16.73.189 www.facebook.com


Solution 1:[1]

Are you using Chrome by any chance? If you aren't, then try flushing your system's DNS cache (ipconfig /flushdns on Windows).

Chrome however has its own DNS cache. I found that going to chrome://net-internals/#sockets and clicking "Flush socket pools" worked.

If you don't want to have to do this manually every time, the Host Switch Plus extension might help.

Note: you can also go to chrome://net-internals/#dns to check what the URL is resolving to, but the "Clear host cache" button didn't fix this particular problem for me.

Solution 2:[2]

I recently came across this issue in Firefox and was scratching my head for an explanation. At first I thought it was an issue with how DuckDuckGo was configured, then I remembered a Register article about Mozilla enabling DNS over HTTPS (AKA DoH) and wondered if that might be the cause.

Below you'll find a solution followed by some information and a potential fix for any privacy implications if you're interested.

Solution (Firefox):

This solution is quite similar to Chrome - just look for "Use secure DNS" and ensure it is unchecked - but I don't use Chrome so here are the instructions for Firefox.

  1. Go to the Options page.
  2. In General, scroll down to "Network Settings" (or type "DNS" into the options search box).
  3. Click Settings.
  4. At the very bottom, you'll find a checkbox with the label "Enable DNS over HTTPS". This is enabled by default in recent versions. Enabled DNS over HTTPS
  5. Untick/uncheck/deselect this checkbox. Disabled DNS over HTTPS
  6. Press "OK" and exit settings.
  7. Attempt to browse to the site in your hosts file - facebook.com in your case. It should now be handled by your hosts file entry.

Explanation

I won't go into full explanation of DNS, HTTPS or DNS over HTTPS (often abbreviated to DoH). For the vast majority of users however, it should be said that, disabling DNS over HTTPS is probably not a good thing to do for privacy reasons. However, there are cases where it can be counter-productive, and ignoring custom domain entries in the hosts file is one of them.

DNS over HTTPS means that any DNS queries your browser makes are resolved by the provider (Cloudflare in this case). This means that DNS queries in Firefox are no longer resolved by the OS (the old default) and instead, your DNS queries are now sent securely to Cloudflare.

This is not the place to discuss whether opt-out DoH is right or wrong, so I'll simply offer an alternative that I use as I work from home - Pi-Hole.

Pi-Hole is extraordinarily useful if you happen to have a Raspberry Pi - and if you don't, you can get one quite cheap. It's great because you can plug it into your router and tell it that you want Pi-Hole to act as both a DNS resolver and DHCP server, amongst other things. The former (DNS resolver) is great because it negates any privacy issues with DNS resolution you may have as a result of the above solution. And the latter (DHCP server) is great because it means that all machines connecting to your router will now have their DNS requests routed through your Pi-Hole.

It should also be noted that another huge benefit of Pi-Hole is the blocking of telemetry and ads!

Solution 3:[3]

I had the same problem and I tried everything and it didn't help. Eventually I realized that I was using domain name instead of www version for my site:

so instead of for example:

ip.address example.com

you should use www prefix if that's what you're testing with the actual link not just domain, for instance for website http://www.example.com:

ip.address www.example.com

it should work then.

Also make sure your network card does not use any custom dns such as google or cloudflare but automatic from your ISP.

Solution 4:[4]

I found solution at last.. In chrome, chrome://flags/ ->Secure DNS lookups -> disable.

Solution 5:[5]

STEPS OVERVIEW

  1. Verify the problem
  2. By trial and error, start with the most common solution
  3. Continue until the problem is fixed.

Verify the Problem The problem can be with a browser or with settings not part of the browser. It is possile to have two browsers, such as Chrome and FireFox that aboth have the problem, each for their own reasons, or through settings not in either browsewr.

Chrome Solutions

Chrome Solution 1 - adulterate the hosts file, placing .localhost at the end of each host name. At: /etc/hosts:

127.0.0.1 myservername.localhost Google Chrome Ignoring Hosts File

Chrome Solution 2 - (an answer to this question, not repeated here)

Solutions for FireFox Configuration, in Descending Order of Likelihood

FireFox Solution 1: Disable DNS over HTTPS (DoH), by unchecking the feature in settings. To find the checkbox, open FireFox settings or preferences, whichever you have. Scroll to the bottom of the first panel to find and click on Network Settings. Near the bottom there is a checkbox marked, "Enable DNS over HTTPS". Unchecking that might solve the problem.
Why is my hosts file entry being ignored by the browser?

FireFox Solution 1a - If you want to use DoH AND your hosts file then you need to use DoH as your OS's DNS so it is after the hosts file. https://www.reddit.com/r/firefox/comments/e64073/dns_over_https_doh_needs_hosts_file/

FireFox Solution 2 - set network.dns.offline-local to false. To do this, type about:config in the address bar. Click through the warning that comes up, then use the configuration search to find "network.dns.offline-local". Toggle it to false. https://superuser.com/questions/1433325/does-firefox-ignore-the-hosts-file-how-to-make-firefox-honor-the-hosts-file

FireFox Solution 3 - If you are using ip6, make hosts file has ip6 entries.

FireFox Solution 4 - Set both www and non-www domain names in the hosts file. 127.0.0.1 foo.com 127.0.0.1 www.foo.com

FireFox Solution 5 - Disable dns cache in FireFox. To do this, type about:config in the address bar. Click through the warning that comes up, then use the configuration search to find "network.dnsCacheEntries". Change its value to 0.

FireFox Solution 6 - Involve yourself in nsswitch.conf. It is involved in how /etc/hosts is used. May the Force be with you. That is all I have for this.

FireFox Solution 7 - Try more configuration solutions:

browser.fixup.dns_first_for_single_words to True
network.dns.disablePrefetch to True
network.dns.offline-localhost to False
network.dnsCacheEntries to 0 (ZERO)
network.dnsCacheExpiration to 0 (ZERO)
network.dnsCacheExpirationGracePeriod to 0 (ZERO)

credit: Jay Rizzo

FireFox Solution 8 - Try this solution:

Navigate to 3 bars > Preferences > General > Scroll to the end > Network Proxy > settings

Configure Proxies to Access the Internet: *CHANGE* Auto-detect proxy settings for this network

    I was using Use System Proxy Settings
        I also found that is you change tracking in preferences & config this hides the box's too in Firefox
Navigate to 3 bars > Preferences > Privacy & Security > Tracking Protection >Always& "Do Not Track" >Always
AND YOU ALSO have to set this in the browserabout:config`
privacy.trackingprotection.enabled To True

credit: Jay Rizzo

FireFox Solution 9 - Blame your proxy, if you are using one. Then tinker with it to see if you can fix it.

FireFox Solution 10 - Use an addon such as SwitchHost, hoping the new problems introduced are less than those resolved, bearing in mind the problems might not materialize for a year or more.

FireFox Solution 11 - Involve yourself in fireFox's Trusted Recursive Resolver (trr). It is involved in how /etc/hosts is used. May the Force be with you. That is all I have for this.

FireFox Solution 12 - Stop using CloudFlare for dns, https://www.reddit.com/r/firefox/comments/f32enr/firefox_ignores_etchosts_file_on_macbook/

FireFox Solution 13 - If your version of FireFox is from 2008, upgrade to a more recent version http://forums.mozillazine.org/viewtopic.php?p=3274240

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 Nico Rodsevich
Solution 4 Ünsal Korkmaz
Solution 5