Strovent SystemsSign in
Reference

SSH port forwarding through a jump host: -J with -L

Combine -J and -L: ssh -N -J [email protected] -L 5433:db.internal:5432 [email protected]. The last host in the chain connects to the destination.

Put the jump host in -J and the forward in -L, on the same command:

ssh -N -J [email protected] -L 5433:db.internal:5432 [email protected]

Then point your program at localhost:5433 on your own device. -N tells ssh not to start a shell, which is what you want for a tunnel only.

Which host makes the connection

Options on the ssh command line apply to the destination, not to the jump hosts (the ssh manual says this for -J). So in the example:

  • Your device listens on port 5433.
  • The traffic goes through the jump host to app.internal, inside the encrypted SSH connection.
  • app.internal opens the connection to db.internal:5432. The name db.internal is looked up on app.internal, and only that host's network path to the database matters.

That also means localhost in the forward is the target host, not your device. -L 8080:localhost:80 [email protected] reaches a web server that listens only on app.internal itself.

When you do not need the target login

If the jump host can reach the service itself, forward through the jump host alone:

ssh -N -L 5433:db.internal:5432 [email protected]

Here the jump host opens the connection to the database. You need no account on any other machine.

In ~/.ssh/config

Host jump
  HostName jump.example.com
  User user1

Host app
  HostName app.internal
  User user1
  ProxyJump jump
  LocalForward 5433 db.internal:5432
  ExitOnForwardFailure yes

Then ssh -N app. LocalForward takes the local port, a space, then host:port as seen from the remote side. Forwards go in the block of the host that should make the connection, here app. For more on the file, see The ssh config file.

With ExitOnForwardFailure yes, ssh quits if it cannot set up the forward, for example when the local port is taken, instead of leaving you with a login and no tunnel. It does not detect a destination that refuses connections later.

Errors

  • bind [127.0.0.1]:5433: Address already in use, then Could not request local forwarding. Another program, or an older tunnel, already uses that port on your device. Pick another local port.
  • channel 2: open failed: administratively prohibited: open failed (any channel number). The host that should open the connection refused to forward. Its sshd_config may set AllowTcpForwarding no or remote, or DisableForwarding yes, or a PermitOpen list without this destination, or your key line in authorized_keys has no-port-forwarding or restrict.
  • channel 2: open failed: connect failed: Connection refused. The forwarding host tried, but nothing accepted the connection on that port, or a firewall rejected it. Check the port from that host. A name that does not resolve there gives connect failed: followed by a lookup error. The same errors without a jump host are covered in SSH "channel 3: open failed: connect failed: Connection refused" with port forwarding.
  • The login to the target fails, with administratively prohibited and stdio forwarding failed. This time the jump host refused: -J needs TCP forwarding allowed on the jump host as well. See Jump hosts (ProxyJump).

The local port listens on the loopback address only, unless you give a bind address or set GatewayPorts, so other devices on your network cannot use the tunnel by default.

Several hops

List the jump hosts in order, separated by commas. The last host in the chain still makes the connection:

ssh -N -J [email protected],[email protected] -L 5433:db.internal:5432 [email protected]

Sources

Spotted a mistake? Email [email protected].