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.internalopens the connection todb.internal:5432. The namedb.internalis looked up onapp.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, thenCould 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. Itssshd_configmay setAllowTcpForwarding noorremote, orDisableForwarding yes, or aPermitOpenlist without this destination, or your key line inauthorized_keyshasno-port-forwardingorrestrict.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 givesconnect 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 prohibitedandstdio forwarding failed. This time the jump host refused:-Jneeds 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]
Related
- Port forwarding: local, remote, dynamic
- Jump hosts (ProxyJump)
- The ssh config file
- Use a different SSH key for the jump host and the target
- Check if a TCP port is open without Telnet: nc, Test-NetConnection, bash
- SSH "channel 3: open failed: connect failed: Connection refused" with port forwarding
Sources
- ssh(1): -J, -L, -N
- ssh_config(5): ProxyJump, LocalForward, ExitOnForwardFailure, GatewayPorts
- sshd_config(5): AllowTcpForwarding, DisableForwarding, PermitOpen
- sshd(8): authorized_keys options no-port-forwarding and restrict
- RFC 4254, section 7.2: direct-tcpip channels
- OpenSSH channels.c: open failure reasons and bind errors