wsl_x11_gui

十一月 17, 2022

wsl_x11_gui

不能正常生成Xauthority的临时解决方式

ssh -R

X11 forwarding本质上就是通过ssh remote tunnel将本地X server转发到远程, 所以即使远程主机的X11 forwarding是关闭的,我们也可以通过ssh -R转发服务。

# win11 上已经开启了vcXsrv
# ssh -R6000:127.0.0.1:6000 peter@192.168.1.xxx
# after login
# export DISPLAY=localhost:0.0
ssh -t -R6000:127.0.0.1:6000 peter@192.168.1.xxx 'DISPLAY=localhost:0.0 zsh -i'

新建一个空账户,共享Xauthority(不能生成Xauthority可能是账户的ssh config导致的?)

useradd -m guiu
passwd guiu
# relogin guiu from local, check the Xauthority & DISPLAYA env
context=$(xauth list -f ~guiu/.Xauthority)
display=DISPLAY

# in the origin user: peter
xauth add $context
export DISPLAY=$display  #localhost:10.0
xeyes

WSL2

Rather than disabling access control on VcXSrv, you should use the .Xauthority file to share keys between your X11 clients and the VcXSrv X11 server. The .Xauthority contains a collection of authorization keys indexed by the DISPLAY . You'll need to setup this file with a key for your particular Windows host and share that file between the VcXSrv and your X11 clients running on your WSL2 distro. To setup this, follow these steps:

Run your WSL2 distro (Assuming this is a debian based one) and install xauth, md5sum and gawk or awk. We'll also install an X11 client to test our setup. In this case, we'll install gnome-terminal but you can install something else if you want. On an Ubuntu distro, you can do:

# get the IP ADDRESS NN.NN.NN.NN for your host machine. You may have to try a number
# of different ways. Some are listed in this page. Others can be found on Microsoft's
# support pages.
export DISPLAY=NN.NN.NN.NN:0
# IF you have docker installed on your windows host, you can just set the display to
# export DISPLAY=host.docker.internal:0 
sudo apt install -y xauth coreutils gawk gnome-terminal 
xauth list # this should be an empty list
magiccookie=$(echo '{some-pass-phrase}'|tr -d '\n\r'|md5sum|gawk '{print $1}')
xauth add "$DISPLAY" . "$magiccookie"
# Now check to see if it was added by doing
xauth list # you should see the newly added entry for host.docker.internal
# The line below assumes that you've installed your windows into the default location
# C:\Windows folder. We use the cmd.exe to grab the location of the Windows' UserProfile 
# folder
userprofile=$(wslpath $(/mnt/c/Windows/System32/cmd.exe /C "echo %USERPROFILE%" | tr -d '\r\n'))
cp ~/.Xauthority "$userprofile"

Add the above export DISPLAY=XXXXX:0 to your ~/.bashrc in your WSL2 distro home We need to create either an XLaunch configuration file (i.e. config.xlaunch ) or a shortcut to VcXSrv.exe with the desired command line args. XLaunch is a simple launcher that assists in setting up the arguments and in turn calls vcxsrv.exe. We'll ingore using XLaunch and just create our own shortcut with the appropriate arguments. We want to run VcXSrv.exe with these args:

vcxsrv.exe -multiwindow -clipboard -wgl -auth {.XAuthority file} -logfile {A Log file} -logverbose {int log level} From above, we copied the .Xauthority file to your Windows 10 userprofile directory which will be something like /mnt/c/Users/{WindowsUserName}/.Xauthority which means our desired command line is:

%ProgramFiles%\VcXsrv\vcxsrv.exe -multiwindow -clipboard -wgl -auth "%USERPROFILE%.Xauthority" -logfile "%USERPROFILE%\VcXSrv.log" -logverbose 5 We use the Windows 10 environment variables %ProgramFiles% and %USERPROFILE% to get the location of the Program Files folder and UserProfile folder under Windows 10. Feel free to omit the logfile and logverbose options if you're not debugging any issues. So you can just do:

%ProgramFiles%\VcXsrv\vcxsrv.exe -multiwindow -clipboard -wgl -auth "%USERPROFILE%.Xauthority" To create the shortcut, navigate to where VcXSrv.exe is installed. The default location of this is

%ProgramFiles%\VcXSrv\VcXSrv.exe In the explorer file window, right click on the VcXSrv.exe and click "Create Shortcut" . This will create a shortcut on your desktop.

Right click over the created shortcut icon, and select properties.

In the Shortcut tab, append the arguments above after the executable . It should look something like:

%ProgramFiles%\VcXsrv\vcxsrv.exe -multiwindow -clipboard -wgl -auth "%USERPROFILE%.Xauthority" In the General tab of the Properties dialog, change the name to be "VcXSrv with XAuthority" .

Click ok.

Now you can start the X11 server by double clicking on the shortcut.

If you wish to have the X11 server started at startup, follow the instructions here: https://support.microsoft.com/en-us/windows/add-an-app-to-run-automatically-at-startup-in-windows-10-150da165-dcd9-7230-517b-cf3c295d89dd

Now back in the WSL distro terminal, you should be able to run the gnome-terminal or other X11 client and have it display securely on the VcXSrv X11 server running on the Windows host.

export DISPLAY=host.docker.internal:0 gnome-terminal