Skip to content

Commit

Permalink
Update Bootstrap-RemoteHostViaSSH.ps1 updated wsl install instruction…
Browse files Browse the repository at this point in the history
…s to allow for wsl within ssh session
  • Loading branch information
pldmgg authored Nov 29, 2023
1 parent 68fe898 commit 9cff31d
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions MyScripts/Bootstrap-RemoteHostViaSSH.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 9cff31d

Please sign in to comment.