Configuring External SSH Access to WSL on Windows
Installing OpenSSH in WSL
Begin by reinstalling OpenSSH server in your WSL instance:
sudo apt-get remove openssh-server
sudo apt-get install openssh-server
Modify the SSH server configuration:
sudo vi /etc/ssh/sshd_config
Update these crtiical parameters:
Port 22
PermitRootLogin Yes
PasswordAuthentication Yes
Automated Service Startup
Create a startup script to automatically launch the SSH service when WSL starts.
Port Forwarding Configuration
wslname = "Ubuntu-20.04"
wslport = "2222"
winport = "2222"
Set objShell = CreateObject("WScript.Shell")
objShell.Run "cmd /c wsl -d Ubuntu-20.04 -u root /etc/init.wsl start", 0, True
objShell.Run "cmd /c ""wsl -d " & wslname & " -u root hostname -I"">C:\Windows\Temp\wslip.tmp", 0, True
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("C:\Windows\Temp\wslip.tmp", 1)
wslip = f.ReadAll()
f.Close()
objShell.Run "cmd /c ""netsh interface portproxy add v4tov4 listenport=" & winport & " listenaddress=0.0.0.0 connectport=" & wslport & " connectaddress=" & wslip, 0, True
fso.DeleteFile("C:\Windows\Temp\wslip.tmp")
Automatic Startup Methods
Method 1: Startup Folder
Place your script in the user's startup folder (accessible via shell:startup).
Method 2: Scheduled Task Create a task with these properties:
- Run with highest privileges
- Set triggger for system startup
- Action to execute your VBS script
- Configure additional conditions as needed
Verification
Check succesfull port forwarding with:
wsl --list --running
netsh interface portproxy show all