sohotcall

WSL2 SSHD

Jan 14th, 2021
72
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WSL2 SSHD
  2. (From https://medium.com/@gilad215/ssh-into-a-wsl2-host-remotely-and-reliabley-578a12c91a2)
  3.  
  4. Install sshd
  5. $ sudo apt install openssh-server
  6.  
  7. Config sshd with these values
  8. $ sudo nano /etc/ssh/sshd_config
  9. Port 2222
  10. ListenAddress 0.0.0.0
  11. PasswordAuthentication yes
  12.  
  13. Remove password to sudo
  14. $ sudo nano /etc/sudoers.d/README
  15. %sudo ALL=NOPASSWD: /usr/sbin/service ssh *
  16.  
  17. Create powershell script file "C:\scripts\wsl-ports.ps1"
  18. #########
  19. # WSL2 network port forwarding script v1
  20. # for enable script, 'Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser' in Powershell,
  21. # for delete exist rules and ports use 'delete' as parameter, for show ports use 'list' as parameter.
  22. # written by Daehyuk Ahn, 2020-08-01
  23.  
  24. # You should modify '$Ports' for your applications
  25. $Ports = (2222,8080)
  26.  
  27. # Start SSH Service.
  28. wsl sudo service ssh start
  29.  
  30. # Display all portproxy information
  31. If ($Args[0] -eq "list") {
  32. netsh interface portproxy show v4tov4;
  33. exit;
  34. }
  35.  
  36. # If elevation needed, start new process
  37. If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
  38. # Relaunch as an elevated process:
  39. Start-Process powershell.exe "-File",('"{0}"' -f $MyInvocation.MyCommand.Path),"$Args runas" -Verb RunAs
  40. exit
  41. }
  42.  
  43. # Check WSL ip address
  44. wsl hostname -I | Set-Variable -Name "WSL"
  45. $found = $WSL -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';
  46. if (-not $found) {
  47. echo "WSL2 cannot be found. Terminate script.";
  48. exit;
  49. }
  50.  
  51. # Remove and Create NetFireWallRule
  52. Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock';
  53. if ($Args[0] -ne "delete") {
  54. New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $Ports -Action Allow -Protocol TCP;
  55. New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $Ports -Action Allow -Protocol TCP;
  56. }
  57.  
  58. # Add each port into portproxy
  59. Foreach ($Port in $Ports) {
  60. iex "netsh interface portproxy delete v4tov4 listenaddress=0.0.0.0 listenport=$Port | Out-Null";
  61. if ($Args[0] -ne "delete") {
  62. iex "netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=$Port connectaddress=$WSL connectport=$Port | Out-Null";
  63. }
  64. }
  65.  
  66. # Display all portproxy information, Give user to chance to see it when relaunched start
  67. netsh interface portproxy show v4tov4;
  68. If ($Args[0] -eq "runas" -Or $Args[1] -eq "runas") {
  69. Write-Host -NoNewLine 'Press any key to close! ';
  70. $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown');
  71. }
  72. #########
  73.  
  74. Open Startup folder Win+R, "shell:startup", create shortcut:
  75. C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "C:\scripts\wsl-ports.ps1"
  76. Right click, properties, Start in: C:\scripts\
  77.  
  78. Run the shortcut
  79.  
  80. Access the sshd
  81. $ ssh {wsluser}@{yourcomputer} -p 2222
RAW Paste Data