DEV Community

Locahl
Locahl

Posted on

DNS not updating after hosts file change? Fix it in order

You edited the hosts file. The line looks correct. The browser still opens the old site.

Do not rewrite the hosts file again yet. Walk the stack in order.

1. Confirm the file that actually loaded

macOS / Linux:

grep -n "example.com" /etc/hosts
Enter fullscreen mode Exit fullscreen mode

Windows (Admin PowerShell):

Select-String -Path C:\Windows\System32\drivers\etc\hosts -Pattern "example.com"
Enter fullscreen mode Exit fullscreen mode

Check for:

  • a # commenting the line out
  • a second line for the same hostname later in the file
  • a typo in the hostname

If the terminal cannot see the line, the browser will not either.

2. Flush the OS DNS cache

macOS:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
Enter fullscreen mode Exit fullscreen mode

Windows (Admin):

ipconfig /flushdns
Enter fullscreen mode Exit fullscreen mode

Linux with systemd-resolved:

sudo resolvectl flush-caches
Enter fullscreen mode Exit fullscreen mode

3. Ask the OS, not the browser

# macOS / Linux
ping -c 1 example.com
getent hosts example.com
Enter fullscreen mode Exit fullscreen mode

Windows:

ping example.com
nslookup example.com
Enter fullscreen mode Exit fullscreen mode

If the terminal shows the wrong IP, stop. Fix hosts or flush again. Do not debug the app yet.

On some systems nslookup talks to a DNS server and can ignore hosts. Prefer ping / getent for hosts checks.

4. Clear browser DNS

Browsers cache names too.

  1. Quit the browser fully (not just the tab)
  2. Reopen and retry
  3. Chrome: chrome://net-internals/#dnsClear host cache
  4. Try a private window once, to rule out extensions

5. Watch for HTTPS and HSTS traps

Hosts can point www.client.com at staging, while the browser still forces HTTPS and an old certificate story.

Symptoms:

  • certificate warnings
  • redirects back to production
  • cookies scoped to the real domain behaving oddly

For local-only work, prefer .test names. For cutover tests, expect TLS quirks and test with curl too:

curl -vI https://www.client.com
Enter fullscreen mode Exit fullscreen mode

6. Docker and VPN layers

If the browser is on the host but the service is in Docker, your laptop hosts file may not be what the container uses.

If a VPN or corporate DNS is active, flush again after connect/disconnect. Some agents re-inject resolvers.

Short checklist

  1. Line present and uncommented in the real hosts file
  2. One active mapping per hostname
  3. OS DNS flushed
  4. Terminal resolves the expected IP
  5. Browser DNS cleared
  6. Only then debug the app, proxy, or TLS

Most "hosts is broken" reports die at step 3 or 4.


If you switch hosts a lot, a small desktop manager helps keep profiles and DNS flush in one place: Locahl.

Top comments (0)