X11 Forwarding GUI Appears on Remote Server Instead of Client

sshx11

I have two machines, PC1 and PC2. in PC1 I have Msys2 and Xming installed on Windows 7 while in the second machine PC2 I have Windows 10 with Ubuntu 18.04 on WSL (Windows Subsystem for Linux), Xming is also installed. I configured both machines to use ssh and the necessary configurations for X11 forwarding in (/etc/ssh/sshd_config). in PC1 I have also to set DISPLAY variable:

export DISPLAY=localhost:0 

I want to ssh from PC1 to PC2 but I have one problem:
The following command works fine:

ssh -Y username@PC2 gedit

it shows gedit on PC1 as expected.
But when I ssh to PC2 then I start gedit:

$ ssh -Y username@PC2  # Now I am logged in to PC2
$ gedit

The GUI appears in PC2 instead of PC1

Could you please explain what's going wrong with the second way?

Best Answer

X forwarding over SSH is dependent on sshd setting the value of $DISPLAY appropriately, so forcing an override on the remote system will interfere. If you must set it regardless then only set it if it doesn't already have a value:

: ${DISPLAY:=:0}