From 9cff31d9175ad17aaa9d5906438f907af2fc8ce3 Mon Sep 17 00:00:00 2001 From: Paul DiMaggio Date: Wed, 29 Nov 2023 16:11:17 -0500 Subject: [PATCH] Update Bootstrap-RemoteHostViaSSH.ps1 updated wsl install instructions to allow for wsl within ssh session --- MyScripts/Bootstrap-RemoteHostViaSSH.ps1 | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/MyScripts/Bootstrap-RemoteHostViaSSH.ps1 b/MyScripts/Bootstrap-RemoteHostViaSSH.ps1 index b8168aa..63d876e 100644 --- a/MyScripts/Bootstrap-RemoteHostViaSSH.ps1 +++ b/MyScripts/Bootstrap-RemoteHostViaSSH.ps1 @@ -240,6 +240,9 @@ wsl --set-default-version 2 Restart-Computer -Force mstsc /v:$RemoteIPAddress # Just wait for wsl to pop open a window to finish the install +# IMPORTANT NOTE: At this point, in order to get wsl to work from within an ssh session to the host machine... +# ...open the Windows "Settings" GUI and navigate Apps -> Installed Apps -> Search for "linux" -> Uninstall everything EXCEPT the Windows Subsystem for Linux Update +# The "Ubuntu" instance you already setup should still be fully configured with everything you've already done. wsl sudo apt update && sudo apt upgrade -y sudo apt install openssh-server -y @@ -252,18 +255,28 @@ exit New-NetFirewallRule -Name wsl_sshd -DisplayName 'OpenSSH Server (sshd) for WSL' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 2222 # Now we want to create a scheduled task that will start WSL AND ssh within WSL on boot $ScriptOutputPath = "C:\Scripts\bin\wsl_sshd.ps1" -$ScriptContentAsString = @' -# Set Log Info and WSL Instance Name +$ScriptContentAsString = @'# Set Log Info and WSL Instance Name $LogFileDir = "C:\Scripts\logs" $LogFilePath = $LogFileDir + '\' + 'start_wsl_sshd_' + $(Get-Date -Format MMddyy_hhmmss) + '.log' $WslInstanceName = "Ubuntu" # Start SSH service in WSL -bash.exe -c "sudo /usr/sbin/service ssh start" -#wsl --distribution "$WslInstanceName" --user root /bin/sh -c "/usr/sbin/service ssh start" +try { + bash.exe -c "sudo /usr/sbin/service ssh start" + #wsl --distribution "$WslInstanceName" --user root /bin/sh -c "/usr/sbin/service ssh start" +} catch { + $ErrMsg = $_.Exception.Message + $null = Add-Content -Path $LogFilePath -Value $ErrMsg + Write-Error $ErrMsg + return +} # Remove port proxy rule -netsh.exe interface portproxy delete v4tov4 listenport=2222 listenaddress=0.0.0.0 protocol=tcp +try { + netsh.exe interface portproxy delete v4tov4 listenport=2222 listenaddress=0.0.0.0 protocol=tcp +} catch { + Write-Host "Continuing..." +} # Get IP address from WSL try {