DEV Community

klo2k
klo2k

Posted on

Create Ubuntu 24.04.2 VM in Hyper-V, with "Enhanced Session" RDP support (Windows 11, xrdp, development)

To setup an Ubuntu 24.04.2 development Hyper-V VM, with built-in "Enhanced Session" + Remote Desktop Protocol (RDP) support (until it's available in Hyper-V's "Quck Create"):

  1. Create Hyper-V VM
  2. Configure VM for "Enhanced Session", allow nested virtualisation
  3. Start VM, install Ubuntu
  4. XRDP Setup
  5. Optional, but really useful setup
  6. Fix Xrdp slow performance
  7. Connect to VM

Create Hyper-V VM

Create VM ("New" > "Virtual Machine"):

# Specify Name and Location
Name: Ubuntu 24.04
[Next]

# Specify Generation
Generation: 2
[Next]

# Assign Memory
Startup memory: 4096
[Next]

# Configure Networking
Connection: Default Switch
[Next]

# Connect Virtual Hard Disk
Create a virtual hard disk:
  Name: Ubuntu 24.04.vhdx
  Size: 256GB
[Next]

# Installation Options
Install an operating system from a from bootable CD/DVD-ROM:
  Image file: ubuntu-24.04.2-desktop-amd64.iso

[Finish]
Enter fullscreen mode Exit fullscreen mode

DO NOT START VM YET

Configure VM (Right-click "Ubuntu 24.04" > "Settings..."):

# Change boot order (boot from HDD before Network)
Firmware
  DVD Drive
  HDD
  Network

# Necessary to boot Ubuntu
Security
  Enable Secure Boot
  Template: Microsoft UEFI Certificate Authority

# Optional
Integration Services
  (Check all)

# Optional
Checkpoints
  (Uncheck) Use automatic checkpoints

# Optional
Automatic Stop Action
  Shut down the guest operating system

[OK]
Enter fullscreen mode Exit fullscreen mode

DO NOT START VM YET

Configure VM for "Enhanced Session", allow nested virtualisation

From admin PowerShell (Right-click "Windows PowerShell" > "Run as administrator..."):

# Connect with Enhanced Session
Set-VM -VMName 'Ubuntu 24.04' -EnhancedSessionTransportType HvSocket

# Allow nested virtualisation (optional)
Set-VMProcessor -VMName 'Ubuntu 24.04' -ExposeVirtualizationExtensions $true
# Verify - expect 'True'
(Get-VMProcessor -VMName 'Ubuntu 24.04').ExposeVirtualizationExtensions
Enter fullscreen mode Exit fullscreen mode

Start VM, install Ubuntu

  1. Start VM, install with default options, using the latest installer (Update installer if promoted).

    Note:

    1. ❗ Make sure Require my password to log in is checked (this is needed for xrdp)
    2. Example Default installation options:
      Interactive installation
      Default selection
      Erase disk and install Ubuntu
    
  2. Restart - Hard-reset VM if you get error: SQUASHFS error: Failed to read block

XRDP Setup

Install Xrdp + apply Ubuntu 24.04 specific fixes:

💡 You may find it easier to do this over SSH: apt install -y openssh-server, then ssh to new VM.

# As root
sudo su -

# Hyper-V integration services
apt install -y linux-tools-virtual-hwe-24.04 linux-cloud-tools-virtual-hwe-24.04
# Fix-up journal log error
mkdir /usr/libexec/hypervkvpd/
ln -s /usr/sbin/hv_get_dhcp_info /usr/libexec/hypervkvpd/hv_get_dhcp_info
ln -s /usr/sbin/hv_get_dns_info /usr/libexec/hypervkvpd/hv_get_dns_info

# Install xrdp
apt install -y xrdp
cp -p /etc/xrdp/sesman.ini /etc/xrdp/sesman.ini.original
cp -p /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.original

# Allow enhanced session
sed -i -e 's/^port=3389$/port=3389 vsock:\/\/-1:3389/g' /etc/xrdp/xrdp.ini

# Rename redirected drives to 'shared-drives'
sed -i -e 's/FuseMountName=thinclient_drives/FuseMountName=shared-drives/g' /etc/xrdp/sesman.ini


# Use "Ubuntu" session
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
# use the script to setup the ubuntu session
sed -i -e 's/startwm/startubuntu/g' /etc/xrdp/sesman.ini

# Fixes login black screen delay
echo "blacklist vmw_vsock_vmci_transport" > /etc/modprobe.d/blacklist-vmw_vsock_vmci_transport.conf

# Unlock keyring on login
cat > /etc/pam.d/xrdp-sesman <<'EOT'
#%PAM-1.0
auth     required  pam_env.so readenv=1
auth     required  pam_env.so readenv=1 envfile=/etc/default/locale
@include common-auth
-auth    optional  pam_gnome_keyring.so
-auth    optional  pam_kwallet5.so

@include common-account

@include common-password

# Ensure resource limits are applied
session    required     pam_limits.so
# Set the loginuid process attribute.
session    required     pam_loginuid.so
# Update wtmp/lastlog
session    optional     pam_lastlog.so quiet
@include common-session
-session optional  pam_gnome_keyring.so auto_start
-session optional  pam_kwallet5.so auto_start
EOT
Enter fullscreen mode Exit fullscreen mode

Optional, but really useful setup

Reduce boot delay:

cp -p /etc/default/grub /etc/default/grub.default
# Show boot log instead of splash screen
sed -i -e 's/GRUB_CMDLINE_LINUX_DEFAULT=.*/GRUB_CMDLINE_LINUX_DEFAULT=""/g' /etc/default/grub
# Faster boot - reduce grub wait for input timeout
echo 'GRUB_RECORDFAIL_TIMEOUT=3' >> /etc/default/grub
update-grub
Enter fullscreen mode Exit fullscreen mode

Enable zram (compressed memory), disable swap file:

apt install -y systemd-zram-generator
cp -p /etc/systemd/zram-generator.conf /etc/systemd/zram-generator.conf.original
cat > /etc/systemd/zram-generator.conf <<'EOT'
[zram0]
zram-size = 12288
compression-algorithm = zstd
EOT
systemctl daemon-reload
systemctl restart systemd-zram-setup@zram0

# Disable swap file
cp -p /etc/fstab /etc/fstab.original
sed -i -e 's@^/swap.img@#/swap.img@g' /etc/fstab
swapoff /swap.img
rm /swap.img
Enter fullscreen mode Exit fullscreen mode

Full reboot (must power off > power on - else Enhanced Session won't kick-in)

poweroff
Enter fullscreen mode Exit fullscreen mode

Fix Xrdp slow performance

In Feb 2025 one of the Ubuntu update caused a performance regression with the Xorg backend (even on 1920x1080...).

To switch to the VNC Backend:

# As root
sudo su -

# Install TigerVNC
apt install -y tigervnc-standalone-server tigervnc-xorg-extension

# Configure tigervnc backend
cp -p /etc/xrdp/sesman.ini /etc/xrdp/sesman.ini.20250213
sed -z -i -e \
's/'\
'\(\[Xvnc\].*\nparam=96\n\)\n\[/'\
'\1param=-CompareFB\nparam=1\nparam=-ZlibLevel\nparam=0\nparam=-geometry\nparam=1920x1080\n\n[/'\
'gi' /etc/xrdp/sesman.ini

# Fix "VNC server resize failed - error code 3" login error
cp -p /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.20250213
sed -i -e 's/^#delay_ms=2000$/delay_ms=2000/gi' /etc/xrdp/xrdp.ini

# Remove Xorg backend
sed -z -i -e \
's/'\
'\[Xorg\].*\ncode=20\n\n\[/'\
'[/'\
'gi' /etc/xrdp/xrdp.ini

reboot
Enter fullscreen mode Exit fullscreen mode

Connect to VM

Either using mstsc / Hyper-V

Profit 😉

Credits

Top comments (0)