Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set up working X11 forwarding on WSL2 [closed]

People also ask

Does X11 support WSL?

Windows Subsystem for Linux (WSL) now supports running Linux GUI applications (X11 and Wayland) on Windows in a fully integrated desktop experience.

How do I run xwindows on WSL?

To start using WSL, open up a PowerShell terminal and type wsl . If you've set up WSL correctly, you'll enter a bash terminal running on the WSL distro of choice. From here, you can run any Linux commands you wish. Below you will find a reference to all of the options the wsl.exe provides when starting up.


TL;DR:

Add the following to your ~/.bashrc:

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=1

Enable Public Access on your X11 server for Windows.*

Add a separate inbound rule for TCP port 6000 to the windows firewall in order to allow WSL access to the X server, as described by the wsl-windows-toolbar-launcher people.


As pointed out by WSL_subreddit_mod on reddit and as you can read in Microsoft's documentation on WSL2, the WSL2 architecture uses virtualized network components. This means that WSL2 has a different IP address than the host machine. This explains why the X11 forwarding settings of WSL1 cannot simply be transferred to WSL2.

On the Ubuntu Wiki page about WSL you can already find a configuration adapted for WSL2 under Running Graphical Applications. A similar configuration is also suggested by the above mentioned Reddit User, who also contributes another part of the solution: Enable Public Access on the X11 server under Windows.

This means add the following to your ~/.bashrc:

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=1

And Enable Public Access on your X11 server for Windows.*

The most important part to enable X11 forwarding for WSL2 on Windows 10 is still missing: the Windows firewall blocks connections via the network interface configured for WSL by default.
A separate inbound rule for TCP port 6000 is required to allow WSL access to the X server. After the rule has been created, as described by the wsl-windows-toolbar-launcher people, the IP address range can be restricted to the WSL subnet in the settings of the newly created rule, under Scope: 172.16.0.0/12.

*: If you use VcXSrv you can enable public access for your X server by disabling Access Control on the Extra Settings:
Disable access control VcXSrv
Or by calling vcxsrv.exe directly with the ac flag: vcxsrv.exe -ac as pointed out by ameeno on the github issue.

Alternatively this SO answer shows how to share keys via .Xauthority files, leaving you with intact access control.


For some people who allowed only for private networks like me,

although they Should have been Both Ticked

enter image description here

It should have stop signs on Windows Defender firewall

enter image description here

Double click it and allow the connection for both private and public,

enter image description here

So all the 4 items should be ticked green.

Then the above answer from @NicolasBrauer was working for me.

Like disabling the access control when you XLaunch and

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
export LIBGL_ALWAYS_INDIRECT=1

Using /etc/resolv.conf nameserver won't work for me since I disabled resolv.conf generation in /etc/wsl.conf (I have a custom resolv.conf).

Ultimately you want the WSL2 host IP address, which should also be your default route. Here's my ~/.bashrc entry for my Debian WSL2 distro:

export DISPLAY=$(ip route | awk '/^default/{print $3; exit}'):0

I come up with a solution using vxcsrv on windows 10, as others pointed out.

XServer Windows - WSL1 & WSL2:

Install X-Server Windows

https://sourceforge.net/projects/vcxsrv/

Set Display forward in WSL Distro

Configure Display:

  • If you running WSL1:
export LIBGL_ALWAYS_INDIRECT=1
export DISPLAY=localhost:0
  • If you running WSL2:
export LIBGL_ALWAYS_INDIRECT=1
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
  • (If you have disabled resolv.conf use this definition: https://stackoverflow.com/a/63092879/11473934)

and then (install x11-apps):

sudo apt update
sudo apt install x11-apps

Start XLaunch on Windows

  • Multiple Windows
  • Start no client
  • disable Native opengl
  • enable Disable access control

Test it

In wsl: enter xcalc - Calculator should open in Windows10

If everything worked

And you want to persist the settings in your wsl distro. Store them in your ~/.bashrc.

sudo nano ~/.bashrc

Copy the two lines (from Set Display forward in WSL Distro - Configure Display), two the end and save it.

Add it to autostart

  1. Run Dialog see Start XLaunch on Windows
  2. Save configuration
  3. Press Windows + R
  4. Enter: shell:startup
  5. Copy saved configuration: *.launch (Generated in step 2) to this folder (step 4)

Now the XServer will be started with windows startup.

I’m using it for ROS. Works for me.

My XServer isn’t available over internet so its okay to disable access control.