localhost:5173 / vite-wsl2
Reaching localhost:5173 from Windows when Vite runs in WSL2
WSL2 forwards localhost to Windows for you, so a Windows browser normally opens localhost:5173 without --host — but reaching the same server from another machine on your LAN is a Windows-side networking change, not a Vite setting.
Windows can already reach it, the LAN cannot
This is the part that confuses people who come from Docker. In WSL2 the Windows side is not "another machine" — WSL forwards localhost into the distribution, so Edge or Chrome on Windows opens localhost:5173 against a dev server that is still bound to loopback inside Linux. Nothing else on your network can, because the distribution sits behind a virtualised ethernet adapter with its own IP address. Vite states this plainly in the server.host option: on WSL2, host: true is not sufficient to reach the server from your LAN.
- Windows browser → localhost:5173 — works with the default config.
- Phone or second laptop → your Windows IP on 5173 — does not, until the Windows side forwards it.
Which networking mode are you in
WSL uses a NAT architecture by default, and the answer to every LAN question below depends on whether you have left it there. Two commands tell you: the WSL version, and whether the distribution has an address of its own or one of the Windows addresses.
PS> wsl --version WSL version: 2.6.1.0 # mirrored mode needs 2.0.0+ on Win11 22H2+ $ wsl hostname -I 172.24.118.43 # own subnet -> NAT mode # a Windows LAN address -> mirrored mode
NAT mode: forward the port on the Windows side
Microsoft documents the workaround as the one you would use for any virtual machine: a portproxy on Windows that listens on your LAN address and connects to the WSL2 VM. The connect address is the output of wsl hostname -I — with a capital I. The lowercase variant returns 127.0.1.1, a placeholder that will forward to nothing.
PS> netsh interface portproxy add v4tov4 \ listenport=5173 listenaddress=0.0.0.0 \ connectport=5173 connectaddress=(wsl hostname -I) The WSL2 VM address changes on restart — this entry goes stale.
Mirrored mode: the LAN reaches WSL directly
On Windows 11 22H2 and higher you can switch networking mode instead of maintaining a port proxy. Mirrored mode mirrors the Windows network interfaces into Linux; Microsoft lists "connect to WSL directly from your local area network" as one of its benefits, along with IPv6 support and reaching Windows servers from Linux over 127.0.0.1. It is a per-machine setting in .wslconfig, not a per-project one.
[wsl2] networkingMode=mirrored
Mirrored mode still has a firewall in front of it
Since WSL 2.0.9 on Windows 11 22H2 and higher the Hyper-V firewall is on by default, and it drops inbound connections to the distribution before Vite ever sees them. Microsoft gives two ways to open it: allow inbound traffic for the WSL VM creator, or add a single rule for the port you actually want.
# one port only — the narrower of the two
New-NetFirewallHyperVRule -Name "ViteDev" -DisplayName "Vite dev server" \
-Direction Inbound -Protocol TCP -LocalPorts 5173 \
-VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}'Once traffic arrives, --host matters again
Both routes above deliver connections that look like LAN connections to the dev server. A server still bound to loopback inside the distribution refuses them, so the Windows-side work only pays off together with the bind. Set it for the run you need it in rather than in the committed config — the dev server serves your source and applies no authentication.
$ npm run dev -- --hostThe page loads but never reloads
This is the second WSL2 problem and it has nothing to do with networking. Vite documents that on WSL2 file system watching does not work when a file is edited by a Windows application — a non-WSL2 process — because of a WSL2 limitation, and that the same applies to Docker running on a WSL2 backend. If you keep the project under /mnt/c and edit it from a Windows editor, the dev server never hears about the change.
- Recommended by Vite: edit with WSL2 applications, and move the project folder off the Windows filesystem — reaching it from WSL2 is slow on top of being unwatchable.
- Fallback: server.watch.usePolling, which Vite flags for high CPU utilisation.
Polling is the fallback, not the setting
Enable it where events genuinely do not arrive, and leave it off everywhere else. On a project of any size it keeps a core busy for as long as the server runs.
export default {
server: {
watch: { usePolling: true },
},
}If you reach it through VS Code instead
VS Code forwards the port itself rather than using either route above, and Vite documents a separate fix for it: with a Dev Container or the port forwarding feature in VS Code, set server.host to 127.0.0.1, because that forwarding does not support IPv6.
server: { host: '127.0.0.1' }# faq
Questions
Why does localhost:5173 work from Windows without --host?
WSL forwards localhost from Windows into the distribution, so the Windows browser reaches a dev server that is still bound to loopback inside Linux. Other machines on your network have no such forwarding.
Is host: true enough to reach the WSL2 dev server from my LAN?
No. Vite states in the server.host option that on WSL2 it is not sufficient. You also need a Windows-side port proxy, or mirrored networking mode.
Why does hot reload miss my edits on WSL2?
File system watching does not fire when the file is edited by a Windows application. Edit from WSL2, keep the project off the Windows filesystem, or fall back to server.watch.usePolling.
Do I need the portproxy command in mirrored mode?
No. Mirrored mode makes the distribution reachable from the LAN directly, but the Hyper-V firewall still has to allow the inbound connection on 5173.
Why does my portproxy entry stop working after a reboot?
In NAT mode the WSL2 VM gets a new address, and the entry still points at the old one. Re-add it with the current output of wsl hostname -I.
# next
Related
# sources
- Vite — server.host (WSL2 note)
- Vite — server.watch (WSL2 file watching)
- Vite — Troubleshooting: Dev Containers / VS Code port forwarding
- Microsoft — Accessing network applications with WSL
Checked against the Vite documentation on 2026-09-12.