Dev.to WebDev πŸ›  Dev πŸ‘ 1 πŸ“– 2 min read

How to flush DNS on Mac, Windows, and Linux

How to flush DNS on Mac, Windows, and Linux You changed your hosts file. The line looks right. The browser still loads the old IP. That is usually DNS cache, not a bad hosts entry. Flush the OS cache first. Check wit

How to flush DNS on Mac, Windows, and Linux

You changed your hosts file. The line looks right. The browser still loads the old IP.

That is usually DNS cache, not a bad hosts entry.

Flush the OS cache first. Check with a terminal command. Only then blame the browser.

macOS

Works on recent macOS (Sequoia, Sonoma, Ventura, and neighbors):

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

Enter your password. No output is normal.

Still wrong after that?

  1. Quit the browser fully and reopen it
  2. In Chrome, open chrome://net-internals/#dns and click Clear host cache
  3. Re-check the hosts line (typo, or a # that comments it out)

Windows 10 and 11

Open Command Prompt or PowerShell as Administrator:

ipconfig /flushdns

Or in PowerShell:

Clear-DnsClientCache

Hosts file location if you need it:

C:\Windows\System32\drivers\etc\hosts

Linux

It depends which resolver is caching.

systemd-resolved (common on Ubuntu and Fedora)

sudo resolvectl flush-caches

On older setups:

sudo systemd-resolve --flush-caches

nscd

sudo systemctl restart nscd

dnsmasq

sudo systemctl restart dnsmasq

Hosts file:

/etc/hosts

Check the result outside the browser

Do this before you keep refreshing Chrome.

macOS

dscacheutil -q host -a name myapp.test
ping -c 1 myapp.test

Linux

getent hosts myapp.test
ping -c 1 myapp.test

Windows

ping myapp.test
nslookup myapp.test

If the terminal shows the right IP and the browser does not, the OS hosts file is probably fine. Clear browser DNS, or check HTTPS / HSTS / Secure DNS (DoH).

If you flushed DNS and it still fails

Browser wrong, terminal correct

Browser cache, Secure DNS, or a certificate / redirect issue.

Terminal wrong too

Typo in hosts, wrong file, line commented with #, or you edited a copy that never got applied.

Works, then breaks after reboot

Something rewrites hosts, or a VPN / DNS tool takes over.

Only one browser fails

That browser has its own DNS settings.

Works on normal Wi-Fi, fails on VPN

VPN DNS is ignoring or overriding local hosts.

Also check AdGuard, NextDNS, Cloudflare WARP, Pi-hole, and Tailscale MagicDNS. They can make a correct hosts change look like it did nothing.

Hosts syntax worth double-checking

127.0.0.1   myapp.test
127.0.0.1   api.myapp.test
# 10.0.0.5  staging.myapp.test
  • IP first, then hostname(s)
  • A # disables the rest of the line
  • Keep the line clean (no random trailing junk)
  • IPv6 localhost is ::1 when you need it

Copy-paste

# macOS
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

# Windows (Admin)
ipconfig /flushdns

# Linux (systemd-resolved)
sudo resolvectl flush-caches

Edit hosts, flush DNS, verify in the terminal. That order fixes most of these tickets.

πŸ“° Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β€” full credit and traffic to the original publisher.