Menu

VcXsrv & Win10 Log in to Edit

Xauth WSL2 (1)
blackcrack Joe On Hotmail

with explaintions in English


with explaintions in English


Windows 10 Pro Version 20H2 Setup For 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:

  1. 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"
  1. Add the above export DISPLAY=XXXXX:0 to your ~/.bashrc in your WSL2 distro home
  2. 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

Alternate Setup

These steps worked as of Mar 2021 on Windows 10 Pro Version 20H2

Steps to setup the server-side on windows:

At this point the server-side is ready. Now it's time to configure the client:

  • Open a terminal and find the IP of the windows host machine with this command:
    export DISPLAY=$(/sbin/ip route | awk '/default/ { print $3 }'):0
    https://github.com/microsoft/WSL/issues/4106#issuecomment-634778111.

  • run: echo $DISPLAY to see the IP.

  • run netcat to check if you've got connectivity:
    nc -v IP_FROM_PREVIOUS_STEP 6000 //make sure to remove the appended ":0" after the IP.
  • If you get Connection...succeeded! you are set.
  • Open your .bashrc and add the "export ..." command there in order to get it automatically every time you open a terminal. (Don't use static IP here because it's subject to change. Use the awk-ed export statement as is.)
  • Run an application and a window will pop-up on your host OS.

Source: alextsil/ steps_gui_wsl2_xserver.txt


or check out Using VcXsrv Windows X Server, add VcXsrv windows server exe to the Firewall ^ , create a Shortcut (.lnk) to the VcXsrv.exe and put it to the Autostart in your Startmenu .
And open a Terminal like at the Description from Alexil and make do the rest so that the display is exported


Discussion

  • Joe On Hotmail

    Joe On Hotmail - 2021-03-23

    I added instructions for using xauth with VcXSrv. Disabling access control is a really bad idea as anyone on your network can log any keys you enter or mouse movements etc simply by connectiong to your VcXSrv . Its very easy to setup a .Xauthority that you can share between different clients allowing them to connect to your server. This will at least allow you to control who can connect to your server.

     
    • blackcrack

      blackcrack - 2021-03-24

      all informations are well, for short using and for make a hard connection in security way, alone the User/Admin decide what he use .. so, thumps up :)

      but, you coud add to the commandline the variable for the user instead "{WindowsUserName}" should you use " %USERPROFILE%\.Xauthority"
      then is more easy for the peoples to use your cmdline,
      maybe could you update your cmdline ?

       

      Last edit: blackcrack 2021-03-24
  • Joe On Hotmail

    Joe On Hotmail - 2021-03-24

    Done. Great suggestion!

     
    👍
    1
  • Solopov Sergey

    Solopov Sergey - 2021-03-28

    my 2 cents, I was initially using other instruction, but also found this one and tried to set up xauth, so

    • xauth list was showing me "xauth: file /home/ssolopov/.Xauthority does not exist" error
      if someone deal with same, use
    touch ~/.Xauthority
    
    • if you are not working with docker, but with your PC, instead of host.docker.internal, use next, which will automatically retrieve your wsl host IP:
    export DISPLAY="$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }'):0.0"
    
    • in my case code "xauth add" above did not work as is, it was one parameter missing before magiccookie (took from here) :
    xauth add $DISPLAY . $magiccookie
    

    notice dot before $magiccookie

    • and after that when I started Xsrv with .Xauthority and was trying to call gnome-terminal I still had to allow Public firewall (not sure why, maybe later will dig for it)

    thanks for article

     

    Last edit: Solopov Sergey 2021-03-29
    • Joe On Hotmail

      Joe On Hotmail - 2021-03-29

      Thanks for the feedback. I fixed the typo in the xauth add as it was missing the '.' . As for the IP address, I'll leave it as an exercise to the reader to get the correct address. It seems that right one will depend upon both your VcXSrv and your particular WSL2 setup. Since I use docker, the host.docker.internal hostname works well and in my case, its mapped to the IP address associated with the ethernet connection WHICH IS NOT what the /etc/resolv.conf gives.

       
  • Joe On Hotmail

    Joe On Hotmail - 2021-03-29

    I added a gist on github that will walk you through setting up your xauthority file with WSL2. It will ask that you create a WSLENV variable to export a couple of Windows Environment Variables to your WSL2 distro. You'll need to determine the IP address which I obtained the one associated with my PC's ethernet connection using Window's "Network and Internet settings".

    1. Create a Shortcut to VcXSrv and rename it to "VcXSrvWIthXauth.lnk". Place this shortcut in your %USERPROFILE% folder. The command line for the shortcut should be something like %ProgramFiles%\VcXsrv\vcxsrv.exe -multiwindow -clipboard -nowgl -auth "%USERPROFILE%.Xauthority"
    2. Then in the WSL2 distro app, entering:
      cd to your USERPROFILE folder (e.g. cd /mnt/c/Users/{WindowsUserName})
      export DISPLAY=NN.NN.NN.NN:0
      bash -c "$(curl "https://gist.githubusercontent.com/zuut/c9c3b95b90d9620d4805c30e0b4791cc/raw/66d2d44e9843c1906024f4b6135de50f868f62c7/wsl2SetupForVcXSrv.sh")"

    NOTE: Download the zip containing the shortcut plus the script above and extract it and run in a directory. https://gist.github.com/zuut/c9c3b95b90d9620d4805c30e0b4791cc/archive/35ed038f1495179348878d0279a06952a103c18e.zip

     

    Last edit: Joe On Hotmail 2021-04-13
    • blackcrack

      blackcrack - 2021-03-30

      do you have an Github.com account ?

       
      • blackcrack

        blackcrack - 2021-03-31

        Joe told:

        Yes, I've a github account.

        then could you make a cmd/batch file and upload it, this could linking in this sourceforge wiki.. or like me : setup/update batch (btw; this tread is for conversations, be pm's only for wz *bg* ;) )
        best

         

        Last edit: blackcrack 2021-03-31

Log in to post a comment.