How to connect to remote X11 hosts from a Mac

In my previous two posts on the subject, I explained why you'd want to use X11 to drive a host remotely, and the basics of configuring your Mac to run OS X's X11 server and to use local X11 software. Now we get to the most important step, which, once you understand the whole X11 client/server thing, is a walk in the park.

In X11 parlance, the X11 server is the software that handles communications and renders client content. The X11.app that you run on your Mac is the server. X11 applications on remote hosts are clients. They reach out to your server to tap your display, keyboard and mouse, but with far lower networking and compute overhead than full-screen remote desktop sessions require.

The toughest thing about X11 used to be arranging for X11 clients to see your server. Reaching across LAN segments, or through NATs and firewalls, was no picnic without resorting to VPN. Fortunately, some creative melding of X11 and SSH, the secure shell, gave us this gem:

ssh -X hostname

When run from inside xterm on your Mac, this command creates a tunnel from the remote machine to your X server. You have to be able to access that machine via ssh, of course, which requires that you set up sshd (the SSH daemon) on the remote box and exchange credentials.

When ssh -X connects, it will ask for a password, just as regular ssh does. Once you get a shell prompt, do this:

echo $DISPLAY

The answer should come back "localhost:10.0" unless the remote machine has been configured differently. If DISPLAY is blank, you can set it:

export DISPLAY="localhost:10.0"

Now, whenever you run an X11-enabled app in that ssh session, the application runs on the remote machine and automatically opens its windows on your Mac. You may need to specify the path to your remote system's stash of X11 clients. For example,

export PATH=/usr/openwin/bin:$PATH

is required on Solaris machines.

Once the X11 apps are in your PATH, you can go snooping around. Everything compiled against GNOME and KDE is intrinsically X Window-enabled. If your remote machine has the GNOME desktop environment installed (it doesn't need to be running), try this in your ssh session:

nautilus &

That's GNOME's file manager.

gnome-system-monitor &

is useful, too, and Firefox runs nicely on X11.

When you're offline for periods of a few minutes, your SSH tunnel will be held open for you and reconnected as soon as your LAN interface comes back up. But if you're offline for too long, your session will expire and you'll get kicked back to your Mac's shell prompt. Just ssh -X again.

X11 is much faster and more efficient than VNC for remote access to GUI apps, and once you get it down the first time, it'll be second nature, even to connect two Macs.