This collection of PowerShell scripts helps you manage port forwarding between Windows and WSL (Windows Subsystem for Linux) distributions. These scripts are particularly useful for accessing services running in WSL from external machines or when WSL's default networking doesn't work as expected.
A straightforward script that sets up SSH port forwarding for a specific WSL distribution.
Features:
- Automatic WSL IP detection
- Administrator privilege checking
- Error handling and validation
- Firewall reminder
Usage:
# Run as Administrator
.\wsl-ssh-portforward.ps1
# Custom distribution
.\wsl-ssh-portforward.ps1 -WSLDistribution "Ubuntu-22.04"
# Custom ports
.\wsl-ssh-portforward.ps1 -ListenPort 2222 -ConnectPort 22Removes existing SSH port forwarding rules.
Usage:
# Run as Administrator
.\wsl-ssh-cleanup.ps1
# Custom port
.\wsl-ssh-cleanup.ps1 -ListenPort 2222Full-featured port management with support for multiple services and distributions.
Actions:
list- Show all current port forwarding rulesadd- Add new port forwarding rulesremove- Remove existing rulesshow- Display WSL distributions and their IP addresseshelp- Show usage information
Usage Examples:
# Show help
.\wsl-port-manager.ps1 -Action help
# List current rules
.\wsl-port-manager.ps1 -Action list
# Show WSL distributions
.\wsl-port-manager.ps1 -Action show
# Add SSH forwarding (predefined service)
.\wsl-port-manager.ps1 -Action add -Service ssh
# Add custom port forwarding
.\wsl-port-manager.ps1 -Action add -ListenPort 8080 -ConnectPort 3000
# Add forwarding for different distribution
.\wsl-port-manager.ps1 -Action add -Service ssh -Distribution "Ubuntu-22.04"
# Remove port forwarding
.\wsl-port-manager.ps1 -Action remove -ListenPort 22The port manager includes these predefined services:
| Service | Listen Port | Connect Port | Description |
|---|---|---|---|
| ssh | 22 | 22 | SSH access |
| http | 80 | 80 | HTTP web server |
| https | 443 | 443 | HTTPS web server |
| docker | 2375 | 2375 | Docker daemon |
| jupyter | 8888 | 8888 | Jupyter Notebook |
| webapp | 3000 | 3000 | Development web app |
- Administrator Privileges: All port forwarding operations require running PowerShell as Administrator
- WSL Installed: Windows Subsystem for Linux must be installed and configured
- Running WSL Distribution: The target WSL distribution must be running
To enable SSH access to your WSL distribution:
-
Install SSH server in WSL:
sudo apt update sudo apt install openssh-server
-
Configure SSH (optional):
sudo nano /etc/ssh/sshd_config # Modify settings as needed -
Start SSH service:
sudo service ssh start # To start automatically sudo systemctl enable ssh
-
Run the port forwarding script:
# Run as Administrator .\wsl-ssh-portforward.ps1
-
Configure Windows Firewall (if needed):
New-NetFirewallRule -DisplayName 'WSL SSH' -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow
- "Access Denied" errors: Make sure you're running PowerShell as Administrator
- Can't connect to WSL: Ensure the WSL distribution is running (
wsl -d Ubuntu-24.04) - Connection refused: Check if the service (like SSH) is running in WSL
- External connections blocked: Configure Windows Firewall to allow the ports
# List WSL distributions
wsl --list --verbose
# Check if specific distribution is running
wsl -d Ubuntu-24.04 echo "WSL is running"
# Get WSL IP address manually
wsl -d Ubuntu-24.04 hostname -I# Show all current rules
netsh interface portproxy show v4tov4
# Show firewall rules
Get-NetFirewallRule | Where-Object DisplayName -like "*WSL*"- Windows 10/11 with WSL2 installed
- PowerShell 5.1 or PowerShell 7+
- Administrator privileges for port forwarding operations
- Port forwarding exposes services to the network - ensure proper authentication
- Consider using non-standard ports for external-facing services
- Regularly review and clean up unused port forwarding rules
- Use Windows Firewall to restrict access to specific IP ranges if needed
Your original script has been enhanced and included as wsl-ssh-portforward.ps1. The key improvements include:
- Error handling: Validates WSL distribution exists and is running
- Administrator checking: Ensures script has required privileges
- Better output: Colored output and progress information
- Parameterization: Configurable distribution and ports
- Firewall reminder: Alerts about potential firewall configuration
The core functionality remains the same but with added robustness and user-friendliness.