Search code examples
sshcygwinx11xterm

Cygwin ssh not setting DISPLAY correctly (extra :0)


I am using Cygwin on Windows 7 to connect to a unix (RHEL 6) server via SSH. When I do so, X forwarding is not being set correctly:

(Starting from Cygwin xterm on the Windows machine)

> echo $DISPLAY
:0
> ssh -Y [email protected]
[enter password]
> echo $DISPLAY
localhost:52.0:0

If I now try to start any windowed process, I get an error message:

> emacs
emacs: Cannot connect to X server localhost:52.0:0.
Check the DISPLAY environment variable or use '-d'.
Also use the 'xhost' program to verify that it is set to permit connections from your machine.

Additional Details

The number that follows 'localhost' (52 in the example above) is different each time I connect.

If I do the following,

> export DISPLAY=localhost:52.0

(that is, I remove the trailing ":0) then this DOES fix the issue, and the window forwarding works just fine.

So the question is: how to fix so that I do not need to manually change the DISPLAY variable with each connection?


Solution

  • A false alarm, much to my embarrassment. Turns out my .profile (that I had borrowed from someone else, as it contained a large quantity of useful customization for our development environment) contained these lines:

    export DISPLAY
    if [[ -z $DISPLAY ]]; then
        DISPLAY=my.machine.addr:0
    elif echo $DISPLAY | grep -z -v ':0'; then
        DISPLAY=$DISPLAY:0
    fi
    

    Lo and behold, commenting out the elif branch solves the problem. May this be a lesson to us all on the dangers of copying someone else's config without knowing what it contains.