Skip to content

Instantly share code, notes, and snippets.

@qtfkwk
Created January 20, 2021 14:44
Show Gist options
  • Save qtfkwk/867ca2a7edac576343d193d1b2f65a84 to your computer and use it in GitHub Desktop.
Save qtfkwk/867ca2a7edac576343d193d1b2f65a84 to your computer and use it in GitHub Desktop.
WSL SSH
# Usage
#
# 1. Install WSL https://aka.ms/wsl
# 2. Install distribution (Microsoft Store > Debian) https://www.microsoft.com/en-us/p/debian/9msvkqc78pk6
# 3. Initialize WSL (start it, set username, etc)
# 4. Install SSH server
#
# ```bash
# sudo apt update && sudo apt install openssh-server -y
# ```
#
# 5. Configure desired listen port
#
$ssh_listen_port=2201
#
# 6. Get WSL IP address
#
$ip=wsl ip a show dev eth0 `|grep 'inet ' `|cut -d ' ' -f6 `|cut -d / -f1
#
# 7. Configure port forward
#
netsh interface portproxy delete v4tov4 $ssh_listen_port 0.0.0.0
netsh interface portproxy add v4tov4 $ssh_listen_port $ip 22 0.0.0.0
netsh interface portproxy show v4tov4
#
# 8. Start SSH server
#
wsl service ssh status
wsl sudo service ssh start
wsl service ssh status
#
# 9. Open firewall port
#
# ```powershell
# netsh advfirewall firewall add rule name="WSL2 SSH $ssh_listen_port" protocol=TCP dir=in localport=$ssh_listen_port action=allow
# netsh advfirewall firewall show rule name="WSL2 SSH $ssh_listen_port"
# ```
#
# 10. Place this file at `$env:userprofile\wsl-ssh.ps1`; i.e. `C:\Users\username\wsl-ssh.ps1`
# 11. Set to run at boot:
#
# ```powershell
# $job_name = "wslssh"
# $trigger = New-JobTrigger -AtStartup -RandomDelay 00:00:30
# Register-ScheduledJob -Trigger $trigger -FilePath $env:userprofile\wsl-ssh.ps1 -Name $job_name
# Get-ScheduledJob $job_name
# ```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment