Thursday, May 5, 2011

HOWTO tunnel through multiple boxes with SSH

Making ssh tunnels, even through multiple machines, is easy. These two commands will effectively link localhost:9999 with box3:80, via box2.

user@box1:~$ ssh -L 9999:localhost:9999 box2
user@box2:~$ ssh -L 9999:localhost:80 box3
If you just want to ssh through one machine to another, as of OpenSSH 5.4 there is a better way (I haven't tested this yet):
 $ cat ~/.ssh/config
 Host internalhost.myhouse.com
   ProxyCommand ssh -W %h:%p sshgateway.myhouse.com
So when you ssh to internalhost.myhouse.com you will connect through sshgateway.myhouse.com. You can chain these together, so if there was a third host only accessible from internalhost that would look like:
 $ cat ~/.ssh/config
 Host internalhost.myhouse.com
   ProxyCommand ssh -W %h:%p sshgateway.myhouse.com

 Host ilovelayers.myhouse.com
   ProxyCommand ssh -W %h:%p internalhost.myhouse.com

No comments: