SSH Tunnels Headaches

1 Comment

I’m writing a VNC mini howto and I got stuck with something. I wanted to do the following with a single command:

  • create the SSH tunnel (local port forwarding)
  • execute vncviewer on the local machine
  • have the SSH tunnel to be automatically closed at the time vncviewer was closed


After about one hour of trial and error and man page reading, I ended up with a very useful line of code:

ssh -f -L 25930:127.0.0.1:5904 -C me@remote sleep 10; vncviewer 127.0.0.1:25930:4

What this does is:

  • forwards local port 25930 to remote port 5904
  • forks the ssh session to the background executing a sleep command on the remote machine
  • executes vncviewer so that it connects to the vnc server through the tunnel

What I badly needed was a way to auto-close the tunnel at the time vncviewer was closed. This seems to do it. The sleep command keeps the tunnel open for ten seconds. After this period of time it’s closed, unless vncviewer or some other application uses it. In this case, it is closed at the time this application stops using it. Pretty fu..ing cool!! I have wasted all my head’s juice until I managed to figure this out…

SSH Tunnels Headaches by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2005 - Some Rights Reserved

One response on “SSH Tunnels Headaches