Thursday, July 24, 2008

How To X-Forward On a Mac

If you're a terminal-jockey and you use a Mac, you've probably come across the term 'X-forwarding' before, but very few places will actually tell you what it is or how to do it.

The X Windowing System is an open source window manager that's used to draw windows in most free operating systems, including GNU/Linux, the *BSDs, and Solaris. It was ported to OS X in the form of the XQuartz project and is distributed with Tiger and Leopard install DVDs. You can also download it here if you're using Leopard (10.5), here if you're using Tiger (10.4), or here if you're using Panther (10.3).

Once it's installed, you can do a lot of cool stuff. For instance, you can run many more open source programs than before (check out Fink and Darwinports), and (more importantly for me) you can X-forward from any other computer that is currently running X.

What that means is that you can open an instance of an X-native program from another computer using your Mac and it will seem (to you) as though it were running on your Mac (i.e., you will have a window appear on your desktop that you can move, minimize, etc., but it uses the other computer's resources). It ends up being sort of like a remote desktop--such as VNC--but faster and without affecting the experience of local users. For example, I use X-forwarding to run programs from my Linux media center while my wife watches videos.

To do it, the remote computer you want to X-forward from has to have an SSH client--such as OpenSSH--installed. Once that is in place, open the on your Mac (located in Applications > Utilities by default) and type:
ssh -X
You have to use an account that already exists on the remote computer. It will then ask for the account's password. You will then be logged-in to the remote machine via your local terminal. Your command prompt will also change to reflect this:

Now, you can run programs via this terminal and any windows will be drawn on your local computer (i.e., your Mac) instead of the remote computer.

Here is what it looks like when I forward the Linux QT4 GUI for HandBrake to my Mac:

As you can see, the widgets look a little weird, and the font is clunky, but it's fully functional and has much lower latency than other remote access methods. Also, thanks to the ssh connection (also known as an 'ssh tunnel'), your connection cannot be spied upon, even if you X-forward across the Internet. Leave me a comment if you have any questions or run into any problems.


Anonymous said...

You should check out the "xpra" software suite on the Linux/Unix/whatever side. It is kind of like screen for X11. I use it in combination with X11 forwarding over SSH to run "long running" X11 applications, like HandBrake.

Here's a little use case:
Start the xpra daemon:
xpra start :1337 # starts a special X server on display :1337
xpra attach :1337 # connects to the new display

In another shell:
ghb --display :1337 # starts GHandBrake on :1337.

Magic! HandBrake appears on my Mac.

In the shell I ran xpra attach:
Control C

Look at that, HandBrake disappeared.

xpra attach :1337

And now it's back.

Hunter K. said...

Oh man, that sounds really great. I'll have to look into that. Thanks for the tip!

Analytics Tracking Footer