
How to Sync WSL2 with Windows Hosts File for Local Domains (2026)
Fix WSL2 localhost resolution and map custom .test domains to your WSL2 Linux IP automatically without breaking Windows networking.
Manage hosts files without the terminal
Sleezr helps you manage environments visually on Windows, macOS, and Linux, with automatic DNS flush and backups.
One-time payment
Direct answer: To map custom local domains (app.test, api.test) to servers running inside WSL2 on Windows 11, add your entries to the Windows hosts file (C:\Windows\System32\drivers\etc\hosts) pointing to 127.0.0.1, and ensure localhost forwarding is enabled in your .wslconfig.
# Inside C:\Windows\System32\drivers\etc\hosts
127.0.0.1 app.test
127.0.0.1 api.testBecause WSL2 runs inside a lightweight virtual machine with a dynamic NAT IP, understanding how Windows bridges loopback traffic is key to a frictionless development workflow.
The WSL2 Networking Challenge: NAT vs Loopback
Unlike WSL1 (which shared the exact network stack of Windows), WSL2 runs as a native Linux kernel in a managed Hyper-V utility VM.
| Feature | WSL1 | WSL2 (Default NAT) | WSL2 (Mirrored Mode) |
|---|---|---|---|
| Architecture | Emulated Linux API | Real Linux VM (Hyper-V) | Real Linux VM (Shared NIC) |
| IP Address | Shared with Windows Host | Dynamic private IP (172.x.x.x) | Shared with Windows Host |
| Windows hosts file edit | Points to 127.0.0.1 | Points to 127.0.0.1 (via forwarder) | Points to 127.0.0.1 |
| IPv6 support | Limited | Requires custom setup | Native |
| Requires Admin to edit | Yes | Yes | Yes |
Step 1: Configure .wslconfig for Localhost Forwarding
Windows includes a built-in proxy service that automatically forwards traffic from 127.0.0.1 on Windows to the active ports in WSL2.
Open or create your global WSL configuration file in Windows:
%USERPROFILE%\.wslconfig
(Example: C:\Users\YourUsername\.wslconfig)Add the following configuration:
[wsl2]
localhostForwarding=true
# Optional for Windows 11 23H2 / 24H2 and newer:
# networkingMode=mirrored
# dnsTunneling=trueRestart WSL to apply the configuration:
# In Windows PowerShell (Admin)
wsl --shutdownStep 2: Make Sure Your Dev Server Listens on 0.0.0.0
A frequent stumbling block: modern build tools (such as Vite, Next.js, Express or Django) often bind exclusively to 127.0.0.1 inside Linux. Because Windows connects through the virtual network interface, the server must listen on all interfaces (0.0.0.0).
Vite (`vite.config.ts`)
export default defineConfig({
server: {
host: '0.0.0.0', // or pass --host in package.json
port: 3000,
},
});Next.js
# In package.json
"scripts": {
"dev": "next dev -H 0.0.0.0 -p 3000"
}Step 3: Add Custom Local Domains to Windows Hosts
Open your Windows hosts file with elevated administrator privileges:
C:\Windows\System32\drivers\etc\hostsAppend your project domains:
# WSL2 Local Development Projects
127.0.0.1 myapp.test
127.0.0.1 api.myapp.test
127.0.0.1 admin.myapp.test*(We recommend using the reserved .test TLD to avoid collisions with public top-level domains. See why .test is the safest local TLD.)*
Step 4: Flush DNS Cache
After saving the hosts file, flush the Windows DNS resolver cache:
# Windows PowerShell / Windows Terminal as Administrator
ipconfig /flushdns
Clear-DnsClientCacheNow open http://myapp.test:3000 in Chrome or Edge on Windows. Your requests will route directly to your dev server running inside WSL2.
How WSL2 Generates /etc/hosts Automatically
By default, WSL2 generates its internal /etc/hosts on startup by copying entries from the Windows hosts file.
This behavior is controlled inside WSL2 by /etc/wsl.conf:
# /etc/wsl.conf inside your WSL2 Linux distro
[network]
generateHosts = true
generateResolvConf = trueWhen generateHosts = true, every entry you add to your Windows hosts file is mirrored inside your Linux terminal. This means curl http://myapp.test:3000 works seamlessly both inside Windows and inside Linux.
Advanced: Modern Mirrored Networking Mode (Windows 11 24H2)
If you are running Windows 11 (build 22621+ or 24H2), Microsoft introduced Mirrored Networking Mode. This mode mirrors Windows network adapters directly into Linux, eliminating the NAT bridge completely.
In %USERPROFILE%\.wslconfig:
[wsl2]
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=trueBenefits of Mirrored Mode:
- Zero NAT translation delays.
- Linux processes bind directly to
127.0.0.1on Windows. - Full VPN compatibility (Cisco AnyConnect, GlobalProtect).
Troubleshooting Common WSL2 Hosts Issues
1. Connection Refused on http://myapp.test
- Verify that your dev server is running and bound to
0.0.0.0. - Check if another Windows process is already listening on the same port using
netstat -ano | findstr :3000in PowerShell. - Read our guide on troubleshooting connection refused errors.
2. Browser Still Shows Old IP or Live Site
- Clear Chrome internal DNS cache at
chrome://net-internals/#dns. - Flush Windows DNS with
ipconfig /flushdns. - Check why hosts edits disappear or fail to take effect.
3. Docker Containers Inside WSL2
- When running Docker Engine directly in WSL2 (or via Docker Desktop WSL2 backend), use
--add-host myapp.test:host-gatewayorextra_hostsindocker-compose.ymlto route traffic between containers and host services. - Related: managing hosts entries with Docker.
Automating WSL2 and Windows Hosts with Sleezr
Juggling multiple client projects across WSL2, Docker, and native Windows tools can quickly make your hosts file messy.
Sleezr provides:
- Named environments (e.g., Client A, Staging, Microservices) that toggle with one click.
- Automatic DNS flush on every environment switch.
- Automatic hosts backup before every write operation.
- Full cross-platform support across Windows, macOS, and Linux.
Explore the complete Windows 11 hosts file guide or compare Sleezr vs PowerToys Hosts.
_Last tested: August 2026 on Windows 11 24H2 with WSL2 Ubuntu 24.04 LTS._
Sources and further reading
Frequently Asked Questions
WSL2 runs inside a lightweight Hyper-V virtual machine connected via an internal virtual NAT bridge. Each time Windows restarts or WSL shuts down, Hyper-V assigns a new private IP address dynamically.
Yes. When localhostForwarding=true is set in your .wslconfig file, Windows automatically forwards connections to 127.0.0.1 on the host to the active WSL2 Linux instance for mapped listening ports.
If you access your local app from a Windows browser (Chrome, Edge), you must edit the Windows hosts file at C:\Windows\System32\drivers\etc\hosts. WSL2 automatically generates its own /etc/hosts from Windows hosts unless generateHosts=false is configured in /etc/wsl.conf.
Mirrored networking (networkingMode=mirrored in .wslconfig) shares the network interfaces between Windows and Linux seamlessly. It allows full IPv6 support, direct localhost binding, and removes NAT translation hurdles.
Many modern dev servers (Vite, Next.js, Webpack) bind by default to 127.0.0.1 inside Linux only. Configure your server to bind to 0.0.0.0 or pass --host to accept forwarded connections from the Windows host.
Related Articles
ERR_CONNECTION_REFUSED on Hosts File Domains: 6 Fixes (2026)Troubleshooting
ERR_CONNECTION_REFUSED on Hosts File Domains: 6 Fixes (2026)
Custom domain in your hosts file returning ERR_CONNECTION_REFUSED? Fix port mismatches, 0.0.0.0 vs 127.0.0.1 binding, Docker mappings and reverse proxies.
7 min read
How to Find & Edit Hosts File on Windows 11 (2026 Guide + Fix Access Denied)Editing the hosts file
How to Find & Edit Hosts File on Windows 11 (2026 Guide + Fix Access Denied)
Step-by-step tutorial to open and edit the hosts file on Windows 11 and 24H2 with Notepad or PowerShell, fix Access Denied errors, and flush DNS.
8 min read
Using Hosts Files for Docker Development on MacLocal development
Using Hosts Files for Docker Development on Mac
Configure hosts files for Docker, docker-compose and container networking. Map services to local domains and simplify Mac development.
9 min read
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)Troubleshooting
Fix getaddrinfo ENOTFOUND in Docker and Node.js with Hosts File (2026)
Docker container or Node.js app throwing getaddrinfo ENOTFOUND or EAI_AGAIN despite /etc/hosts? Fix Docker network isolation and IPv4 DNS precedence.
3 min read
Flush DNS on macOS Sequoia (15): Exact Command (2026)Flushing DNS & cache
Flush DNS on macOS Sequoia (15): Exact Command (2026)
Flush DNS cache on macOS 15 Sequoia with sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder β verify with dscacheutil, clear browser cache, fix hosts edits.
2 min read
Flush DNS on macOS Sonoma (14): Exact Command (2026)Flushing DNS & cache
Flush DNS on macOS Sonoma (14): Exact Command (2026)
Flush DNS cache on macOS 14 Sonoma with sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder β verify resolution, clear browser DNS, fix stale hosts edits.
2 min read