Multi-hop SSH Port Forwarding

Following code block can initiate a tunnel between two ssh servers when the second ssh server doesn’t/can’t accept direct connections from the first, and it uses an interim ssh server to forward the port from the source (where you would run this code block)  to the destination.

If you don’t have public/private keys established between servers, ssh will prompt you to enter two passwords, first is for the interim-server and second is for the destination.

PORT=1900; USER=purinda; SECOND_HOP="ssh -N -R destination-server:${PORT}:localhost:${PORT} ${USER}@destination-server"; ssh -t -t -R interim-server:$PORT:localhost:80 ${USER}@interim-server ${SECOND_HOP}

1. Ports you need to hop between server are available (closed), on both interim and destination servers.

2. There is a common user account which you can use for hopping. My first name in this case (if not you will need to modify the command to use different usernames).

3. You should use IP addresses as interim and destination servers (at least in the section where you forward the port, ‘IP-destination-server:${PORT}’)

4. Forwarding port 80 in this example, change it to whatever port you want to forward.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s