<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daniel Corona ⚡</title>
    <description>The latest articles on DEV Community by Daniel Corona ⚡ (@dnlcorona).</description>
    <link>https://dev.to/dnlcorona</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1055596%2F7af706bc-a593-4210-bd75-86781daae1c9.jpeg</url>
      <title>DEV Community: Daniel Corona ⚡</title>
      <link>https://dev.to/dnlcorona</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dnlcorona"/>
    <language>en</language>
    <item>
      <title>Running a development server on WSL2 and connecting devices to the server via port forwarding on the local network. 🚀</title>
      <dc:creator>Daniel Corona ⚡</dc:creator>
      <pubDate>Tue, 16 May 2023 23:09:54 +0000</pubDate>
      <link>https://dev.to/dnlcorona/running-a-development-server-on-wsl2-and-connecting-devices-to-the-server-via-port-forwarding-on-the-local-network-50m7</link>
      <guid>https://dev.to/dnlcorona/running-a-development-server-on-wsl2-and-connecting-devices-to-the-server-via-port-forwarding-on-the-local-network-50m7</guid>
      <description>&lt;h2&gt;
  
  
  🌐 Connecting to WSL2 server via local network
&lt;/h2&gt;

&lt;p&gt;❓ Are you unable to connect to your WSL server from another computer or your mobile device on the same network?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftzz0qoldggzs20qzyi7q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftzz0qoldggzs20qzyi7q.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptoiqhxgu1numjnsiqvz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fptoiqhxgu1numjnsiqvz.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Running my local server in WSL2 with Next.js/CRA and being unable to access it from other devices on the local network.)&lt;/p&gt;




&lt;h3&gt;
  
  
  ⚙ Follow the steps below and you should be able to access the WSL2 server through other devices on the same local network.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  ▶ Step 1
&lt;/h3&gt;

&lt;p&gt;On the &lt;em&gt;Windows PowerShell&lt;/em&gt; or &lt;em&gt;cmd&lt;/em&gt;, use the command &lt;code&gt;ipconfig&lt;/code&gt;, and search for &lt;strong&gt;IPv4 Address&lt;/strong&gt; to find your &lt;strong&gt;Windows&lt;/strong&gt; IP address. &lt;/p&gt;

&lt;h3&gt;
  
  
  ▶ Step 2
&lt;/h3&gt;

&lt;p&gt;Open the port on your machine in order to access it over your network. Run the command bellow in &lt;strong&gt;Windows PowerShell as admin&lt;/strong&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

netsh advfirewall firewall add rule name="Allowing LAN connections" dir=in action=allow protocol=TCP localport=3000


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In my case, the port that I allowed is &lt;strong&gt;3000&lt;/strong&gt;.&lt;br&gt;
(Change according to the port you want to allow -&amp;gt; &lt;code&gt;localport=PORT_TO_ALLOW&lt;/code&gt;)&lt;/p&gt;

&lt;h3&gt;
  
  
  ▶ Step 3
&lt;/h3&gt;

&lt;p&gt;Still in Windows PowerShell, pass the port that the app is listening on to the corresponding port within WSL. Follow the command below to do this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

netsh interface portproxy add v4tov4 listenport=3000 listenaddress=YOUR_IPV4_ADDRESS connectport=3000 connectaddress=$($(wsl hostname -I).Trim());


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;listenport&lt;/code&gt;: The port that Windows will listen to.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;listenaddress&lt;/code&gt;: The address that your Windows will listen to (finded on step 1).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;connectport&lt;/code&gt;: The port that your Linux distribution through WSL2 will listen to.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;connectaddress&lt;/code&gt;: The public IP of your Linux WSL2 instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📝 (Note that the Hyper-V IP changes every time Windows reboots, so the public IP in WSL2 also changes). To avoid hardcoding due to updating the WSL IP every reboot, pass the &lt;code&gt;$($(wsl hostname -I).Trim())&lt;/code&gt; to dynamically put the IP of the WSL.&lt;/p&gt;

&lt;p&gt;🔥 Finally, to see if everything is working properly, write the command:&lt;br&gt;
&lt;code&gt;netsh inteface portproxy show v4t0v4&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If everything is fine, you should see something like:&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fftsxej22yu3ox4oqpe3v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fftsxej22yu3ox4oqpe3v.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now, you can access your server using WINDOWS_IP:PORT_ALLOWED&lt;/strong&gt;&lt;br&gt;
🤓 In my case, &lt;code&gt;192.168.18.217:3000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;🎉🎉&lt;/p&gt;

</description>
      <category>wsl</category>
      <category>wsl2</category>
      <category>webdev</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
