I want to tunnel VNC traffic to host2, which is only accessible from host1, whereas host1 is publicly accessible.

I setup a multi-hop SSH tunnel as described in this question, using:

ssh -L 5901:localhost:6000 host1 ssh -L 6000:localhost:5901 -N host2

This indeed works perfectly and does the job. However, I don't know how to correctly close the nested tunnel. I tried Ctrl+c but this seems to kill the first ssh instance to host1. But, the second ssh tunnel between host1 and host2 remains open, which is highly undesirable as any one can actually forward traffic through it.

Also, with -N option, I don't get an actual tty on host2, so I can't simply exit from there. Without, -N, I still don't get a tty, instead I get the following error:

Pseudo-terminal will not be allocated because stdin is not a terminal.
Warning: no access to tty (Bad file descriptor).
Thus no job control in this shell.
term: Undefined variable.

I am starting the connection from MacOS X, and both host1 and host2 are running RHEL 6.


You're on the right track with tty, and the -t option gives you just that. However, unless you are actually aiming to get a tty session for interacting, leave this option off of the last ssh command in your chain. In your case you just need it on the first connection:

ssh -L 5901:localhost:6000 host1 -t ssh -L 6000:localhost:5901 -N host2

Now when you use Ctrl-C, the tunnel will between all hosts.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.