Generated with sparks and insights from 53 sources

img10

img11

img12

img13

img14

img15

Introduction

  • Yes, it is possible to access services on the host machine using localhost in WSL2.

  • There are two primary methods to achieve this: using mDNS with the hostname.local format or enabling Mirrored Network Mode.

  • In mDNS mode, you can access the Windows host by appending .local to the hostname.

  • In Mirrored Network Mode, localhost should work directly, but this requires Windows 11 23H2 or later.

  • Firewall rules may need to be adjusted to allow traffic from WSL2 to the Windows host.

mDNS Method [1]

  • Description: mDNS allows WSL2 to access the Windows host using the hostname.local format.

  • Command: Use ping $(hostname).local to verify connectivity.

  • Example: If your hostname is MyComputer, use MyComputer.local.

  • Firewall: Ensure necessary firewall ports are open for mDNS to work.

  • Limitations: mDNS may not work if /etc/resolv.conf is modified.

img10

img11

img12

img13

img14

img15

Mirrored Network Mode [1]

  • Description: Mirrored Network Mode allows localhost to work directly in WSL2.

  • Requirement: Requires Windows 11 23H2 or later.

  • Configuration: Add networkingMode=mirrored to .wslconfig.

  • Steps: Exit WSL, run wsl --shutdown, and restart WSL.

  • Note: mDNS will not work in Mirrored Network Mode.

img10

img11

img12

img13

img14

img15

Firewall Configuration [2]

  • Description: Adjusting firewall settings is crucial for connectivity.

  • Command: Use New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow in PowerShell.

  • Note: Ensure Windows services accept connections from remote hosts.

  • Tip: Check for automated firewall rules that block access from public networks.

  • Example: Modify or delete inbound rules that block WSL connections.

img10

img11

img12

img13

img14

img15

Common Issues [3]

  • Issue: Connection refused errors when accessing services.

  • Solution: Ensure the service is bound to 0.0.0.0 instead of localhost.

  • Issue: mDNS not working.

  • Solution: Check /etc/resolv.conf and firewall settings.

  • Issue: Inconsistent network configurations.

  • Solution: Use PowerShell to diagnose and fix network adapter issues.

img10

img11

img12

img13

img14

img15

Additional Tips [4]

  • Tip: Use ip route show | grep -i default | awk '{print $3}' to find the host IP.

  • Tip: Add the host IP to /etc/hosts for easier access.

  • Tip: Use nc -zv $(hostname).local <portnumber> to test port connectivity.

  • Tip: Consider downgrading to WSL1 if persistent issues occur.

  • Tip: Regularly update WSL and Windows for the latest features and fixes.

img10

img11

img12

img13

img14

img15

Related Videos

<br><br>

<div class="-md-ext-youtube-widget"> { "title": "How to Access Services Running Inside WSL 2 from Other ...", "link": "https://www.youtube.com/watch?v=DNp2WMIaOwE", "channel": { "name": ""}, "published_date": "Jun 26, 2023", "length": "" }</div>

<div class="-md-ext-youtube-widget"> { "title": "how to access windows localhost from wsl2?", "link": "https://www.youtube.com/watch?v=CQ-1Mfjtvf8", "channel": { "name": ""}, "published_date": "Dec 23, 2023", "length": "" }</div>

<div class="-md-ext-youtube-widget"> { "title": "How to Access a Web Server which is Running on WSL Instance", "link": "https://www.youtube.com/watch?v=-0XgLRAQ6eE", "channel": { "name": ""}, "published_date": "Jul 13, 2023", "length": "" }</div>