Motivation
I want to connect to my work computer from home, to the primary X session and continue working remotely.
Problem description
My work computer is the vnc server. My home computer (the client) is to use a vnc viewer such as krdc (KDE Remote Desktop Connection) to allow me to control the remote machine. An added layer of complexity arises from the firewall that I need to tunnel through to reach my machine at work.
Solution
Server side, I’ll use x11vnc to accomplish this. This is because x11vnc allows me to connect to the primary X session – I need this since I might already have applications running at work and would like to continue where I left off. If this is not a concern, you can use tightvnc or other vnc servers, but you will need to start a separate X session to connect to.
As an aside, vnc is a better solution than using ssh with X11 forwarding. This has to do with the dreaded dropped connection problem. If the connection is dropped when you’re working on an application forwarded through ssh, it is terminated. It may or may not cleanly exit, leading to potential issues with data loss/corruption. This really depends on the application in question. However, if you use vnc, if the connection is dropped, you just get disconnected. The application is still running remotely. All you need to do is reconnect. Besides, if the application is already running on the remote machine, ssh forwarding doesn’t really help.
Prerequisites
An ssh server running on the server is highly recommended so you can tunnel all communication to and from the vnc server securely. This, however is optional, strictly speaking.
Procedure
1) Install the x11vnc package on the server. The following command will install x11vnc on arch systems.
# pacman -S x11vnc
2) Set up a vnc password for authentication. This step is optional, but recommended as without a password, anyone can connect to the vnc server. The following command will store a vnc password in ~/.vnc/passwd.
# x11vnc -storepasswd
3) Open the first of two tunnels. The first tunnel is for ssh. The following command will open a tunnel from local port 2222 (or any other port of your choice) to remote port 22.
$ ssh -fNL 2222:<remote server>:22 <user>@<gateway>
4) Now, in a terminal, ssh into the remote server and start the x11vnc server. The -create flag will scan for X sessions you’re logged into and connect to it. If you aren’t logged in or if someone else is logged in at the remote machine, it’ll create a new session for you. This new session cannot be viewed by someone at the remote server console. The -usepw flag tells the server to use an authentication password. The first place this looks for the password is in ~/.vnc/passwd. The -noxdamage flag is a performance option that worked well for me. Note the port number x11vnc is listening on. Usually, this will be 5900.
$ ssh -p 2222 localhost
<remote>$ x11vnc -create -usepw -noxdamage
5) Now, open the second tunnel for your vnc client to connect to the x11vnc server at the port it’s listening on. In this case, I’ve assumed it’s 5900. The following command will open a tunnel from local port 5959 (or any other port of your choice) to remote port 5900.
$ ssh -fNL 5959:<remote server>:5900 <user>@<gateway>
6) Now, you’re ready to point your vnc viewer to localhost:5959 and commence your vnc session. Terminate the x11vnc server on the remote machine when done. If you log out, the server will automatically terminate.
Problems
If you connect to an already running X session, it remains visible remotely. Other than leaving your screen turned off, I haven’t come up with a better solution. However, if the remote machine is either at the login screen or if someone else is logged in, the new session is invisible to the remote user.
Performance is wickedly slow when compared to protocols like rdp (remote desktop from Windows). This seems to be the best vnc is capable of. I’ve heard good things about FreeNX and will play with it when I get the time. If you have any suggestions about improving the performance of vnc, do let me know in the comments!
EDIT: Clarifications, reordering of steps to be more logical.
