Skip to content

feniix/someps1

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

WSL Port Forwarding Scripts

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.

Scripts Overview

1. wsl-ssh-portforward.ps1 - Simple SSH Setup

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 22

2. wsl-ssh-cleanup.ps1 - Remove Port Forwarding

Removes existing SSH port forwarding rules.

Usage:

# Run as Administrator
.\wsl-ssh-cleanup.ps1

# Custom port
.\wsl-ssh-cleanup.ps1 -ListenPort 2222

3. wsl-port-manager.ps1 - Comprehensive Port Manager

Full-featured port management with support for multiple services and distributions.

Actions:

  • list - Show all current port forwarding rules
  • add - Add new port forwarding rules
  • remove - Remove existing rules
  • show - Display WSL distributions and their IP addresses
  • help - 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 22

Predefined Services

The 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

Prerequisites

  1. Administrator Privileges: All port forwarding operations require running PowerShell as Administrator
  2. WSL Installed: Windows Subsystem for Linux must be installed and configured
  3. Running WSL Distribution: The target WSL distribution must be running

Setting Up SSH Access

To enable SSH access to your WSL distribution:

  1. Install SSH server in WSL:

    sudo apt update
    sudo apt install openssh-server
  2. Configure SSH (optional):

    sudo nano /etc/ssh/sshd_config
    # Modify settings as needed
  3. Start SSH service:

    sudo service ssh start
    
    # To start automatically
    sudo systemctl enable ssh
  4. Run the port forwarding script:

    # Run as Administrator
    .\wsl-ssh-portforward.ps1
  5. Configure Windows Firewall (if needed):

    New-NetFirewallRule -DisplayName 'WSL SSH' -Direction Inbound -Protocol TCP -LocalPort 22 -Action Allow

Troubleshooting

Common Issues

  1. "Access Denied" errors: Make sure you're running PowerShell as Administrator
  2. Can't connect to WSL: Ensure the WSL distribution is running (wsl -d Ubuntu-24.04)
  3. Connection refused: Check if the service (like SSH) is running in WSL
  4. External connections blocked: Configure Windows Firewall to allow the ports

Checking WSL Status

# 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

Checking Port Forwarding Rules

# Show all current rules
netsh interface portproxy show v4tov4

# Show firewall rules
Get-NetFirewallRule | Where-Object DisplayName -like "*WSL*"

System Requirements

  • Windows 10/11 with WSL2 installed
  • PowerShell 5.1 or PowerShell 7+
  • Administrator privileges for port forwarding operations

Security Considerations

  • 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

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published