Run Ubuntu WSL (Windows Subsystem for Linux) with a desktop environment on Windows 10 with a single click.
With the right setup it is possible to use Ubuntu WSL (= Windows Subsystem for Linux) on Windows not just using the terminal, but also a desktop environment. All this with only a single click (or double click ;)).
Used versions:
Sounds good? So let’s get started…
Steps to perform:







Reboot Windows.




# Install the (lightweight) XFCE desktop environment. sudo apt -y install xfce4
# Install remote desktop (RDP) server. sudo apt-get install xrdp
# Configure the RDP server. sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak sudo sed -i ‘s/3389/3390/g’ /etc/xrdp/xrdp.ini sudo sed -i ‘s/max_bpp=32/#max_bpp=32\nmax_bpp=128/g’ /etc/xrdp/xrdp.ini sudo sed -i ‘s/xserverbpp=24/#xserverbpp=24\nxserverbpp=128/g’ /etc/xrdp/xrdp.ini
# Start the xrdp server (manually). sudo /etc/init.d/xrdp start
* Adapt xrdp configuration file
``` bash
sudo sed -i 's%\(test -x /etc/X11/Xsession && exec /etc/X11/Xsession\)%#\1%g' /etc/xrdp/startwm.sh
sudo sed -i 's%\(exec /bin/sh /etc/X11/Xsession\)%#\1%g' /etc/xrdp/startwm.sh
echo -e "\n# xfce\nstartxfce4" | sudo tee -a /etc/xrdp/startwm.sh > /dev/null



* Now you should see the full screen XFCE desktop environment.This seems to be the most tricky part of this whole setup. Fortunately it is possible to execute commands on Ubuntu using the Windows command line:
wsl.exe <command>
Or for short:
wsl <command>
Keeping in mind that our goal is to use just use a single click (or execution of a single command) to start Ubuntu with a desktop environment, we face following challenges:
wsl.exe on the Windows command or execute the program directly from the filesystem). However, when executing Ubuntu, it opens a terminal window, which we don’t want.systemd / /etc/init.d) implemented, we need to start the remote desktop server manually.So how to start Ubuntu without leaving a command line opened?
At this point it’s important to understand the mechanism.
Since running wsl.exe is the only way to boot the system and this command opens a terminal window, we need to close this window automatically.
Therefore, we need an exit command in the ~/.bashrc, which has the downside that if we open an Ubuntu terminal later on, it will close immediately.
To solve this, the most straight forward solution seems to be using a flag. This flag can be implemented using a file within Ubuntu, as it can be accessed by Windows and Ubuntu. The existence if this file means that the flag is set, otherwise it’s not set.
When running our start script on Windows (see below), this flag needs to be set using wsl.exe touch /tmp/wsl_flag and after starting Ubuntu, the flag needs to be reset using rm /tmp/wsl_flag.
So how to get the remote desktop server (xrdp) started when there’s no startup system on the Ubuntu WSL?
To solve this problem we just hook the solution of the first problem and start the server on the first login (i.e. after booting Ubuntu) using sudo /etc/init.d/xrdp start.
Did you recognize the sudo? This is necessary if you want to start a service as a regular user. But to use it, there need’s to be an appropriate entry in the sudoers config.
We’ll make use of the /etc/sudoers.d/ system, which means that we create a new file called xrdp in this folder, which gets analysed automatically by the sudoers system.
This approach is more flexible, since there’s no more need to modify the /etc/sudoers file.
To create this sudoers entry, run
sudo vi /etc/sudoers.d/xrdp
Then paste the following config text in it and replace <USER> with your Ubuntu username.:
# Allow xrdp server to work with sudo
<USER> ALL=(ALL) NOPASSWD: /etc/init.d/xrdp
Save the file and then change the file mode as follows:
sudo chmod 440 /etc/sudoers.d/xrdp
Attention:
. or ~ in it. Otherwise, it will not be scanned from the sudoers system.wsl -u root rm /etc/sudoers.d/xrdp
or if you need somewhat more control, use:
wsl -u root bash
and execute all necessary commands.
To perform all these action at once, we need some scripts. Note, that some commands below may run asynchronously.
Create a new file which might be called Start Ubuntu 20.04 LTS WSL VM.bat and paste this code in it:
@echo "Starting Ubuntu 20.04 LTS WSL VM..."
@echo off
wsl touch /tmp/wsl_flag
wsl
start /B mstsc /f /v:localhost:3390
Executing this script perform these actions:
To prevent the terminal window shows up until the system has started, just create a shortcut to the batch script and in the settings go to “Run” and select “Minimized”.
Add the following code block to the very end of ~/.bashrc:
# Exit from login if just used to start WSL
flag="/tmp/wsl_flag"
if [ -f "${flag}" ] ; then
rm "${flag}"
# Start xrdp server
sudo /etc/init.d/xrdp start
# Wait for service is running
while ! service --status-all 2>&1 | grep xrdp | grep + >/dev/null ; do
sleep 1
done
echo "Cancelling from login"
exit
fi
When the user logs in, this code checks if the flag is set.
On clicking the script Start Ubuntu 20.04 LTS WSL VM.bat on Windows, you should see the Ubuntu terminal opening, printing some output on it and closing automatically. Then the remote desktop client starts and connects to the server. You should now see the XFCE desktop from Ubuntu in full screen.
Congratulations!
wsl -hwsl.exe --shutdownwsl --list and find the name of the one you want to back upwsl --export <IMAGE_NAME> <EXPORT_FILENAME.tar>wsl --import <IMAGE_NAME> <INSTALL_LOCATION> <PATH\TO\EXPORT_FILENAME.tar>After installing the necessary software, the whole mechanism comes down to: