Thursday, July 01, 2010

X server in cygwin

Start an X server locally
Install Cygwin
http://x.cygwin.com/docs/ug/setup.html

Identify X Displays
Start by identifying your X displays.  Xrandr is good for this:

$ xrandr 
Screen 0: minimum 2560 x 1024, current 2560 x 1024, maximum 2560 x 1024
default connected 2560x1024+0+0 0mm x 0mm
   2560x1024      50.0* 

For cygwin, start X:
$ startxwin &
[1] 3528

Make sure you have a DISPLAY environment variable set locally:
$ export DISPLAY=:0.0

To verify, echo it out:
$ echo $DISPLAY
:0.0

This is important because a properly working X server will export the DISPLAY environment variable to the remote server.

You can discover the process id of the X server by looking at the lock file:
$ ll /tmp/.X0-lock 
-r--r--r-- 1 root root 11 Sep 13 11:26 /tmp/.X0-lock

$ cat /tmp/.X0-lock 
      5647

$ ps -ef | grep 5647
root      5647  5645  5 Sep13 tty7     1-00:16:30 /usr/bin/Xorg :0 -br -verbose -auth /var/run/gdm/auth-for-gdm-SWEiqo/database -nolisten tcp

X servers will listen on port 6000 by default:
$ netstat -na | grep LISTENING
  TCP    0.0.0.0:6000           0.0.0.0:0              LISTENING

The first digit of the port will change based on the display number (0, 1, 2 etc).  So the default port of 6000 may change to 6001 if you've started an X server on display 1:
$ startxwin -- :1

$ netstat -na | grep LISTENING
  TCP    0.0.0.0:6001           0.0.0.0:0              LISTENING

Test by opening an xterm locally:
$ /usr/X11R6/bin xterm -display 127.0.0.1:0.0 -ls

Running remote X commands
Once I've verified that my X server is running properly, I will test running an X program remotely.  In this example, I needed to kick off a script on my Mac that depends on X.  My Macbook Pro (downstairs) is remote to my main Linux box which is upstairs.  I needed to startup an X session on the remote Mac.

From my local Linux box, I start a secure X session on my remote Mac:
$ ssh -Y sodo@192.168.110.177
Password:
Warning: No xauth data; using fake authentication data for X11 forwarding.
Last login: Fri Sep 30 14:23:09 2011 from computer
Have a lot of fun...
sodo@remote:~>

This sets up a secure X windows communication, the DISPLAY environment variable, the X authentication and starts up the local X terminal application logged on to the remote server.

I verify that the Mac's X server has a DISPLAY environment variable set.  This variable was properly forwarded by my Linux box and translated by the Mac:
sodo@remote:~> echo $DISPLAY
localhost:10.0

Once these settings were confirmed, I started an xterm
sodo@remote:~> xterm

Note that the above steps could have been accomplished with a one liner:
$ ssh -Y sodo@192.168.110.177 xterm

Cool.

Troubleshooting
1) I first got this error:
Warning: No xauth data; using fake authentication data for x11 forwarding

From this thread:
http://www.cygwin.com/ml/cygwin-xfree/2004-10/msg00236.html

I choose to ignore!

2) This is very important. The remote server's SSH daemon must be configured to forward X11 requests.  From http://x.cygwin.com/docs/ug/using-remote-apps.html#using-remote-apps-ssh:
Note: By default, the OpenSSH server does not allow forwarded X connections. This must be configured on the remote host by adding X11Forwarding yes to the sshd_config configuration file. The OpenSSH server must be restarted or SIGHUP'ed to re-read the configuration file after it is changed.

Note: The OpenSSH server requires the xauth command to be available to forward X connections. Consequently, it must be installed on the remote host.

3) .Xauthority does not exist
$ ssh -X liveuser@192.168.x.x
liveuser@192.168.x.x's password:
Last login: Thu Oct  4 16:59:48 2012 from 192.168.x.y
/usr/bin/xauth:  file /home/liveuser/.Xauthority does not exist
 

Well..create it then, fah Gawd's sake!
[liveuser@localhost ~]$ touch .Xauthority
[liveuser@localhost ~]$ chmod 755 .Xauthority
[liveuser@localhost ~]$ exit
logout
Connection to 192.168.x.x closed.

$ ssh -X liveuser@192.168.x.x
liveuser@192.168.x.x's password:
Last login: Thu Oct  4 17:02:13 2012 from 192.168.x.y
[liveuser@localhost ~]$ cat .Xauthority
localhost.localdomain10MIT-MAGIC-COOKIE-165??


About .Xauthority
http://en.wikipedia.org/wiki/X_Window_authorization#Cookie-based_access

ReferenceUsing Cygwin/X
Cygwin X FAQ
Using Cygwin/XUsing Cygwin/X
http://www.int.gu.edu.au/~anthony/info/X/Display.needs

No comments:

Feel free to drop me a line or ask me a question.