Skip to content

Instantly share code, notes, and snippets.

@csgordon
Created June 24, 2020 15:51
Show Gist options
  • Save csgordon/a276b0447b58eab9edade8550be31d66 to your computer and use it in GitHub Desktop.
Save csgordon/a276b0447b58eab9edade8550be31d66 to your computer and use it in GitHub Desktop.

I've been running Linux in Hyper-V for some time, as an alternative to remotely connecting or dual-booting. A perpetual problem with this, however, is that Hyper-V's hyperv_fb driver only supports monitor resolutions up to 1600x1200, and doesn't support resizing. (Microsoft has guest extensions, but they are gradually maintained for fewer and fewer variants of Linux; currently it appears only RHEL is actively maintained, with the latest Ubuntu release supporting 18.04. It is now 2020.)

I've tried a number of different approaches to connecting remotely to a Hyper-V machine over the years, and most break after some period of time. I have a relatively stable solution for connecting to a Docker container with RDP, but if I want to run programs using lots of memory, I don't necessarily want to dedicate that much memory to the main Docker host VM.

The following short script is based on instructions I found for making it possible to SSH into a Hyper-V guest. I've adapted it to also set up remote desktop access.

On Windows, elevated PowerShell:

New-VMSwitch -SwitchName "NATSwitch" -SwitchType Internal
New-NetIPAddress -IPAddress 192.168.10.1 -PrefixLength 24 -InterfaceAlias "vEthernet (NATSwitch)"
New-NetNAT -Name "NATNetwork" -InternalIPInterfaceAddressPrefix 192.168.10.0/24

Then set up the Linux installation of your choice. Install openssh-server, xrdp, and tigervnc (package names may vary; these are Debian).

Assuming you've configured the ssh server and xrdp server to run on boot, the ssh server listens on port 22, while the RDP server listens on port 3389. We'll reroute local host ports on the Windows machine to those ports on the VM.

With the VM up and running:

Add-NetNatStaticMapping -ExternalIPAddress "0.0.0.0/24" -ExternalPort 22 -Protocol TCP -InternalIPAddress "192.168.10.2" -InternalPort 22 -NatName NATNetwork
Add-NetNatStaticMapping -ExternalIPAddress "0.0.0.0/24" -ExternalPort 9999 -Protocol TCP -InternalIPAddress "192.168.10.2" -InternalPort 3389 -NatName NATNetwork

This sets things up so that connecting to via ssh to localhost (localhost:22 technically, but 22 is the default SSH port) connects to the VM. It also configures remote desktop connections to localhost:9999 to route to the VM's xrdp. Note that I changed the external port here because I often do this on a machine where I already want to use RDP for the actual Windows Desktop, which will already be listening on port 3389 of the Windows host.

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