This guide covers how to install the XFCE4 desktop environment on WSL2 (Ubuntu) and connect to it using Windows Remote Desktop.
It specifically addresses two common issues on WSL2:
- Black Screen: Caused by incorrect color depth settings.
- Session Closing Immediately: Caused by conflicts between XRDP and WSLg environment variables (
DBUS/XDG).
Open your WSL2 terminal and run the following to install XRDP and the XFCE4 environment.
# Update repositories
sudo apt update && sudo apt -y upgrade
# Install XRDP
sudo apt -y install xrdp
# Install XFCE4 and recommended utilities (goodies)
# Note: 'goodies' is recommended for a better experience (terminal, icons, etc.)
sudo apt -y install xfce4 xfce4-goodies
We need to change the default RDP port (to avoid conflict with Windows) and ensure the color depth is compatible.
# Backup the original configuration
sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak
# Change port from 3389 to 3390
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
# Ensure max_bpp is set to 32 (Setting this to 128 causes a black screen)
sudo sed -i 's/max_bpp=128/max_bpp=32/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/xserverbpp=128/xserverbpp=32/g' /etc/xrdp/xrdp.ini
Tell the system to use XFCE4 when a remote session starts.
echo "startxfce4" > ~/.xsession
By default, WSL2 passes Windows environment variables to Linux. This confuses XFCE when running via XRDP, causing the RDP window to close immediately after login.
We must edit the startup script to unset these variables.
- Open the startup script:
sudo nano /etc/xrdp/startwm.sh
- Replace the entire content of the file with the following script:
#!/bin/sh
# Load essential paths
if [ -r /etc/profile ]; then . /etc/profile; fi
# UNSET WSLg VARIABLES
# These lines prevent conflicts with the Windows display system
unset DBUS_SESSION_BUS_ADDRESS
unset XDG_RUNTIME_DIR
# Start XFCE
startxfce4
- Save and exit (
Ctrl+O,Enter,Ctrl+X).
Restart the service to apply changes:
sudo service xrdp restart
- Open Remote Desktop Connection on Windows.
- Computer:
localhost:3390 - Enter your WSL2 username and password.
If you are stuck at a login loop or the window still closes:
- Clear old session locks:
rm ~/.Xauthority
rm ~/.xsession-errors
- Restart the service again:
sudo service xrdp restart