Ubuntu (Linux) Desktop using RDP GUI with Windows WSL

This article explains the steps to setup Ubuntu virtual machine with Desktop environment on Windows using Window's Subsystem for Linux (WSL) and be able to connect to Ubuntu using Window's builtin Remote Desktop Protocol (RDP) app.

Enable WSL on Windows:

Enabling the features can be done only using administrator user. In corporate laptop or enterprises, this step is performed by Windows Administrator.

Through GUI:

  1. Start -> Search -> Turn Windows feature on or off
  2. Virtual Machine Platform -> Tick
  3. Windows Hypervisor Platform -> Tick
  4. Window Subsystem for Linux -> Tick
  5. Click OK
  6. Wait for some installations to complete
  7. Restart Windows

Through Terminal:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

dism /online /enable-feature /featurename:HypervisorPlatform /all /norestart

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

Restart-Computer

Install Ubuntu:

Start -> Terminal and execute

wsl.exe --update

wsl.exe --install -d Ubuntu

When prompted 'Create a default Unix user account' give any username like 'ubuntu'

Type a password

Retype the password

Wait for installation to complete

Verify Ubuntu:

wsl.exe --list --verbose

Launch Ubuntu:

wsl.exe -d Ubuntu

or through UI

Start -> Ubuntu

Setup GUI for RDP:

Update packages using commands

sudo apt-get update

sudo apt-get upgrade -y

Install and setup tasksel:

sudo apt-get install -y tasksel

sudo tasksel

Use Up and Down Arrows to choose the Desktop environment. KDE Plasma seems to be the most stable flavour for xrdp remote session and it also supports using multiple monitors out of the box, but feel free to explore other desktop environments like GNOME or XfceSelect only one desktop environment, as selecting multiple causes configuration issues.

Use Space key to tick or untick

Use Tab bar to go to OK button

Press Space key to click OK button

Wait for the tasksel command to complete desktop installation and setup, as this will take a long time to complete.

Install chromium browser:

The default browser firefox doesn't work with xrdp session, hence installing chromium will help in windows RDP session

sudo apt-get install -y chromium

Install xrdp:

sudo apt-get install -y xrdp

Configure and restrict xrdp connection:

Edit the following file to restrict xrdp connection, so that RDP connection is allowed only from Windows Host and the port change to avoid port clash with Window's built in RDP settings

sudo nano /etc/xrdp/xrdp.ini

  1. If address entry is already present, then change the existing address entry, otherwise add an address entry as address=127.0.0.1 
  2. If port entry is already present, then change the existing port entry, otherwise add a port entry as port=3390
  3. Save and exit (Ctrl+O, Enter, Ctrl+X)

sudo nano /etc/xrdp/startwm.sh

Add the following lines before the last line (usually before test -x /etc/X11/Xsession)

unset DBUS_SESSION_BUS_ADDRESS

unset XDG_RUNTIME_DIR

Save and exit (Ctrl+O, Enter, Ctrl+X)

Enable and restart the xrd service:

sudo systemctl enable xrdp

sudo systemctl restart xrdp

Restart Ubuntu virtual machine from a new Windows Terminal:

Terminate single Ubuntu virtual machine

wsl.exe --terminate Ubuntu

or

Shutdown entire wsl instances

wsl.exe --shutdown

Check shutdown status

wsl.exe --list --verbose

Start Ubuntu virtual machine and check xrdp service status

wsl.exe -d Ubuntu

sudo systemctl status xrdp

Connect from Windows using RDP:

Start -> Search -> Remote Desktop Connection

Enter the Computer as localhost:3390 and connect

Select the Session type as Xorg and enter the Ubuntu username and password when prompted

Type in the Ubuntu password once again if prompted

Display Options:

  1. To avoid launching the session in full screen mode, Display Tab -> Start Session in Fullscreen -> Untick
  2. To use multiple monitors / dual screen, Display Tab -> Use all monitors -> Tick

Changing the memory allocation to Ubuntu

By default Windows WSL allocated 50% of total ram to Ubuntu Virtual Machine. If we want to increase of decrease the memory or processor allocation, we need to create and set the values in %UserProfile%\.wslconfig file in window's user profile directory as explained in the advanced configuration page.

If Ubuntu is used for heavy development activity, then we can allocate up to 85% of total memory from Windows, so that the Windows can remain stable for non-development activities.


To create or edit the file, execute the following command in windows power shell

notepad "$env:USERPROFILE\.wslconfig"

Add the following content and save the file

[wsl2]
# Sets the maximum RAM WSL2 can use.
memory=12GB

Shutdown Ubuntu

wsl.exe --terminate Ubuntu

Shutdown WSL service

wsl.exe --shutdown

Start Ubuntu again and it should now take the values from the .wslconfig file

wsl.exe -d Ubuntu

Changing password of Ubuntu user:

The following command can be used to change the Ubuntu virtual machine's password

passwd

Clean up of WSL and distributions:

Delete a particular virtual machine:

wsl.exe --unregister Ubuntu

Delete all virtual machines:

wsl.exe --list --quiet | ForEach-Object { wsl --unregister $_ }

Uninstall wsl application itself:

wsl.exe --uninstall

Directory Sharing between Windows and Ubuntu:

Full Sharing (Default):

By default all Windows (Host) directory is available as read/write directory under /mnt directory of Ubuntu.

For example the C:\ of windows can be accessed in Ubuntu Files explorer using command

open /mnt/c

Making Windows Directory as Read Only:

To make the Windows (Host) directory as read only, add the following lines to /etc/wsl.conf using 

sudo nano /etc/wsl.conf

[automount]
enabled = true
options = "ro"

Save and exit (Ctrl+O, Enter and Ctrl+X)
Restart Ubuntu using wsl.exe --terminate Ubuntu

No Sharing (Complete Isolation):

To completely isolate Windows (Host) directory from Ubuntu, add the following lines /etc/wsl.conf using 

sudo nano /etc/wsl.conf

[automount]
enabled = false

Save and exit (Ctrl+O, Enter and Ctrl+X)
Restart Ubuntu using wsl.exe --terminate Ubuntu

Sharing only single directory:

Create a directory in Windows C:\ Drive for sharing with Ubuntu. Execute the command in Windows PowerShell

New-Item -Path "C:\windows_shared" -ItemType Directory -Force

To share only a single directory from Windows (host) to Ubuntu, add the following lines /etc/wsl.conf using 

sudo nano /etc/wsl.conf

[automount]
enabled = false

Save and exit (Ctrl+O, Enter and Ctrl+X)

Create a directory under /mnt in Ubuntu Terminal for shared directory and change the file permission to be permissible

sudo mkdir -p /mnt/windows_shared

chmod -R a+rwx /mnt/windows_shared

Add the following content using 

sudo nano /etc/fstab

C:\windows_shared /mnt/windows_shared drvfs defaults 0 0

Save and exit (Ctrl+O, Enter and Ctrl+X)
Restart Ubuntu using wsl.exe --terminate Ubuntu

Using WSL version 1 (Legacy):

If you get the following error

WSL2 is not supported with your current machine configuration.

Please enable the "Virtual Machine Platform" optional component and ensure virtualisation is enabled in the BIOS.

Enable "Virtual Machine Platform" by running: wsl.exe --install --no-distribution

For information please visit https://aka.ms/enablevirtualization

Error code: Wsl/InstallDistro/Service/RegisterDistro/CreateVm/HCS/HCS_E_HYPERV_NOT_INSTALLED

Try using legacy version 1 of wsl

wsl.exe --set-default-version 1

wsl.exe --install -d Ubuntu

Comments

Popular posts from this blog

Export/Backup Ollama Model

JSON with curl and jq

Import self signed in Linux for Chrome / Chromium headless testing