Skip to main content

Setup Hyper-V Enhanced Session for Ubuntu 20.04 LTS

Introduction#

Hyper-V's Enhanced Session Mode allows it to connect to virtual machines via RDP (remote desktop protocol). This not only improves your overall virtual machine viewing experience, but it also allows the virtual machine to share devices with your computer.

Download Ubuntu 20 Desktop#

Important

Do not use Quick Create in Hyper-V Manager to create the VM. Follow these steps precisely.

Hyper-V

To learn more about, please visit Introduction to Hyper-V on Windows

Download Ubuntu Desktop 20.04 LTS from here. Subsequent 20.xx versions should also work.

Direct link as of this writing is here.

Set up Hyper-V VM#

Refer to the screencast below and follow the steps to construct the new virtual machine in Hyper-V.

  1. Action
  2. New
  3. Virtual Machine
  4. Name: ubuntu2004
  5. Generation: 2
  6. Startup memory: 4096
  7. Virtual hard disk
  8. Connection: Default Switch
  9. Install from bootable image file: (Use iso downloaded above)
  10. PressOK button

Creating  new vm in Hyper-v

Set Ubuntu Compatible Security Template For Secure Boot#

IMPORTANT

Take a look at the screenshot below. Please follow the numbered screen sequence 1..5.

  1. Select the VM
  2. Settings
  3. Security
  4. Change Template to Microsoft UEFI Certificate Authority
  5. OK

Selecting Hyper-v Template for Secure Boot

Start VM#

  • Select Connect, then, Start

Install Ubuntu VM in Hyper-V#

Use all defaults.

WARNING

Do not choose Log in Automatically

Install and Run linux-vm-tool Script#

  • Update local package cache.
sudo apt-get update
  • Install git client tool.
sudo apt-get install --yes git
  • Clone Hyper-V Linux drivers repository.
git clone https://github.com/Hinara/linux-vm-tools.git
  • Select Hyper-V linux drivers for Ubuntu 20.04.
cd linux-vm-tools/ubuntu/20.04
  • Add the execute permision to install.sh script
chmod +x ./install.sh
  • Run the install script.
sudo ./install.sh
  • Reboot the Ubuntu desktop.
sudo shutdown -r now

The Contents of the install.sh Script#

IMPORTANT

This is solely for the sake of reference only. You're not doing anything here.

#!/bin/bash
#
# This script is for Ubuntu 20.04 Focal Fossa to download and install XRDP+XORGXRDP via
# source.
#
# Major thanks to: http://c-nergy.be/blog/?p=11336 for the tips.
#
###############################################################################
# Use HWE kernel packages
#
HWE=""
#HWE="-hwe-20.04"
###############################################################################
# Update our machine to the latest code if we need to.
#
if [ "$(id -u)" -ne 0 ]; then
echo 'This script must be run with root privileges' >&2
exit 1
fi
apt update && apt upgrade -y
if [ -f /var/run/reboot-required ]; then
echo "A reboot is required in order to proceed with the install." >&2
echo "Please reboot and re-run this script to finish the install." >&2
exit 1
fi
###############################################################################
# XRDP
#
# Install hv_kvp utils
apt install -y linux-tools-virtual${HWE}
apt install -y linux-cloud-tools-virtual${HWE}
# Install the xrdp service so we have the auto start behavior
apt install -y xrdp
systemctl stop xrdp
systemctl stop xrdp-sesman
# Configure the installed XRDP ini files.
# use vsock transport.
sed -i_orig -e 's/port=3389/port=vsock:\/\/-1:3389/g' /etc/xrdp/xrdp.ini
# use rdp security.
sed -i_orig -e 's/security_layer=negotiate/security_layer=rdp/g' /etc/xrdp/xrdp.ini
# remove encryption validation.
sed -i_orig -e 's/crypt_level=high/crypt_level=none/g' /etc/xrdp/xrdp.ini
# disable bitmap compression since its local its much faster
sed -i_orig -e 's/bitmap_compression=true/bitmap_compression=false/g' /etc/xrdp/xrdp.ini
# Add script to setup the ubuntu session properly
if [ ! -e /etc/xrdp/startubuntu.sh ]; then
cat >> /etc/xrdp/startubuntu.sh << EOF
#!/bin/sh
export GNOME_SHELL_SESSION_MODE=ubuntu
export XDG_CURRENT_DESKTOP=ubuntu:GNOME
exec /etc/xrdp/startwm.sh
EOF
chmod a+x /etc/xrdp/startubuntu.sh
fi
# use the script to setup the ubuntu session
sed -i_orig -e 's/startwm/startubuntu/g' /etc/xrdp/sesman.ini
# rename the redirected drives to 'shared-drives'
sed -i -e 's/FuseMountName=thinclient_drives/FuseMountName=shared-drives/g' /etc/xrdp/sesman.ini
# Changed the allowed_users
sed -i_orig -e 's/allowed_users=console/allowed_users=anybody/g' /etc/X11/Xwrapper.config
# Blacklist the vmw module
if [ ! -e /etc/modprobe.d/blacklist-vmw_vsock_vmci_transport.conf ]; then
echo "blacklist vmw_vsock_vmci_transport" > /etc/modprobe.d/blacklist-vmw_vsock_vmci_transport.conf
fi
#Ensure hv_sock gets loaded
if [ ! -e /etc/modules-load.d/hv_sock.conf ]; then
echo "hv_sock" > /etc/modules-load.d/hv_sock.conf
fi
# Configure the policy xrdp session
cat > /etc/polkit-1/localauthority/50-local.d/45-allow-colord.pkla <<EOF
[Allow Colord all Users]
Identity=unix-user:*
Action=org.freedesktop.color-manager.create-device;org.freedesktop.color-manager.create-profile;org.freedesktop.color-manager.delete-device;org.freedesktop.color-manager.delete-profile;org.freedesktop.color-manager.modify-device;org.freedesktop.color-manager.modify-profile
ResultAny=no
ResultInactive=no
ResultActive=yes
EOF
# reconfigure the service
systemctl daemon-reload
systemctl start xrdp
#
# End XRDP
###############################################################################
echo "Install is complete."
echo "Reboot your machine to begin using XRDP."

Start VM (again!)#

Click Connect, then Start

Run the script again#

Yes, after rebooting, you must execute the script again. Many online guides failed to include this step.

NOTE

This step takes a while to complete.

cd linux-vm-tools/ubuntu/20.04
sudo ./install.sh
sudo shutdown -h now

Set Hyper-V Transport Type#

In PowerShell, run the following:

Set-VM -VMName 'ubuntu2004' -EnhancedSessionTransportType HvSocket
IMPORTANT

If you named your VM something else, change the VMName argument accordingly. To see the names of all VMs, use 'Get-VM' command.

Start and connect to the virtual machine. When the VM boots up, you should now receive a prompt for resolution. You have now enabled enhanced session, which allows for copy and paste between host and VM, among other things.

IMPORTANT

If you are unable to obtain an enhanced session the first time, you can temporarily disable secure boot and restart the VM. Make sure to power down your VM before you disable secure boot. Restart your virtual machine. Then you'll experience your first Hype-V enhanced session. Please ensure that secure boot is enabled after this process.

Setup External Network Interface#

You may require access to your Ubuntu VM, which is connected to your local area network. You may need to set up an external virtual network that is bridged with your Hyper-V system's LAN interface ethernet interface card in this situation.

Refer to the screencast below to learn the steps required to construct an external virtual network and link your Ubuntu virtual machine to it.

Create external virtual network in Hyper-V

Configuring Network Interfaces#

Please follow the steps in the following screencast.

Configure Network Interface using NetworkManager cli utilty nmcli#

The command-line utility nmcli is used to manage NetworkManager and report network status. It is a suitable substitute for nm-applet and other graphical clients. In addition to controlling and displaying network device status, nmcli is used to establish, show, modify, remove, activate, and deactivate network connections. For nmcli examples that are ready to be executed, see nmcli-examples(7).

Configure Network Interface using netplan cli utilty#

What is Netplan#

Netplan is a software that allows you to easily configure networking on a Linux system. Simply write a YAML description of the network interfaces needed and what each should be configured to accomplish. Netplan will produce all of the necessary setup for your selected renderer tool based on this description.

How does it work?#

Netplan reads network settings from files named /etc/netplan/*.yaml that have been produced by administrators, installers, cloud image instantiations, or other OS deployments. During early boot, Netplan creates backend-specific configuration files in /run to hand off device control to a specified networking daemon.

Netplan Design Overview

Netplan Design Overview

Image Source

Netplan currently works with these supported renderers

Netplan Main Web Site:

Netplan

Install Rustdesk on Ubuntu VM#

RustDesk is a full-featured open source remote control solution with minimal configuration for self-hosting and security. The open source replacement for TeamViewer and AnyDesk.

Download Rustdesk#

wget https://github.com/rustdesk/rustdesk/releases/download/1.2.2/rustdesk-1.2.2-x86_64.deb
NOTE

The above version is current as of the time this recipe was written. The most recent version can be found at 'https://github.com/rustdesk/rustdesk/releases/'.

Install Rustdisk#

sudo apt install -f ./rustdesk-1.2.2-x86_64.deb

References#

We would like to take this opportunity to recognize the original authors of the following works. We couldn't have done it so easily without your help.

Setup Hyper-V enhanced session for Ubuntu 20

How to install Ubuntu 20.04 on Hyper-V with enhanced session

Ubuntu 20.04 on Hyper-V