This guide will walk you through how to set up X-Forwarding for Windows Subsystem for Linux version 2. This guide has been tested on Ubuntu 20.04 LTS running on WSL 2. If you have WSL 1, follow the WSL 1 guide instead! If you aren’t sure which version of WSL you have, or if you do not have WSL installed, read through the WSL Intro.
You will need to make a small configuration change to your installation of Ubuntu before x-forwarding will work. Perform the following step on the bash prompt in WSL.
Running the following command will make it so Ubuntu knows how to open a connection to VcXsrv
, the program that will allow you to see windows opened on WSL.
echo "export DISPLAY=\$(awk '/nameserver / {print \$2; exit}' /etc/resolv.conf 2>/dev/null):0" >> ~/.bashrc && echo "export LIBGL_ALWAYS_INDIRECT=1" >> ~/.bashrc
After running this command, you will need to reload the .bashrc
file like so:
. ~/.bashrc
This step will set up your Windows installation to display GUIs from WSL 2. Perform the following steps on Windows, not in WSL.
This program will act as a virtual display for WSL to show all of its graphics on. In short, it needs to be running for you to see any GUIs from WSL.
You can download VcXsrv
from sourceforge. Once VcXsrv
is installed, the program used to launch it will be called XLaunch
, which you should be able to find from your Start menu search bar.
When you run VcXsrv
using XLaunch
, you will see a few options you can configure. Leave everything at default settings and click Next
until the Extra Settings
page. At the bottom of this page is a textbox labelled Additional parameters for VcXsrv
. Enter -ac
in this box, and click through the rest of the prompts. You need to do this every time you start VcXsrv
.
You will be asked to allow VcXsrv
access to public and private networks. Check only the box for private networks.
By default, Windows 10 won’t allow any connections to come from WSL 2. This means graphical apps in WSL won’t work. We need to add and change some firewall rules to fix this.
First, open your firewall settings. To do so, open Windows Settings
, navigate to Firewall and Network Protection
, then click Advanced Settings
. You should see a window titled Windows Defender Firewall with Advanced Security
. It’s a catchy name, right?
Now we need to add an inbound rule. Right-click Inbound Rules
on the left sidebar, and select New Rule
. Set the rule type to Port
, the protocol to TCP
, and have it apply to Specific local ports
, which you should specify as 6000
. Choose Allow the connection
, leave the Domain
, Private
, and Public
boxes checked, choose a name you’ll remember (seriously remember it), and click Finish
.
Now that we’ve added an inbound rule, we need to go and modify it, because as it stands, it leaves your machine slightly more susceptible to security threats. Click Inbound Rules
on the left sidebar, find an entry with Name
being whatever you named your new TCP
rule to earlier, right-click it, choose Properties
, and click on the Scope
tab. Under Remote IP address
click These IP addresses
, then Add the address 172.16.0.0/12
to the list and click OK
. Sorting the entries by Name
makes it easier to find the right rule.
Finally, we need to modify an existing inbound rule. Click Inbound Rules
on the left sidebar, make the window wide enough so you can see the Protocol
column of each entry, and sort the entries by Name
by clicking the heading of the Name
column. Now find an entry with Name
being VcXsrv windows xserver
, Profile
being Public
, and Protocol
being TCP
. Right-click it, choose Properties
, and under Action
, choose Allow the connection
, and click OK
.
To check if x-forwarding is working, launch VcXsrv
(don’t forget the -ac
option), open WSL, and run the gtkwave
command. If everything has gone as planned, you will see a window open.