SSH Tunnel connect 2 Servers via third Server

networkingsshssh-tunneltunneling

I have 3 Servers, A is a public accessable Server in the Internet.
B Hosts a Webservice I want to access.
C has access rights to connect to A and B.

Now i want that if a Client D tries to access a special Port on A that he gets forwarded to B.

IP's and Ports

A:

  • 1.0.0.1:22 SSHD Server
  • 1.0.0.1:443 Public Port which i want to use

B:

  • 1.0.0.2:23 SSHD Server
  • 1.0.0.2:444 Webservice I want to access

C:

  • 1.0.0.3

D:

  • 1.0.0.4

Diagram:

  +------------+        +------------+
  | Client (D) +--------> Public (A) |
  +------------+        +-----^------+
                              |
  +----------------+    +-----------+
  | Webservice (B) <----+ Proxy (C) |
  +----------------+    +-----------+

Question:

What ssh tunnel Commands do i need to execute on C so that if i try to open 1.0.0.1:443 on D i get the service hosted on 1.0.0.2:444 ?

Best Answer

On C you can run

ssh -fNR 1.0.0.1:443:1.0.0.2:444 root@1.0.0.1

It will only work if you login as root user because 443 is a privileged port. Moreover it only works if sshd on A is configured with GatewayPorts set to yes or clientspecified. (The default is no and using yes cannot be recommended, so if you want to do it this way I recommend clientspecified).