39

I installed WSL2 and I want to access WSL2 GUI apps from Windows 10. For this, I installed Xming and start it in "No client" mode. Xming startes, ok. After I open the WSL2 terminal and there execute these commands

export DISPLAY=:0

And after

xeyes

And get error

Error: Can't open display: :0

X11Forwarding checked, yes.

4 Answers 4

47

Solved problem with executing this command

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0

This command can also be added in shell initialization files, for example, .bashrc, .zshrc etc.

Also make sure to allow public access of X server running in Windows 10. For VcXsrv, -ac option is most important because it allows public access. For Xming, run through XLaunch and in the part "Specify parameter settings" select "No Access Control".

For more see this discussion Microsoft/WSL: Can't use X-Server in WSL2.

5
  • 5
    For anybody out there that isn't sure what adding the -ac option means, and can't get to the link in the answer, when you run your X server, there should be a step near the end where you can add Additional parameters for VcXsrv, you type the -ac in there. That solved my problem after the export provided above. Aug 29, 2019 at 5:28
  • Thanks for the one-liner! Have to modify this if your "nameserver" is not the same as your host. About WSL2 - Your WSL2 instance uses a separate network stack, so you need a way to discover what you're host IP is from within your WSL2 instance.
    – Lon Kaut
    May 26, 2020 at 16:28
  • export DISPLAY=$(ip route get 0.0.0.0 | awk '{print $NF}'):0 can be used to export your display to the interface IP responsible for your route to the internet (WSL1 only).
    – Lon Kaut
    May 26, 2020 at 16:45
  • 2
    I confirm export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0 worked for WSL2 with Windows10x64 May 2004.
    – K F
    May 30, 2020 at 1:15
  • 1
    Holy crap, this worked after sifting through endless stack overflow posts to figure out why the stupid gui wasn't working Jan 20, 2021 at 17:59
13

I encountered this issue after changing the version of my WSL Ubuntu distribution from 1 to 2. Using

export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0

was not sufficient. According to https://github.com/microsoft/WSL/issues/6181 I needed to reconfigure the firewall. This can be done by

  • going to Advanced Security -> Inbound rules (admin permissions required)
  • delete all rules for Vcxsvr
  • restarting Vcxsvr, which prompts you to set Windows Firewall for this program
  • allow all (or enough) access

After these 2 changes, the problem was solved.

2
  • I was able to enable the existing rules for the same effect.
    – slim
    Jan 24, 2021 at 17:47
  • This is important!
    – JKB
    Mar 23, 2021 at 21:50
3

Follow these steps:

  1. Open PowerShell as Administrator and run: New-NetFirewallRule -DisplayName "WSL" -Direction Inbound -InterfaceAlias "vEthernet (WSL)" -Action Allow

  2. Now open WSL2 and run: export DISPLAY=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2; exit;}'):0

  3. Now xeyes

0

For coLinux using eth0=slirp with the default configuration, Xming on the Windows host can be accessed using export DISPLAY=10.0.2.2:0 (or 0.0). Using biswapriyo's solution would not work in this case because slirp sets the resolver IP to 10.0.2.3, but his answer made me realize what I was doing wrong.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .