chaining ssh tcp port forwarding

1

1

I need to connect to a DB server which accepts connections on port 1521, I can only access this from a remote machine which I need to ssh in to through another gateway.

ie from home

home $ ssh user@gateway

gateway $ ssh desktop

desktop $ < I now have access to port 1521 on host dbserver >

What are the commands I need to use for this, so that I would then connect to localhost:1521 on home pc for db connections

Alasdair

Posted 2012-02-08T12:03:55.307

Reputation: 33

Answers

2

Two ways of chaining:


  1. Connect from home to gateway, forwarding localhost:1521 on home via gateway to localhost:PORT.

  2. Connect from gateway to desktop, forwarding localhost:PORT on gateway via desktop to dbserver:1521.

home$ ssh -ftL 1521:localhost:PORT gateway ssh -NL PORT:dbserver:1521 desktop

PORT can be 1521 or any other port.


  1. Connect from home to gateway, forwarding localhost:PORT on home via gateway to desktop:22.

  2. Connect from home to desktop using the established tunnel via gateway, forwarding localhost:1521 on home via desktop to dbserver:1521.

home$ ssh -fNL PORT:desktop:22 gateway
home$ ssh -fNL 1521:dbserver:1521 -o "HostkeyAlias=desktop" -P PORT localhost
DB connection can be made now

(The -fN options tell SSH to connect, establish tunnels, then continue running in background.)


The second method is more useful, since it allows you to reach desktop directly from home (using the running tunnel) for all kinds of connections.

user1686

Posted 2012-02-08T12:03:55.307

Reputation: 283 655

Thanks for posting that answer, I now realise I'm actually up against an altogether bigger problem as the dbserver just listens on that port, it immediately opens another port for communication. Think this is trickier than I can handle... – Alasdair – 2012-02-08T16:56:03.173