Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- WSL2 SSHD
- (From https://medium.com/@gilad215/ssh-into-a-wsl2-host-remotely-and-reliabley-578a12c91a2)
- Install sshd
- $ sudo apt install openssh-server
- Config sshd with these values
- $ sudo nano /etc/ssh/sshd_config
- Port 2222
- ListenAddress 0.0.0.0
- PasswordAuthentication yes
- Remove password to sudo
- $ sudo nano /etc/sudoers.d/README
- %sudo ALL=NOPASSWD: /usr/sbin/service ssh *
- Create powershell script file "C:\scripts\wsl-ports.ps1"
- #########
- # WSL2 network port forwarding script v1
- # for enable script, 'Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser' in Powershell,
- # for delete exist rules and ports use 'delete' as parameter, for show ports use 'list' as parameter.
- # written by Daehyuk Ahn, 2020-08-01
- # You should modify '$Ports' for your applications
- $Ports = (2222,8080)
- # Start SSH Service.
- wsl sudo service ssh start
- # Display all portproxy information
- If ($Args[0] -eq "list") {
- netsh interface portproxy show v4tov4;
- exit;
- }
- # If elevation needed, start new process
- If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
- # Relaunch as an elevated process:
- Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path),"$Args runas" -Verb RunAs
- exit
- }
- # Check WSL ip address
- wsl hostname -I | Set-Variable -Name "WSL"
- $found = $WSL -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
- if (-not $found) {
- echo "WSL2 cannot be found. Terminate script.";
- exit;
- }
- # Remove and Create NetFireWallRule
- Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock';
- if ($Args[0] -ne "delete") {
- New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $Ports -Action Allow -Protocol TCP;
- New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $Ports -Action Allow -Protocol TCP;
- }
- # Add each port into portproxy
- Foreach ($Port in $Ports) {
- iex "netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=$Port | Out-Null";
- if ($Args[0] -ne "delete") {
- iex "netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=$Port connectaddress=$WSL connectport=$Port | Out-Null";
- }
- }
- # Display all portproxy information, Give user to chance to see it when relaunched start
- netsh interface portproxy show v4tov4;
- If ($Args[0] -eq "runas" -Or $Args[1] -eq "runas") {
- Write-Host -NoNewLine 'Press any key to close! ';
- $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
- }
- #########
- Open Startup folder Win+R, "shell:startup", create shortcut:
- C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "C:\scripts\wsl-ports.ps1"
- Right click, properties, Start in: C:\scripts\
- Run the shortcut
- Access the sshd
- $ ssh {wsluser}@{yourcomputer} -p 2222
Add Comment
Please, Sign In to add comment