Introduction

Provide an overview of what WSL2 (Windows Subsystem for Linux 2) is, and why it is useful for developers who want to use Linux environments without leaving Windows. Highlight the goal of the article: Setting up WSL2 and linking it with PhpStorm for a smooth development experience from Mac or any other OS via SSH.

Prerequisites

Before starting, list the prerequisites:

  1. A Windows 10 or 11 system with WSL2 enabled.
  2. PhpStorm installed on the Mac or another OS (you can install PhpStorm from JetBrains).
  3. Basic knowledge of SSH and remote connections.
  4. Windows with OpenSSH client and server enabled.

1. Setting Up WSL2 on Windows

a. Install WSL2 and a Linux Distribution

Guide readers through the installation of WSL2 and a Linux distribution:

  • Open PowerShell as Administrator and run:bashCopy codewsl --install This command installs WSL2 and sets it as the default version.
  • Install a Linux distribution (e.g., Ubuntu):bashCopy codewsl --install -d Ubuntu
  • Launch Ubuntu (or the selected distribution) and complete the setup by creating a new user.

b. Set WSL2 as the Default Version

In case WSL2 isn’t set as the default version, run:

wsl --set-default-version 2

2. Setting Up OpenSSH on WSL2

Explain how to set up SSH in WSL2 so that PhpStorm can connect to it:

a. Install OpenSSH Server in WSL2

In your WSL2 terminal, run the following commands:

sudo apt update
sudo apt install openssh-server

b. Configure OpenSSH to Start Automatically

Edit the SSH configuration to ensure it runs on startup:

  1. Open the SSH configuration file:bashCopy codesudo nano /etc/ssh/sshd_config
  2. Ensure the Port is set to 22 or another port of your choice: 2222
  3. Start the SSH service and enable it to run automatically:bashCopy codesudo service ssh start sudo service ssh enable

1. Firewall Rule for Inbound Traffic

The first command creates a new firewall rule to allow inbound traffic on port 2222 on your Private profile (LAN development):

New-NetFireWallRule -Profile Private -DisplayName 'Expo ports for LAN development' `
-Direction Inbound -LocalPort 2222 -Action Allow -Protocol TCP

This ensures that traffic on port 2222 is allowed through the Windows firewall for devices on the same network (LAN).

2. Port Proxy Rule for Port Forwarding

The second command sets up the port forwarding rule for WSL2 by dynamically getting the WSL2 IP address using PowerShell’s $($(wsl hostname -I).Trim()):

netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 `
connectport=2222 connectaddress=$($(wsl hostname -I).Trim())

This command:

  • Listens on port 2222 on all available interfaces (0.0.0.0).
  • Forwards the traffic to port 2222 on the dynamically fetched WSL2 IP address (retrieved by wsl hostname -I).

Why This Works:

  • The firewall rule ensures the traffic is allowed in.
  • The portproxy rule dynamically forwards traffic from the Windows host to WSL2, using WSL2’s IP address, which may change after each restart.

Table of Useful Commands for WSL2 and SSH Setup

CommandDescription
ip addr show eth0Displays the IP address of the WSL2 network interface (eth0).
sudo netstat -tuln | grep sshThis will show whether the SSH server is listening on the correct port (e.g., 22 or 2222), and it should be bound to 0.0.0.0
sudo service ssh statusChecks the status of the SSH service on WSL2.
sudo service ssh startStarts the SSH service in WSL2.
sudo service ssh restartRestarts the SSH service to apply configuration changes.
netsh interface portproxy show v4tov4Lists all port forwarding rules on the Windows host for traffic forwarding to WSL2.
netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=22 connectaddress=<WSL2_IP>Adds a port forwarding rule from port 2222 on Windows to port 22 on WSL2.
netsh interface portproxy delete v4tov4 listenport=2222 listenaddress=0.0.0.0Deletes a port forwarding rule for port 2222.
netsh advfirewall firewall add rule name="Allow SSH Port 2222" protocol=TCP dir=in localport=2222 action=allowAdds a Windows Firewall rule to allow traffic on port 2222.
wsl --shutdownShuts down all running WSL2 instances.
sudo tail -f /var/log/auth.logMonitors the SSH logs in real-time on WSL2 to track connection attempts.

Conclusion

Summarize how setting up WSL2 and linking it with PhpStorm enhances the development workflow by providing a full Linux development environment while using a powerful IDE like PhpStorm from Mac or any other OS.

Categorized in:

PHP Coding, Technology, Ubuntu,