<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Blog | Pedro Britto]]></title><description><![CDATA[My thoughts on technology, career and life.]]></description><link>https://blog.pedrobritto.com</link><generator>RSS for Node</generator><lastBuildDate>Tue, 12 May 2026 01:47:11 GMT</lastBuildDate><atom:link href="https://blog.pedrobritto.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Accessing your WSL2 instance from your local network]]></title><description><![CDATA[To access an application running in a WSL2 instance from a device in a local network, you'll need to route incoming traffic to your WSL2 instance.
You can do it two ways:
The Ngrok way
This is the easiest way. Just:

Download and install ngrok.

Run ...]]></description><link>https://blog.pedrobritto.com/accessing-your-wsl2-instance-from-your-local-network</link><guid isPermaLink="true">https://blog.pedrobritto.com/accessing-your-wsl2-instance-from-your-local-network</guid><category><![CDATA[General Programming]]></category><category><![CDATA[wsl2]]></category><category><![CDATA[network]]></category><dc:creator><![CDATA[Pedro Britto]]></dc:creator><pubDate>Tue, 28 Nov 2023 14:01:14 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/AyPStDvdQS0/upload/3ad788a313e56230cf1e42d9f0566376.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>To access an application running in a WSL2 instance from a device in a local network, you'll need to route incoming traffic to your WSL2 instance.</p>
<p>You can do it two ways:</p>
<h2 id="heading-the-ngrok-way">The Ngrok way</h2>
<p>This is the easiest way. Just:</p>
<ol>
<li><p>Download and install ngrok.</p>
</li>
<li><p>Run <code>ngrok http http://localhost:PORT</code>, where <code>PORT</code> is the port you want to expose.</p>
</li>
<li><p>You'll get an output that looks like the following:</p>
</li>
</ol>
<pre><code class="lang-plaintext">ngrok                                                                                                   (Ctrl+C to quit)                                                                                                                        Introducing Pay-as-you-go pricing: https://ngrok.com/r/payg

Session Status                online
Account                       ACCOUNT NAME
Version                       3.4.0
Region                        South America (sa)
Latency                       -
Web Interface                 http://127.0.0.1:4040
Forwarding                    https://81d0-123-45-6-100.ngrok-free.app -&gt; http://localhost:3000

Connections                   ttl     opn     rt1     rt5     p50     p90
                              0       0       0.00    0.00    0.00    0.00
</code></pre>
<ol>
<li>Get the first address under <code>Forwarding</code> (<code>https://81d0-123-45-6-100.ngrok-free.app</code>) and use it to access your application from anywhere on the planet that has internet access.</li>
</ol>
<p>That should get you covered. For more details, <a target="_blank" href="https://ngrok.com/docs/getting-started/">check out Ngrok docs</a>.</p>
<h2 id="heading-the-other-way">The other way</h2>
<p>I didn't come up with this, obviously. I got this fine information from <a target="_blank" href="https://www.youtube.com/watch?v=yCK3easuYm4">this video from David Bombal.</a></p>
<p>Here's how the other way works:</p>
<h3 id="heading-1-grab-the-ip-address-of-the-host-machine-in-your-local-network">1. Grab the IP address of the host machine in your local network.</h3>
<p>To do so, open PowerShell on the host machine and run <code>ipconfig</code>.</p>
<p>You'll get a bunch of text, but we are looking for this piece of information:</p>
<pre><code class="lang-plaintext">Wireless LAN adapter Wi-Fi:
   Connection-specific DNS Suffix  . :
   IPv4 Address. . . . . . . . . . . : 192.168.0.2
   Subnet Mask . . . . . . . . . . . : 255.255.255.0
   Default Gateway . . . . . . . . . : 192.168.0.1
</code></pre>
<div data-node-type="callout">
<div data-node-type="callout-emoji">ℹ</div>
<div data-node-type="callout-text">Note: if you are using an ethernet cable to connect to the local network, you might see different results.</div>
</div>

<p>The <code>IPv4 Address</code> value, which in this case is <code>192.168.0.2</code>, is what you need. Then...</p>
<h3 id="heading-2-get-the-ip-address-of-your-wsl2-instance-running-on-your-host-machine">2. Get the IP address of your WSL2 instance running on your host machine</h3>
<p>Instead of using PowerShell on the host machine, open the shell on your distro that's running in WSL2.</p>
<p>As I'm using Ubuntu, I ran <code>ip addr | grep eth0</code>. You'll get something like this:</p>
<pre><code class="lang-plaintext">2: eth0: &lt;BROADCAST,MULTICAST,UP,LOWER_UP&gt; mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.29.19.152/20 brd 172.29.31.255 scope global eth0
</code></pre>
<p>Again, the information that matters here is the IPv4, but the one with the <code>/20</code> mask, which is <code>172.29.19.152</code>. That's the IP address of your WSL2 instance under the host machine.</p>
<h3 id="heading-3-doing-the-thing">3. Doing the thing</h3>
<p>We found out that the IP of the host machine in the local network is <code>192.168.0.2</code>, the IP of the WSL2 instance in the host machine is <code>172.29.19.152</code>.</p>
<p>Now, I want to run a web server on, say, port <code>3000</code>. How can I expose the web server to the local network?</p>
<p>It's simple. Open a <em>PowerShell instance as an Administrator</em> and run the following command:</p>
<pre><code class="lang-powershell">netsh interface portproxy add v4tov4 listenport=&lt;host<span class="hljs-literal">-machine</span><span class="hljs-literal">-port</span>&gt; listenaddress=<span class="hljs-number">0.0</span>.<span class="hljs-number">0.0</span> connectport=&lt;wsl2<span class="hljs-literal">-instance</span><span class="hljs-literal">-port</span>&gt; connectaddress=&lt;wsl2<span class="hljs-literal">-instance</span><span class="hljs-literal">-ip</span>&gt;
</code></pre>
<p>Replacing the values in, we get the following:</p>
<pre><code class="lang-powershell">netsh interface portproxy add v4tov4 listenport=<span class="hljs-number">3000</span> listenaddress=<span class="hljs-number">0.0</span>.<span class="hljs-number">0.0</span> connectport=<span class="hljs-number">3000</span> connectaddress=<span class="hljs-number">172.29</span>.<span class="hljs-number">19.152</span>
</code></pre>
<div data-node-type="callout">
<div data-node-type="callout-emoji">ℹ</div>
<div data-node-type="callout-text">That's right, you don't need to type in the host machine IP address in this command.</div>
</div>

<p>After executing that, incoming traffic from the network to the host machine (<code>192.168.0.2:3000</code>) will be routed to your WSL2 instance (<code>172.29.19.152:3000</code>).</p>
<p>So go ahead, grab your phone or any other device, and try reaching <code>192.168.0.2:3000</code>. It should work.</p>
<p><strong>Note that the IP of your WSL2 will likely change after you reboot your machine or the WSL2 instance.</strong> In that case, you'll need to grab the new WSL2 IP and run the PowerShell command again. It is annoying, but it is what it is.</p>
<h3 id="heading-but-it-didnt-work">But it didn't work!</h3>
<p>If it's still not working after doing everything I told you to do, then you'll need to update your firewall rules.</p>
<p>I won't get into details, <strong>but you should create an inbound rule for your firewall, allowing TCP connections to the specific ports you want to unlock</strong>. In our example, it's port <code>3000</code>. My advice is to name the rule something along the lines of "WSL2 Unlock". Otherwise, believe me, you won't find it.</p>
<p>It should look like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1701178995468/495044d9-f1aa-439e-94d1-e6264654e760.png" alt class="image--center mx-auto" /></p>
<p>Now it should be working fine and you should be able to get back to work.</p>
<p>If it still doesn't work you can always use Ngrok.</p>
]]></content:encoded></item></channel></rss>