Remote display using X11 forwarding


Issue: I often login to a remote computer (for example, using ssh to login to my Mac at work from my Mac at home). However, when I issue a command on the remote computer that would normally cause an X11 window to open (for example, running vim to edit a file), I get an error message that looks like this:

Error: Can't open display:

and the command does not run. How can I display X11 windows from a remote computer?


Solution: The process by which an X11 window can be started on a remote machine but appear on your local machine via an ssh connection is called “tunneling” or “X11 forwarding”. For security reasons, Mac OS X has X11 forwarding turned off by default. In order to enable it, you must edit the /etc/sshd_config file on the remote computer. This can be done by opening this file on the remote computer in your favorite text editor, but will require running with administrator level privileges; for example,

sudo /usr/bin/vi /etc/sshd_config

Note that I have specified using the command line version of vi, since vim will not work yet because X11 forwarding is disabled!

Look for a line in the file that says:

X11Forwarding no

and change it to

X11Forwarding yes

Alternatively, the following sequence of commands will implement this change by creating a temporary copy of the /etc/sshd_config file with the necessary edit, and then replacing the real /etc/sshd_config file with the temporary one:

sed 's/#X11Forwarding\ no/X11Forwarding\ yes/' \
   /etc/sshd_config > /tmp/sshd_config 

sudo mv /tmp/sshd_config /etc/.

Caveat #1:
If you've managed to complete an ssh connection to the remote machine at all, then you probably already know that in order to do so, you must have turned on Remote Login on the remote Mac (in the Sharing panel of the System Preferences) for either “All Users” (not recommended unless absolutely necessary) or only the specific user name(s) you will be utilizing to complete the ssh remote login connection (safer).

Caveat #2:
In order to make your remote login connection as secure as possible, I recommend always using the ssh -X option to create an encrypted link between the local and remote computers.

OS Version Compatibility: Lion, Snow Leopard, Leopard, Tiger

Update Status: 15 March 2010 (added)