Skip to content

Instantly share code, notes, and snippets.

@caseyWebb
Last active July 9, 2021 18:28
Show Gist options
  • Save caseyWebb/7aa6681ad753305892abc3bf27ac5769 to your computer and use it in GitHub Desktop.
Save caseyWebb/7aa6681ad753305892abc3bf27ac5769 to your computer and use it in GitHub Desktop.
Headless Windows Setup (WSL + OpenSSH)
#!/bin/bash
# Run this on the VM host
DOWNLOAD_URL=<download url> # Go to https://developer.microsoft.com/en-us/windows/downloads/virtual-machines to get the latest download url
VMNAME=UniversitySite
CPUS=6
RAM=10240
# Download VM from Microsoft
aria2c -s 16 -x 16 $DOWNLOAD_URL -o WinDev.zip
# Extract zip
7z e WinDev.zip
# Import virtual appliance
vboxmanage import WinDev.ova --vsys 0 --vmname $VMNAME --cpus $CPUS --memory $RAM
# Configure networking
#
# Setting the mac address isn't necessary, but it will ensure that recreating the VM on the same host will maintain the same DHCP assignments (i.e. IP)
vboxmanage modifyvm $VMNAME --nic1 bridged --bridgeadapter1 enp2s0f0 --macaddress1 080027B514B5
# Enable remote desktop
vboxmanage modifyvm $VMNAME --vrde on
# Clean up
# rm -f WinDev.zip WinDev.ova
# Start it up
vboxmanage startvm $VMNAME --type headless
#!/usr/bin/env bash
# RDP into the VM and run this in the WSL.
#
# The default password is "password"
# Configure port to 2222
sudo sed -i -e 's/Port 22/Port 2222/' /etc/ssh/sshd_config
# Enable password-based authentication (recommended to set up keys once you're able to use ssh-copy-id,
# see the comment at the bottom of the last script).
sudo sed -i -e 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
# Generate new server certs
sudo dpkg-reconfigure openssh-server
# Start ssh
sudo service ssh start
# Append Windows PATH (so you can run native Windows exes. You usually get this by default
# with the WSL when launched with `bash` or `ubuntu` from a powershell session because it
# will inherit the existing path, but you lose out on it when logging in via SSH due to no
# parent powershell session).
echo "WINPATH=\$(/mnt/c/Windows/System32/WindowsPowerShell/v1.0/powershell.exe -Command \"echo \\\$Env:Path\" | sed \"s/C:/\/mnt\/c/g\" | sed \"s/\\\\\/\//g\" | sed \"s/\ /\\\\\ /g\" | sed \"s/;/:/g\")" >> ~/.profile
echo "PATH=\"\$WINPATH\$PATH\"" >> ~/.profile
# Run this in an elevated (administrator) Powershell session in the VM
# Punch a hole in the firewall for the SSH server we installed in the WSL (which shares the host's networking)
Import-Module NetSecurity
New-NetFirewallRule -Name WSL_SSH -DisplayName "Allow SSH access to WSL" -Enabled True -Profile Any -Action Allow
# While we're here, set the hostname to something nicer and reboot to apply
$(Get-WmiObject Win32_ComputerSystem).Rename("<hostname>")
Restart-Computer
# If all was successful, you should be able to ssh into your VM from the LAN (provided the VM host firewall permits it)
#
# `ssh -p 2222 user@<hostname or ip>`
#
# If that doesn't work, check the VM host firewall, and ensure the SSH server is running in the WSL (`service ssh status` and `service ssh start`)
# From here, it is recommended to do basic linux setup and hardening. If you don't know how to do this, see here:
# https://www.digitalocean.com/community/tutorials/initial-server-setup-with-ubuntu-16-04

Headless Windows VM Setup

These scripts contain the basic procedure to import a new Windows development VM to VirtualBox and set it up for headless usage via SSH.

It is not a headless or unattended install; don't run these and walk away expecting to come back to a ready to use setup. You will be prompted for input as times.

To use, read each script, and run it in the specified environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment