I want to ssh to a WSL2 instance using X11 forwarding from another machine using MobaXterm. The WSL2 host machine and remote machine are literally sitting beside each other and connected to the same switch on a private network so security shouldn't be an issue. MobaXterm is the app I'm using for my ssh server and it has a built in X Server. I'm assuming the approach would be similar using other tools.
It's currently easy to do this from the host machine. I create a WSL Session in MobaXterm and start it. X11 forwarding is configured by default. From there launching UI apps works without any issues or changes to configuration. This automatically sets the DISPLAY variable in the shell to something like 'localhost:10.0' where 10, the display number, will increment for ever session that is opened.
To ssh from the remote machine I followed the steps outlined by x11 forwarding from WSL2 to remote machine: "THE EASY WAY how to SSH into Bash and WSL2 on Windows 10 from an external machine" https://www.hanselman.com/blog/the-easy-way-how-to-ssh-into-bash-and-wsl2-on-windows-10-from-an-external-machine
This allows me to ssh from the remote machine. However, the DISPLAY shell variable isn't set and I can't run any UI apps. If I'm running a WSL session already on the host as I explained in the first step; I can manually set DISPLAY (ex: export DISPLAY=localhost:10.0) and that will work but not too surprisingly it will open the UI on the host machine rather than the remote machine. I assume I need to somehow create a new display on the remote mobaXterm server and set DISPLAY to use that, but I don't know how to do this. I've opened up the related X11 ports in the firewall. ex: 6000-6020.
Scott Hanselman also outlines another approach, where he doesn't replace the default ssh shell. But he recommends against this for various reasons. I've had some success setting up my own SSH server directly in WSL linux, but this is also a bit of a pain in part because the WSL IP address is always changing on reboot.
There are a few related posts already:
- x11 forwarding from WSL2 to remote machine this mentions the recommend Scott Hanselman doesn't work for X11 but doesn't add much detail as to an alternative.
- How to set up working X11 forwarding on WSL2 this post focuses on getting X11 working on the host machine. But now (around April 2021 release) that is easy to do as described above. I want to get this working from a remote machine.
Using:
- MobaXterm version: V21.2 Build 4642
- WSL2 version: 5.10.16