Software & AppsOperating SystemLinux

How To Access WSL2 Ubuntu’s Application on Hosted Machine’s Network

Ubuntu 1

In today’s digital world, the ability to access applications remotely is a necessity. This article will guide you on how to access your WSL2 Ubuntu application on your hosted machine’s network.

Quick Answer

To access WSL2 Ubuntu’s application on a hosted machine’s network, you can set up port forwarding and configure firewall rules. For WSL1, you can directly connect to the WSL1 instance from another device on the network and set up a firewall rule. For WSL2, you can enable the Windows OpenSSH server, configure the SSH server to allow port forwarding, create a firewall rule, and initiate port forwarding using the SSH command. Alternatively, you can use the expose-wsl npm package as a workaround.

What is WSL2?

Windows Subsystem for Linux (WSL) is a compatibility layer developed by Microsoft for running Linux binary executables natively on Windows 10 and Windows Server 2019. In May 2019, Microsoft announced the release of WSL 2 which includes important changes such as a real Linux kernel, through a subset of Hyper-V features.

Accessing WSL2 Ubuntu Application

To access your WSL2 Ubuntu application on your office network, you need to set up port forwarding. This process may differ slightly depending on whether you’re using WSL1 or WSL2.

WSL1

If you are using WSL1, you can directly connect to the WSL1 instance from another device on the network. However, you will still need to configure a firewall rule. Run the following command in PowerShell to set up the firewall rule:

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -LocalPort 8080 -Protocol TCP -Action Allow

In this command:

  • -DisplayName "WSL" gives the rule a name for easy identification.
  • -Direction Inbound specifies that the rule applies to inbound traffic.
  • -LocalPort 8080 specifies the port to which the rule applies.
  • -Protocol TCP specifies that the rule applies to TCP traffic.
  • -Action Allow allows the traffic.

WSL2

WSL2 requires a bit more setup. The easiest method is to use SSH for port forwarding. Follow these steps:

Enable the Windows OpenSSH Server

Firstly, you need to enable the Windows OpenSSH server. Open PowerShell as Administrator and run the following commands:

Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0
Set-Service -Name sshd -StartupType 'Automatic'

These commands install the OpenSSH client and server, and set the SSH server to start automatically.

Configure the SSH Server

Next, you need to configure the SSH server to allow port forwarding. To do this, edit the C:\ProgramData\ssh\sshd_config file and make sure that GatewayPorts yes is not commented out. This enables the SSH server to forward ports.

Start the SSH Service

Start the SSH service by running the following command in PowerShell:

Start-Service sshd

This starts the SSH service.

Create a Firewall Rule

Create a firewall rule to allow inbound TCP traffic on the desired port (e.g., 8080). Run the following command in PowerShell:

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -LocalPort 8080 -Protocol TCP -Action Allow -Profile Private

This command creates a new firewall rule, similar to the one created for WSL1.

Initiate Port Forwarding

Now, from your Ubuntu/WSL2 instance, run the following command to initiate port forwarding:

ssh -f -N -R 8080:localhost:8080 "$(hostname).local"

This command establishes an SSH connection from WSL2 to the Windows host and forwards traffic received on port 8080 to the local WSL2 port 8080. This forwarding extends to other hosts on the network as well.

Using the expose-wsl Package

Another workaround is to use the expose-wsl npm package. This package automatically downloads and runs the WSLHostPatcher, which allows you to access your WSL2 application on your office network.

Install it by running the following command in your WSL terminal:

npx expose-wsl@latest

Remember to adjust the firewall rules and port numbers according to your specific setup.

Conclusion

Accessing your WSL2 Ubuntu application on a hosted machine’s network involves setting up port forwarding and configuring firewall rules. Although the process may seem complex, it becomes straightforward once you understand the steps involved. By following this guide, you should be able to access your applications remotely with ease.

What is the difference between WSL1 and WSL2?

WSL1 is the first version of Windows Subsystem for Linux, while WSL2 is an updated version that includes a real Linux kernel. WSL2 offers improved performance and compatibility compared to WSL1.

Can I access my WSL2 Ubuntu application from another device on the network?

Yes, you can access your WSL2 Ubuntu application from another device on the network by setting up port forwarding and configuring firewall rules.

How do I set up port forwarding for WSL1?

To set up port forwarding for WSL1, you need to configure a firewall rule using the New-NetFirewallRule command in PowerShell. The command should include the desired port number, protocol, and action.

How do I enable the Windows OpenSSH server for WSL2?

To enable the Windows OpenSSH server for WSL2, you can use PowerShell commands such as Add-WindowsCapability and Set-Service to install the OpenSSH client and server, and set the SSH server to start automatically.

How do I initiate port forwarding for WSL2 using SSH?

To initiate port forwarding for WSL2 using SSH, you need to establish an SSH connection from your WSL2 instance to the Windows host and forward traffic received on a specific port to the local WSL2 port. You can use the ssh command with the -f, -N, and -R options to achieve this.

Is there an alternative method to access WSL2 applications on the network?

Yes, an alternative method is to use the expose-wsl npm package, which automatically downloads and runs the WSLHostPatcher. This package simplifies the process of accessing WSL2 applications on the network.

Do I need to adjust firewall rules and port numbers for accessing WSL2 applications on the network?

Yes, you may need to adjust firewall rules and port numbers according to your specific setup. The provided commands and instructions serve as a general guide, but you should modify them as necessary to match your network configuration.

Leave a Comment

Your email address will not be published. Required fields are marked *