Sleezr
Buy Sleezr
localhosttroubleshootingERR_CONNECTION_REFUSEDportshosts file

Localhost Refused to Connect: How to Fix It

Fix "localhost refused to connect" (ERR_CONNECTION_REFUSED) in Chrome, on Windows, Mac, XAMPP and VS Code: check the server, the port, IPv6 vs IPv4, hosts file and firewall.

S

Sleezr Team

·4 min read
Localhost Refused to Connect: How to Fix It

"Localhost refused to connect" (ERR_CONNECTION_REFUSED) almost always means nothing is listening where the browser is connecting. The usual causes: the local server is not running, it is on a different port, it bound to IPv6 (::1) while you reached IPv4 (127.0.0.1) or vice versa, the localhost hosts entry is missing, or a firewall blocked the port. Work through the checklist below in order and you will find it fast.

Fix "localhost refused to connect", checklist

1
Confirm the server is actually running. Look for a "listening on port…" line in its terminal. No server = guaranteed refusal. Restart it.
2
Check the port. The URL port must match the server (localhost:3000, not :8080). A wrong port is the single most common cause.
3
Try 127.0.0.1 directly. If http://127.0.0.1:PORT works but localhost does not, it is an IPv6/IPv4 mismatch, see below. Related: 127.0.0.1 vs localhost.
4
Restore the hosts entry. Ensure 127.0.0.1 localhost and ::1 localhost exist; a deleted line breaks resolution entirely.
5
Allow the port in the firewall / antivirus. Security tools sometimes block local ports.
6
Clear the browser DNS cache and reload in a fresh tab, chrome://net-internals/#dns.

This shares causes with the "this site can't be reached" on localhost error, same checklist, different wording shown by the browser.

Find out what is listening

Before blaming the browser, check the port from the command line:

BASH
# macOS / Linux
lsof -i :3000
# Windows (Command Prompt)
netstat -ano | findstr :3000
  • Nothing returned → the server is not listening on that port. Fix the app, not the browser.
  • A different PID than expected → another process grabbed the port; stop it or change your port.

The IPv6 vs IPv4 trap (127.0.0.1 works, localhost doesn't)

Modern systems resolve localhost to IPv6 ::1 first. If your dev server listens only on IPv4 127.0.0.1, the IPv6 connection is refused and the browser reports "localhost refused to connect", even though http://127.0.0.1:PORT works.

Two fixes:

  • Bind the server to all interfaces: 0.0.0.0 (IPv4) or :: (dual stack). See 0.0.0.0 vs 127.0.0.1.
  • Or just use http://127.0.0.1:PORT in the browser.

When the hosts file is the culprit

A removed or commented localhost line breaks resolution system-wide. Restore both entries:

TEXT
127.0.0.1 localhost
::1 localhost

On Mac, see localhost not working on Mac. To edit the file safely, see how to edit the hosts file.

Fixes by environment

Chrome (and other browsers)

The error text comes from the browser, but the cause is the server/port/DNS, not Chrome itself. Still, clear Chrome's host cache (chrome://net-internals/#dns) and test in Incognito to rule out an extension or stale cache.

Windows 10 / 11

  • Run the dev server in an elevated terminal if it needs a privileged port (< 1024).
  • Check Windows Defender Firewall is not blocking the port.
  • Confirm no proxy is set: Settings → Network & internet → Proxy.

XAMPP / MAMP / WAMP

  • Start Apache (and MySQL) in the control panel, a stopped service refuses every connection.
  • Port 80/443 is often taken by Skype, IIS, or VPN software. Change Apache's port (e.g. to 8080) or stop the conflicting app.

VS Code / Node / Vite dev servers

  • Read the actual URL the server prints, frameworks pick a free port (3000, 5173, 8000…) and it may differ from what you typed.
  • If you run inside WSL2 or a container, the port must be forwarded to the host.

Docker

  • The container port must be published to the host: -p 3000:3000. Without it, localhost:3000 on the host is refused.

Verify the fix

BASH
curl -I http://127.0.0.1:3000
curl -I http://localhost:3000

If the IP works but the name fails, return to the IPv6/hosts steps. If both fail, the server is not listening, fix the process first.

Avoid future breakage

Accidental edits to the localhost line are a frequent, hard-to-spot cause. A hosts manager like Sleezr keeps the core localhost entries intact, lets you toggle custom domains safely, and flushes DNS automatically, so localhost keeps answering.

_Last tested: June 2026 on Windows 11, macOS 26 and Ubuntu 24.04._

Sources and further reading

Also readFix "this site can't be reached" on localhost
Also read127.0.0.1 vs localhost explained
Share this article

Frequently Asked Questions

Why does localhost refuse to connect?

In almost all cases nothing is listening where the browser connects: the server is not running, it is on a different port, it bound to IPv6 only (or IPv4 only), the localhost hosts entry is missing, or a firewall blocked the port.

How do I fix ERR_CONNECTION_REFUSED on localhost?

Confirm the server is running and on the expected port, try http://127.0.0.1:PORT instead of localhost, restore the 127.0.0.1 localhost hosts line, allow the port in the firewall, and clear the browser DNS cache.

Why does 127.0.0.1 work but localhost refuses to connect?

localhost often resolves to IPv6 ::1 first. If your server listens only on IPv4 127.0.0.1, the IPv6 attempt is refused. Bind the server to all interfaces (0.0.0.0 / ::) or use 127.0.0.1 in the URL.

Why does localhost refuse to connect in XAMPP or MAMP?

The Apache/MySQL service is usually stopped or another app holds port 80/443 (often Skype, IIS or VPN software). Start the service in the control panel and change the port if it is taken.

Can the hosts file cause "localhost refused to connect"?

Yes. If the 127.0.0.1 localhost line was deleted or commented out, localhost will not resolve and the browser cannot connect. Restore 127.0.0.1 localhost and ::1 localhost.

Related Articles

4 min read
localhostsubdomainslocal development

How to Use Subdomains on localhost for Local Development

Use subdomains on localhost: the free *.localhost trick browsers resolve automatically, fixed subdomains via the hosts file, and wildcard subdomains with dnsmasq on Mac and Linux.

S

Sleezr Team

Developer tools team

4 min read
hosts filetroubleshootingDNS

/etc/hosts Not Working? Fixes for Windows, Mac & Linux

The hosts file is ignored or /etc/hosts changes do not take effect? Fix it on Windows, Mac and Linux: flush DNS, check syntax, IPv6, line endings, resolver caches and save permissions.

S

Sleezr Team

Developer tools team

3 min read
DNSChrometroubleshooting

Fix DNS_PROBE_FINISHED_NXDOMAIN (2026)

Fix DNS_PROBE_FINISHED_NXDOMAIN in Chrome and Edge: flush DNS, check the hosts file, reset DNS servers and clear the browser cache. Step-by-step solutions.

S

Sleezr Team

Developer tools team

2 min read
localhost127.0.0.1DNS

What Is localhost? A Clear Explanation

What is localhost? It is the hostname for your own computer, resolving to 127.0.0.1 (or ::1) via the hosts file. How it works, why it matters, and common issues.

S

Sleezr Team

Developer tools team

3 min read
WordPressredirectmigration

Fix WordPress Redirecting to the Live Site

WordPress redirects to the live site when testing on a new server because the URL is hard-coded in the database. Fix it with the hosts file, no DB edits needed.

S

Sleezr Team

Developer tools team