Ssh – Multiple hops tunnels howto

sshssh-tunnel

I wonder if anyone is able to help me with multiple tunnel hops for servers…

basically my setup looks something like this…

[desktop01]=====[server01]=====[server02]---------[machine01]
                                         \--------[machine02]
                                          \-------[machine03]
                                           \------[machine04]

I want to setup some ssh tunnels so that from my desktop I can ssh directly to machine01 through 04 without having to ssh to each box in between in turn…

So desktop01 can only see server01, server02 can only accept connections from server01 and machines 01 to 04 only accept connections from server02.

Can anyone help as im really stuck with this.

Thank you in advance 🙂

Best Answer

The following configuration in ~/.ssh/config allows you to dynamically setup jump hosts by separating them with the % symbol:

Host *%*
    ProxyCommand ssh $(echo %h | cut -d%% -f2-) nc $(echo %h | cut -d%% -f1) %p

You could then run ssh machine01%server02%server01 and it would connect to machine01 via server01 and server02. (This doesn't work so well when you need to specify non-default usernames unfortunately.)