Using graphical interface (xfce4) on wsl2 via vcxsrv

Time:2022-5-26

Of course, there are many related tutorials on the Internet, but in my case, those methods are not completely correct, so I can’t use the graphical interface. So if you check many methods and can’t use them, you can take a look at this article.

This is my system parameter.

come to the point

Start vcxsrv

Download and install vcxsrv on windows.

It doesn’t matter how to choose the window state in the first step. I usually use one large window. There will be some problems with the default multiple windows in multi screen display. Then, the display number can be set to – 1 by default or 0 (of course, it can also be other numbers, which will be explained later).

Next, select start no client.

Next, check all the options and leave the additional parameters alone.

Click Next and click finish. At this time, a black window will appear, so that vcxsrv can be started.

WSL start desktop program

First download xfce4,sudo apt install xfce4

And then in Add a sentence to the end of the bashrc fileexport DISPLAY=xx.xx.xx.xx:0, where XX xx. xx. XX is the IP address of the host where vcxsrv is located, that is, the IPv4 address of the Ethernet adapter in ipconfig executed in CMD or PowerShell of windows.

For example, mine is 10.32.223.175, so I want to be in Add in bashrcexport DISPLAY=10.32.223.175:0

thensource .bashrcOr re-enter the WSL terminal.

Then executestartxfce4, if normal, the black window of vcxsrv will display the desktop of xfce4, and the WSL terminal will output the log of xfce4.

What happened?

The important concepts in the whole process are display number and environment variable$DISPLAY。 The display number set in vcxsrv is$DISPLAYThe number after the semicolon in. Of course, if vcxsrv uses the default display number (- 1), vcxsrv will automatically set this number. In the current situation, it is also possible to use the default setting.

When vcxsrv is started, it is equivalent to starting a virtual display on windows, and then the WSL desktop program needs to be connected to the virtual display to display normally. Linux is through$DISPLAYThis environment variable is used to find the target display. There are many tutorials of WSL connecting vcxsrv on the network$DISPLAYSet as:0.0, this seems to work on wsl1, but it doesn’t work on wsl2. (however, if you use xrdp to use the graphical interface, you can use this setting, because xrdp runs on Linux of WSL, and:0.0What I mean islocalhost:0.0。 As for why0.0, because these two numbers represent display and screen respectively, i.eD.S0.0It means screen 0 of display 0.A more detailed explanation can be implemented in Linuxman XTo see.

Another way on the network is to$DISPLAYSet as$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}'):0, the value is / etc / resolv The IP address of the nameserver in conf. I don’t know why this method doesn’t work on my machine (maybe it’s because the IP of someone else’s wsl2 nameserver is the same as that of windows?).

and$DISPLAYThe essence of is to tell the Linux graphics program X server address and display number. Therefore, it should be no problem to set the address to the IP address of the host where vcxsrv is located. If startxfce4 still fails to connect, thexrdb: Connection timed outThen check whether the IP can be pinged and set the firewall rules of windows.

other

  • Why should the last step of setting be checked.

The last tick is disable access control, which allows all clients to connect to this vcxsrv. Some tutorials say that additional parameters fo vcxsrv should be added in the last step-acThis parameter, but in fact, this parameter should have the same effect as checking disable access control. because-acThe explanation is disable access control restrictions.

  • If you don’t write export in What happens in bashrc

If only set in the terminal$DISPLAYIn fact, the value of can also run, but there may be problems when running some graphic programs in the graphical interface. Because export is only executed on the terminal Then the environment variable will only be useful in this terminal. If you exit the terminal or open a new terminal$DISPLAYThe value of is gone, so we should put it in Bashrc. Of course, other methods of permanently setting the environment are also possible.