Deploy a Linux VM on Hyper-V with Sound 20.04 Edition

In a previous article I explained how to use PulseAudio with XRDP to allow your Ubuntu Hyper-V VM to pass audio through your host machine’s sound system. It has been brought to my attention that the instructions do not work for everyone.

I believe most of the people that have issues are choosing the 20.04 version of Ubuntu when deploying their Hyper-V VM. The 20.04 edition of Ubuntu is not running XRDP (Linux RDP Server) by default. When you connect to it from the Hyper-V manager you are directly viewing the machine’s console. PulseAudio requires an enhanced XRDP session to work.

I’ve spent a lot of time getting pass-through sound working in 20.04. Deploy the VM as outlined in the original document choosing the 20.04 version instead. Once you have completed the operating system installation, run the software updater (in the apps menu) until everything is patched to the newest version.

Please Note: These instructions will not work if your VM is configured for auto logon. PulseAudio runs in the user space and requires active logon to work. It would be possible to convert the daemon to a service, but that is beyond the scope of this post.

Use Hyper-V quick create to deploy a VM

Once Ubuntu is installed and up to date we’ll need to manually configure it for an enhanced session. Open a terminal and run this command to download the configuration script:

wget https://raw.githubusercontent.com/Hinara/linux-vm-tools/ubuntu20-04/ubuntu/20.04/install.sh 

Once the download finishes run:

sudo chmod +x install.sh

followed by:

sudo ./install.sh


to install and configure XRDP.

You will be prompted to logon to your VM with XRDP, but it will not work until you reboot. Use the button in the tool bar to return to the basic session and power off the VM.

The highlighted button will return you to a basic session so you can shutdown the VM.

Open PowerShell on your host system and run the command below, where the name of your particular VM is between the quote marks.

Set-VM -VMName "Ubuntu 20.04" -EnhancedSessionTransportType HvSocket 

Power on the VM and you will be prompted by the RDP client to select your monitor size. Logon to XRDP with your Linux credentials and the desktop will load. Now that we have installed and configured XRDP we can proceed to install and configure PulseAudio.

Occasionally you may stop being prompted to change your monitor size, etc. by the RDP client when you connect to your VM. There is a check box that can accidently be selected that stores your current settings for all future connections. To get the controls back, right click on your VM in the Hyper-V manager and then click Edit Session Settings.

Open a terminal.

Install the pre-requisite libraries with this command:

sudo apt-get install git libpulse-dev autoconf m4 intltool build-essential dpkg-dev libtool libsndfile-dev libspeexdsp-dev libudev-dev -y

Next we need to enable the “source” repository so that we can download PulseAudio and compile it. You can enable the “source” repo in the GUI:

Enable the Source-code repo in the GUI.

Or run:

sudo cp /etc/apt/sources.list /etc/apt/sources.list~
sudo sed -Ei 's/^# deb-src /deb-src /' /etc/apt/sources.list
sudo apt-get update

Now we need to get the source code for pulse audio and compile it. Run the following command groups one after the other. Each will take some time to complete.

sudo apt build-dep pulseaudio -y
cd /tmp
sudo apt source pulseaudio
pulsever=$(pulseaudio --version | awk '{print $2}')
cd /tmp/pulseaudio-$pulsever
sudo ./configure
sudo git clone https://github.com/neutrinolabs/pulseaudio-module-xrdp.git
cd pulseaudio-module-xrdp
sudo ./bootstrap
sudo ./configure PULSE_DIR="/tmp/pulseaudio-$pulsever"
sudo make
cd /tmp/pulseaudio-$pulsever/pulseaudio-module-xrdp/src/.libs
sudo install -t "/var/lib/xrdp-pulseaudio-installer" -D -m 644 *.so

Power off your VM and wait until the snapshot merge in Hyper-V completes then power it back on. When you connect to it this time, use the “Show Options” down arrow and then click the “Local Resources” tab. Click the settings button and then choose the option to play back audio on this device.

Now open the setting applet on your Ubuntu VM and go to sound. Use the drop down to select xrdp-sink as the audio device. Sometimes the option will be missing. Generally, if you wait some random amount of time the daemon will start on its own, I’m not sure what causes the inconsistency.

If the option never appears or you get tired of waiting. Close the Settings applet and open a terminal. Run: pulseaudio -k to start the daemon yourself. Then use settings to adjust the output device again.

About Kevin Trent

IT professional with almost 30 years of experience in Infrastructure, Architecting, Administration, Development, and Communications.

3 Responses

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s