Last active
May 25, 2024 11:48
-
-
Save kennyhyun/adde29ca1ce43fa372731133672fc6cf to your computer and use it in GitHub Desktop.
Bootstrapping WSL1 debian with SSH server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (!$args[0]) { | |
write-error "taskName is require as the first argument" | |
exit -1; | |
} | |
if (!$args[1]) { | |
write-error "schedCommand is require as the second argument" | |
exit -1; | |
} | |
$taskName=$args[0] | |
$schedCommand=$args[1] | |
Write-host "Registinering new '$taskName'" | |
$Action = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument "-NonInteractive -NoLogo -ExecutionPolicy Bypass -Command `"$schedCommand`"" | |
$Trigger = @( | |
$(New-ScheduledTaskTrigger -AtLogon) | |
) | |
$Settings = New-ScheduledTaskSettingsSet | |
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings | |
$user=((Get-WMIObject -class Win32_ComputerSystem | Select-Object -ExpandProperty username)) | |
Register-ScheduledTask -TaskName $taskName -InputObject $Task -User "$user" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ErrorActionPreference = "Stop" | |
$DistroName = "Debian" | |
# Function to run a script with elevated privileges and capture the output | |
function Run-Elevated { | |
param ( | |
[string]$scriptBlock | |
) | |
$tempFile = [System.IO.Path]::GetTempFileName() | |
Start-Process powershell -WindowStyle Minimized -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"& { $scriptBlock } *>> $tempFile`"" -Verb RunAs -Wait | |
cat $tempFile | |
rm $tempFile | |
} | |
$envFilePath = ".env" | |
$envContent = Get-Content -Path $envFilePath | |
$envVariables = @{} | |
foreach ($line in $envContent) { | |
if ($line -match '^(?<key>[^=]+)=(?<value>.+)$') { | |
$envVariables[$Matches['key']] = $Matches['value'] | |
} | |
} | |
# Check if WSL is enabled | |
$scr = [scriptblock]::Create("((Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux)).State") | |
$wslStatus = Run-Elevated $scr | |
#$wslStatus = "Enabled" | |
if ($wslStatus -match "Enabled") { | |
Write-Host "WSL is already enabled." | |
} else { | |
Write-Host "WSL is not enabled. Enabling WSL..." | |
Run-Elevated "Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux" | |
Write-Host "WSL enabled. Please restart your computer and run the script again." | |
exit | |
} | |
wsl --update # install kernel update package if not exist | |
$wslList = ($(wsl --list) -split "\r?\n" | Select-Object -Skip 1) -join "`n" -replace "`0", "" | |
if ($wslList -match "$DistroName" -or $wslList -match "DistroName") { | |
Write-Host "$DistroName is already installed." | |
} else { | |
# Set the default WSL version to 1 | |
wsl --set-default-version 1 | |
Write-Host "Installing $DistroName..." | |
$process = Start-Process -FilePath "wsl" -ArgumentList "--install -d $DistroName" -WindowStyle Minimized -PassThru | |
#-NoNewWindow -PassThru | |
#-WindowStyle Minimized -PassThru | |
do { | |
write-host "waiting for $DistroName" | |
Start-Sleep -Seconds 1 | |
$wslList = wsl --list --verbose | Select-String -Pattern "$DistroName" | |
$wslList = ($(wsl --list) -split "\r?\n" | Select-Object -Skip 1) -join "`n" -replace "`0", "" | |
} while (-not($wslList -match "$DistroName")) | |
write-host "Stopping for now" | |
wsl --terminate $DistroName | |
Stop-Process -Name "$DistroName" -Force | |
} | |
## --------------- | |
# Set PASSWD, sshd | |
# | |
$USER = if ($envVariables['USER']) { $envVariables['USER'] } else { "admin" } | |
$DOTFILES_REPO = $envVariables['DOTFILES_REPO'] | |
write-host "Setting WSL 1 $DistroName" | |
# as root | |
wsl -d $DistroName -- bash -c "if [ -z `"`$(ls -d /home/$USER 2> /dev/null)`" ]; then adduser --disabled-password --gecos '' $USER; fi && if [ -z `"`$(ls /etc/sudoers.d/$USER 2> /dev/null)`" ]; then echo 'Setting NOPASSWD' && echo $USER' ALL=(ALL:ALL) NOPASSWD: ALL' > /etc/sudoers.d/$USER ; fi | |
if [ -z `"`$(grep '^default=$USER' /etc/wsl.conf 2> /dev/null)`" ];then echo Makeing $USER default && echo '[user]' >> /etc/wsl.conf && echo `"default=$USER`" >> /etc/wsl.conf ; fi | |
apt update && | |
apt install -y git openssh-server && | |
sed -i -E 's,^#?Port.*$,Port 3022,' /etc/ssh/sshd_config && | |
echo All done | |
" | |
if (-not $LASTEXITCODE) { | |
# restart to use default user | |
wsl --terminate $DistroName | |
# as ruser | |
wsl -d $DistroName -- bash -c "ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa -q -N '' && | |
echo Created ssh key && | |
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys && | |
echo ---------------- && | |
cat ~/.ssh/id_rsa.pub && | |
echo ---------------- | |
" | |
} | |
# setup scheduler task for start sshd | |
$taskName = "Start WSL sshd" | |
$existingScheduler = Get-ScheduledTask -TaskName $taskName -Erroraction silentlycontinue | |
if (-not $existingScheduler) { | |
Start-Process powershell -WindowStyle Minimized -ArgumentList "-ExecutionPolicy Bypass -file `"$PSScriptRoot\registerStartupTask.ps1`" `"$taskName`" `"wsl bash -c 'sudo /usr/sbin/service ssh start'`"" -Verb RunAs -Wait | |
Get-ScheduledTask -TaskName $taskName #-Erroraction silentlycontinue | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment