Random ramblings, mostly technical

Wiktor writes about stuff.

X-forwarding with WSL2 on Windows 10


Windows Subsystem for Linux becomes more and more popular recently, bridging the tooling gap for many developers. From Build 22000 of Windows 11, it is possible to achieve a near-native experience running Linux GUI applications. However, not everyone has access to the latest updates. Those less fortunate need to fall back to running applications via XServer running on Windows.

There are a few X Server solutions available. I have tried VcXsrv and MobaXterm. They both run fine and are pretty straightforward to set up. MobaXterm in addition provides an awesome environment for managing remote connections (SSH, VNC, RDP, etc.).

WSL2 differs in architecture from WSL1. It now runs a full Linux kernel in a lightweight Hyper-V virtual machine. NAT is utilized instead of bridging with the host interface. It means that simply setting export DISPLAY=:0.0, which worked in WSL1, is now insufficient. We need to configure the DISPLAY variable with the actual host address. We can retrieve it directly calling netsh.exe and parsing its output:

export DISPLAY="$(/mnt/c/Windows/system32/netsh.exe \
interface ip show address "INTERFACE_NAME" | \
awk '/IP Address/ {print $3}'|sed 's/\r//'):0.0”

where INTERFACE_NAME is the host interface the X Server is bound to.

After setting the DISPLAY variable, we start the application of our choice. Windows Firewall monit may pop up (depending on your configuration). After confirming the exception we should see the Linux GUI application running.