Skip to content

Instantly share code, notes, and snippets.

@rhom6us
Last active September 27, 2024 05:05
Show Gist options
  • Save rhom6us/0bf30329f657237954a4e5593b1eecd6 to your computer and use it in GitHub Desktop.
Save rhom6us/0bf30329f657237954a4e5593b1eecd6 to your computer and use it in GitHub Desktop.

Setup WSL for Remote SSH Access

1. Reinstalling SSH on WSL 2

sudo apt remove openssh-server
sudo apt install openssh-server

2. Configuring SSH on WSL 2

Edit SSH config:

sudo nano /etc/ssh/sshd_config
  • Change #Port 22 to Port 22 to enable the default port.
  • Change #PasswordAuthentication yes to PasswordAuthentication yes to enable password authentication.

Restart SSH:

sudo service ssh restart

3. Configuring Port Forwarding on Windows

netsh interface portproxy delete v4tov4 listenport=2222 listenaddress=0.0.0.0 protocol=tcp
for /f %%i in ('wsl hostname -I') do set IP=%%i
netsh interface portproxy add v4tov4 listenport=2222 listenaddress=0.0.0.0 connectport=2022 connectaddress=%IP%

4. Enabling Inbound Rule on Windows Firewall

New-NetFirewallRule -Name sshd -DisplayName 'OpenSSH Server (sshd) for WSL' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 2222

5. Set up Port Forwarding on Router (For WAN)

  • Navigate to Port Forwarding settings in the router’s configuration page.
  • Forward traffic from port 2220 to port 2222 on the IP address of your Windows machine (e.g. 192.168.100.1).

6. Verifying SSH to WSL

Verifying on a Local Area Network

ssh wsl2username@192.168.100.2. -p 2222

Verifying on a Wide Area Network

ssh wsl2username@172.16.254.3 -p 2220
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment