Connecting to TCP WSL2 on Windows: A Comprehensive Guide

Abstract: Resolve TCP connection failures from Windows to WSL2 by identifying the virtual adapter IP and configuring inbound firewall rules.

Created: 2026-10-25 by
To connect to TCP services running inside WSL2 from Windows 11, you must use the host IP address of the virtual network adapter rather than localhost. WSL2 operates in a NAT environment, meaning ports are not automatically forwarded. This guide explains how to find your VM's IP and configure firewall rules to enable seamless communication between your host OS and Linux subsystem.

Understanding the Network Architecture

WSL2 runs on a lightweight utility VM that uses Hyper-V or the Windows Hypervisor Platform. Unlike WSL1, which integrates directly with the Windows kernel, WSL2 has its own private network interface. When you bind a service to localhost or 127.0.0.1 inside WSL2, it only listens on the loopback interface within that virtual machine. Consequently, attempts to connect via localhost from Windows will fail because the traffic never reaches the WSL2 instance.

The solution involves identifying the dynamic IP address assigned to the WSL2 virtual network adapter on the Windows side. This IP changes every time WSL2 restarts unless specific configuration steps are taken. You need to discover this IP and then ensure your Windows Firewall allows inbound connections to the specific port your application is using.

Locating the WSL2 Virtual Adapter

Windows creates a virtual network adapter named "vEthernet (WSL)" when WSL2 is active. To find the IP address associated with this adapter, open PowerShell as an administrator and run the following command:

Get-NetIPAddress -InterfaceAlias "vEthernet (WSL)" | Select-Object IPAddress

This command queries the network interfaces installed in Windows and filters for the one specifically created for WSL2. The output will display the IPv4 address currently assigned to your Linux VM. Note this address down, as you will need it to establish connections from Windows applications such as browsers, database clients, or IDEs.

Configuring Windows Firewall Rules

Even if you have the correct IP address, Windows Defender Firewall may block incoming traffic on the port your service is listening on. By default, inbound connections are denied for most non-system services. You must create an explicit rule to allow this traffic.

Open PowerShell as Administrator and execute the following command, replacing <PORT> with the actual port number your application uses (e.g., 8080, 3000, or 5432):

New-NetFirewallRule -DisplayName "WSL2 Port Forwarding" -Direction Inbound -LocalPort <PORT> -Protocol TCP -Action Allow

This command creates a new inbound rule that permits TCP traffic on the specified port. It does not restrict the source IP, allowing any connection from the WSL2 VM to reach your service. If you want to restrict access only to the WSL2 VM, you can add the -RemoteAddress parameter with the IP address you found earlier. However, for most development scenarios, allowing the port broadly is sufficient and easier to manage.

Connecting from Windows Applications

Once the firewall rule is in place, you can connect to your WSL2 service using the IP address obtained in the previous step. For example, if your Python Flask app is running on port 5000 inside WSL2, and the WSL2 IP is 172.18.16.5, you would navigate to http://172.18.16.5:5000 in your Windows browser.

If you encounter connectivity issues, verify that the service inside WSL2 is binding to 0.0.0.0 instead of 127.0.0.1. Binding to 0.0.0.0 ensures the service listens on all available network interfaces within the VM, including the one connected to the Windows host. Many frameworks default to 127.0.0.1 for security reasons, so explicitly setting the host to 0.0.0.0 is a common requirement.

Handling Dynamic IP Changes

A significant challenge with WSL2 networking is that the IP address changes whenever the Linux distribution restarts. This means your hardcoded IP addresses in configuration files or bookmarks may become invalid after a reboot. To mitigate this, you can create a simple script that retrieves the current IP and updates your hosts file or connection strings dynamically.

Alternatively, you can use port forwarding utilities like netsh to map a static port on the Windows host to the dynamic port in WSL2. However, this approach is more complex and often unnecessary if you simply use the current WSL2 IP. For developers who prefer stability, consider using WSL1 for simpler networking needs, though this sacrifices performance benefits. If you experience unresponsive behavior during these operations, refer to our guide on Troubleshooting Unresponsive WSL2 on Windows 10 for deeper diagnostic steps.

Verifying Connectivity

After configuring the firewall and ensuring your service binds correctly, test the connection from Windows using telnet or Test-NetConnection in PowerShell. Run the following command to check if the port is reachable:

Test-NetConnection -ComputerName <WSL2_IP> -Port <PORT>

If the TcpTestSucceeded field returns True, your connection is established successfully. If it returns False, double-check that the service inside WSL2 is actually running and listening on the expected port. You can verify this by running netstat -tuln inside WSL2 to see active listeners.

Frequently Asked Questions

Why does localhost work in WSL1 but not WSL2?

WSL1 translates Linux system calls directly to Windows NT kernel calls, sharing the same network stack as Windows. WSL2 runs a full Linux kernel in a VM, isolating its network stack. Therefore, localhost in WSL2 refers only to the VM's internal loopback, not the Windows host.

How do I make the WSL2 IP address static?

WSL2 does not natively support static IPs for the vEthernet adapter. The recommended approach is to use a startup script in WSL that detects the current IP and updates local configurations, or to rely on the firewall rule allowing all inbound traffic on the specific port regardless of the source IP.

Can I forward ports automatically on WSL2 start?

While there is no built-in auto-forwarding, you can create a PowerShell profile script that runs New-NetFirewallRule every time you open a terminal. Alternatively, use tools like wsl-port-forwarder which automate the process of mapping host ports to guest ports upon WSL initialization.

What if my Windows Firewall blocks the connection despite the rule?

Ensure that the firewall rule is enabled and set to apply to the correct network profiles (Domain, Private, Public). Sometimes, third-party antivirus software or corporate firewalls override Windows Defender settings. Check your security suite logs to see if the connection attempt was blocked at a different layer.

Is it safe to open all ports for WSL2?

Opening specific ports for known services is generally safe. Avoid creating overly broad rules that expose unused ports. Only allow traffic on ports where your application explicitly listens. Regularly audit these rules to ensure they are still necessary, especially if you change the services you run in WSL2.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More