microsoft/WSL

WSL 2 Error: Can't open display: localhost:0.0

Paul-Toth opened this issue ยท 39 comments

Environment

Windows build number 10.0.19041.746
Ubuntu 20.04
Linux version 4.19.128-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Tue Jun 23 12:58:10 UTC 2020  

Steps to reproduce

The error can be reproduced by running any X server related command. I have been using xeyes for troubleshooting

xeyes strace output

Expected behavior

A pair of eyes should open outside of the terminal which follows my mouse. (All other X server programs have a similar error)

Actual behavior

Nothing happens and I get the following error in my terminal:
Error: Can't open display: localhost:0.0

Additional Details

I have tried running Xming and XLaunch/VcXsrv using the -ac flag. I have also attempted to use the X410 X server with the same result.

VcXsrv is system tray: DESKTOP-LN1HSHQ:0.0 - 0 clients

In /etc/ssh/sshd_config I have set X11Forwarding yes X11UseLocalhost yes

I have tried setting DISPLAY to
localhost:0.0
:0.0
127.0.0.1:0.0
$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0 Which is my address from /etc/resolv.conf

As an additional note, setting DISPLAY=$(cat /etc/re....):0 results in an ~30 second delay before the error occurs. All other DISPLAY settings produce the error instantly.

Disabling Windows Defender Firewall had no effect.

I'm was having the same issue using WSL and Ubuntu 20.04

I found the answer here

https://stackoverflow.com/questions/61860208/wsl-2-run-graphical-linux-desktop-applications-from-windows-10-bash-shell-erro

Your DISPLAY env variable should be set as export DISPLAY=X.X.X.X:0 to use the Windows host's IP address as WSL2 and the Windows host are not in the same network device, where X.X.X.X is the IP address

and your IP address is listed in resolv.conf against the nameserver ($ cat /etc/resolv.conf)

or simply export DISPLAY="grep nameserver /etc/resolv.conf | sed 's/nameserver //':0" to load the correct IP address automatically. Additionally, you can add this to .bashrc or .zshrc (If you use Zsh)

I changed my display to this as you suggested:

export DISPLAY="grep nameserver /etc/resolv.conf | sed 's/nameserver //':0"

This didn't solve this issue... BUT in that link you posted, I looked at some of the solutions people were offering. I decided to check VcXsrv's firewall permissions and discovered it never even created a profile for itself when I installed it.

I went to Control Panel > System and Security > Windows Defender Firewall > Advanced Settings > Inbound Rules > New Rule...
> Program > %ProgramFiles%\VcXsrv\vcxsrv.exe > Allow the connection > checked Domain/Private/Public > Named and Confirmed Rule.

And it worked!!! I've been working on this for 3 days, I don't know how it has taken this long to find the solution, but I am so grateful for your help @moloned!

Editing moloned's answer to below fixed the issue for me:
export DISPLAY="$(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0"

In case anyone here is unaware (as I was) the above setup is not necessary with WSLg, released (as beta) on 2021-04-27. It is now also available on Windows 10.

The latest WSLg does not need to use the IP of the Windows host, VcXsrv, nor any firewall changes.

$ echo $DISPLAY
:0

From https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Prerequisites:

  1. Ensure using Windows 10 Insider build 21362+ [currently only Windows 11]
  2. Recommended: Install driver for vGPU: Intel AMD NVIDIA

For existing WSL systems:

  1. In administrative PowerShell: wsl --update
  2. wsl --shutdown to force a restart of the wsl

The above instructions are old, as of 2021-04-27.

The latest wsl does not need to use IP of windows host, nor does it need VcXsrv or any firewall changes.

$ echo $DISPLAY
:0

From https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

For existing WSL systems:

  1. Ensure using Windows 10 Insider build 21362+
  2. Install driver for vGPU: Intel AMD NVIDIA
  3. In administrative Power Shell: wsl --update
  4. wsl --shutdown to force a restart of the wsl

Will this downgrade my AMD driver if I use it? When I first tried this is said it was a downgrade. Do I need this driver if I have a newer version?

XZLeo commented

Editing moloned's answer to below fixed the issue for me:
export DISPLAY="$(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0"

I followed @vinpremkumar's advice but got this:

sudo xeyes
[sudo] password for xzleo:
Authorization required, but no authorization protocol specified
Error: Can't open display: 172.30.0.1:0

I solved this by turning on "Disable access control" setting in XLaunch. And then Problem solved! Thanks so much!

Will this downgrade my AMD driver if I use it? When I first tried this is said it was a downgrade. Do I need this driver if I have a newer version?

Sounds like a question for the particular driver maker.


I don't understand the downvotes on my previous comment. Is there some reason that it's not helpful to know about WSLg?

I don't understand the downvotes on my previous comment. Is there some reason that it's not helpful to know about WSLg?

Probably just bummed out developers that just found out they need to upgrade to Windows 11 for this Microsoftยฎ patch.
Thanks for the info

I changed my display to this as you suggested:

export DISPLAY="grep nameserver /etc/resolv.conf | sed 's/nameserver //':0"

This didn't solve this issue... BUT in that link you posted, I looked at some of the solutions people were offering. I decided to check VcXsrv's firewall permissions and discovered it never even created a profile for itself when I installed it.

I went to Control Panel > System and Security > Windows Defender Firewall > Advanced Settings > Inbound Rules > New Rule... > Program > %ProgramFiles%\VcXsrv\vcxsrv.exe > Allow the connection > checked Domain/Private/Public > Named and Confirmed Rule.

And it worked!!! I've been working on this for 3 days, I don't know how it has taken this long to find the solution, but I am so grateful for your help @moloned!

How did it "worked?" I only still get https://dpaste.org/GtSb even after adding VcXsrv to the firewall and restarting my desktop?

The above instructions are not necessary with WSLg, released (as beta) on 2021-04-27.

The latest WSLg does not need to use IP of windows host, nor does it need VcXsrv or any firewall changes.

$ echo $DISPLAY
:0

From https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Prerequisites:

  1. Ensure using Windows 10 Insider build 21362+ [currently only Windows 11]
  2. Recommended: Install driver for vGPU: Intel AMD NVIDIA

For existing WSL systems:

  1. In administrative PowerShell: wsl --update
  2. wsl --shutdown to force a restart of the wsl

It's work for me on w11

It had been working great for me to run my WSL GUI applications using a launch script (first argument is the distro name, second argument is the Linux application):

@echo on
for /f "tokens=3 delims=: " %%I in ('netsh interface IPv4 show addresses "vEthernet (WSL)" ^| findstr /C:"IP Address"') do set ip==%%I
set ipAddress=%ip:~1%
Powershell.exe wsl -d %1 "DISPLAY='%ipAddress%':0" %2

But then sometime in October 2021, the above stopped working on one of my computers. Haven't figured out what's different, but after digging around, I think I stumbled upon the solution: use the IP of the host interface, whichever one has a Default Gateway, which in my case was "Ethernet 2". You may have to experiment a bit to figure out which one works.

@echo on
for /f "tokens=3 delims=: " %%I in ('netsh interface IPv4 show addresses "Ethernet 2" ^| findstr /C:"IP Address"') do set ip==%%I
set ipAddress=%ip:~1%
Powershell.exe wsl -d %1 "DISPLAY='%ipAddress%':0" %2

Has anyone set up protocols or have an idea how to do it this way.
I have tried all the above solutions from the comments.

No protocol specified
Error: Can't open display: 172.29.48.1:0

edit
Solution:
I didn't have it connected to my local machine, but to some server, I reloaded the windows on localhost, exported the WSL ip and it works. Check if you Disable Access Control in XLaunch on Windows.

and add this to .bashrc
export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'):0.0

@53jk1 not working ;( Windows 10 WSL2
.190 is my IP address so why is it picking .1?

image

eventually comes back with
ldonofri@FXR32R2:~$ sudo service cron start && sudo service cups start && sudo service dbus start && startxfce4
[sudo] password for ldonofri:

  • Starting periodic command scheduler cron [ OK ]
  • Starting Common Unix Printing System cupsd [ OK ]
  • system message bus already started; not starting.
    /usr/bin/startxfce4: X server already running on display 172.22.64.1:0.0
    xrdb: Connection timed out
    xrdb: Can't open display '172.22.64.1:0.0'
    dbus-daemon[6564]: [session uid=1000 pid=6564] Activating service name='org.freedesktop.systemd1' requested by ':1.0' (uid=1000 pid=6631 comm="systemctl --user list-jobs ")
    dbus-daemon[6564]: [session uid=1000 pid=6564] Activated service 'org.freedesktop.systemd1' failed: Process org.freedesktop.systemd1 exited with status 1
    Unable to init server: Could not connect: Connection refused
    xfce4-session: Cannot open display: .
    Type 'xfce4-session --help' for usage.
    ldonofri@FXR32R2:~$

export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0.0)

ldonofri@FXR32R2:$ exec dbus-run-session -- bash
ldonofri@FXR32R2:
$ sudo service cron start && sudo service cups start && sudo service dbus start && startxfce4

  • Starting periodic command scheduler cron [ OK ]
  • Starting Common Unix Printing System cupsd [ OK ]
  • Starting system message bus dbus [ OK ]
    /usr/bin/startxfce4: X server already running on display 8.8.8.8:0.0
    xrdb: Connection timed out
    xrdb: Can't open display '8.8.8.8:0.0'
    dbus-daemon[8398]: [session uid=1000 pid=8398] Activating service name='org.freedesktop.systemd1' requested by ':1.0' (uid=1000 pid=8504 comm="systemctl --user list-jobs ")
    dbus-daemon[8398]: [session uid=1000 pid=8398] Activated service 'org.freedesktop.systemd1' failed: Process org.freedesktop.systemd1 exited with status 1

Assuming VcXsrv is installed and run with display = 0 with 'Disable access control' checked, set the display as follows:

1- Get the IP from WSL teminal as

cat /etc/resolv.conf

2- Set the display to that ip

export DISPLAY=_<your ip from above>_:0.0

How will this work when I have to change my resolv.conf file to work with my work's VPN? I many different IP addresses and none of them seem to work.

  1. I run & 'C:\Program Files\VcXsrv\vcxsrv.exe' -ac via powerhell
  2. I set my top nameserver value to one of the 4 options I have in my resolve.conf
  3. I run export DISPLAY=$(grep -m 1 nameserver /etc/resolv.conf | awk "{print $2}"):$DISPLAY_NUMBER
  4. I call emacs, and I get error: Display nameserver 172.26.64.1:0.0 unavailable, simulating -nw

This error occurs for any ip address I use

Hey guys. This has been a long running issue, here are my thoughts in case it may be helpful to some:

  1. Looks like this piece is under active development by Microsoft's WSL initiative
  2. I would consider any solution suggested here, working or not - temporary. A new patch may come by any day
  3. Generally, implementing the solution on linux machines and also developing it on one seems ideal if possible. Solutions like VMWare or RHEL seems relevent if applicable.

@Tapuzi This has been solved on Windows 11 with WSLg.

@Tapuzi This has been solved on Windows 11 with WSLg.

Well, why are you tagging me? this discussion is irrelevent for me. I have a solution.
Heared Windows 11 is good by the way, but I don't tend to upgrade systems hastely.

Restart WSL distribution after running this command:
export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0)
restart distribution: wsl --shutdown <distribution>

export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0.0)

This worked for me. WSL, windows 10, Ubuntu 20.04.5 LTS

But I would like to mention that; before this I added a "New Inbound rule" for VcXsrv on Windows defender firewall. Not sure if that had an effect on or was the reason for the success...

For anyone else coming here, it was reinstalling the wsgl nvidia driver that fixed it for me

I had a similar issue first on Windows 10. Then I realized I must have Windows 11 for GUI to work, but the error was the same. That was because I tried to work around the issue while being on Windows 10 with adding export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0) to .bashrc. That set my $DISPLAY variable to <ip-address>:0 while it should have been just :0. After removing the export line from my .bashrc file and restarting WSL the GUI apps started to work ๐ŸŽ‰

@tolache - that was just waaaay too easy, mate! :-)
Wasted several hours reading answers over on SO and SU, and just removing the broken export made everything work.
Thank you so much.

For me the correct IP was the one taken from the Xsrv log: XdmcpRegisterConnection: newAddress

WSLg now also works on Windows 10 after the recent update. No need for any modification to $DISPLAY or Xsrv.

cat /etc/resolv.conf

Worked for me + Disabled Server Access Control

Disabled Server Access Control on XLaunch did the job for me.

I had a similar issue first on Windows 10. Then I realized I must have Windows 11 for GUI to work, but the error was the same. That was because I tried to work around the issue while being on Windows 10 with adding export DISPLAY=$(echo $(grep nameserver /etc/resolv.conf | sed 's/nameserver //'):0) to .bashrc. That set my $DISPLAY variable to <ip-address>:0 while it should have been just :0. After removing the export line from my .bashrc file and restarting WSL the GUI apps started to work

So easy - just removing $DISPLAY from .bashrc. Needed hours to find this. Thanks a lot

I don't know how but this issue resloved on its own after install gedit with " sudo apt install gedit" and then running "gedit".

I just ran wls --update and now I have this issue too.

This

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

does not help.

echo $DISPLAY
localhost:0.0

@sorenwacker don't mess with $DISPLAY environment variable. It is not needed in modern Windows 10 or 11.

image

cinderblock that does not seem to be correct. I just tried to run google chrome without the DISPLAY variable and it did not start. Then after setting it it opened.


google-chrome
[517:517:0416/130735.820967:ERROR:ozone_platform_x11.cc(239)] Missing X server or $DISPLAY
[517:517:0416/130735.821041:ERROR:env.cc(255)] The platform failed to initialize.  Exiting.

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

google-chrome

image

Same with my automated test suite. Without the DISPLAY variable I get this error:


E       _tkinter.TclError: couldn't connect to display "localhost:0.0"

/home/swacker/miniconda3/envs/ms-mint/lib/python3.11/tkinter/__init__.py:2326: TclError


Then with that variable the test run through without problem. That could be specific to my system of course.

now it works without, had some leftover config flying around.

rwp0 commented

In case anyone here is unaware (as I was) the above setup is not necessary with WSLg, released (as beta) on 2021-04-27. It is now also available on Windows 10.

The latest WSLg does not need to use the IP of the Windows host, VcXsrv, nor any firewall changes.

$ echo $DISPLAY
:0

From https://docs.microsoft.com/en-us/windows/wsl/tutorials/gui-apps

Prerequisites:

  1. Ensure using Windows 10 Insider build 21362+ [currently only Windows 11]
  2. Recommended: Install driver for vGPU: Intel AMD NVIDIA

For existing WSL systems:

  1. In administrative PowerShell: wsl --update
  2. wsl --shutdown to force a restart of the wsl

Nothing worked except this.

apt install x11-apps
export DISPLAY=:0
xeyes

Just need to set DISPLAY to :0 and export that.

I wonder why the official Microsoft/Ubuntu tutorials do not mention this required step.

I wonder why the official Microsoft/Ubuntu tutorials do not mention this required step.

@rwp0 because that's the default value. Unless people start messing with $DISPLAY, it will have the correct value.

rwp0 commented

I wonder why the official Microsoft/Ubuntu tutorials do not mention this required step.

@rwp0 because that's the default value. Unless people start messing with $DISPLAY, it will have the correct value.

I didn't mess with DISPLAY in any way.
It was so (undefined) from a default installation.

I can confirm this worked for me https://stackoverflow.com/a/66398613

I was following this tutorial and just used the alternative command https://www.youtube.com/watch?v=XJ0dI2SYHIE