Notes

SSH Tunneling / Port Forwarding

local port forwarding with ssh -L through a jump host, what -f, -N and -L actually do, and how to close the tunnel

Edit on GitHub


Linux
1ssh -f -N -L 2222:vortex.myserver.com:22 user@jumphost
  • -f go to the background (just before running the command, so you can still type a password first). It does NOT mean force
  • -L local port forward, in the form local_port:destination_host:destination_port
  • -N don’t execute any commands at remote host, just forward the port
  • 2222 is local port
  • vortex.myserver.com is remote server, the one you actually want to reach
  • 22 is remote port
  • user@jumphost is the server you SSH into. The connection to vortex.myserver.com:22 is made from there, so vortex only has to be reachable from the jump host, not from you
  • ssh -L 2222:vortex.myserver.com:22 localhost only works if you run an SSH server on your own machine, because then localhost is the server you log into

Now connect to the local port, and you end up on vortex:

1ssh -p 2222 user@localhost

It looks like you have connected to yourself, but what you have done is you’ve tunneled your connection on the 2222 local port. Now you have a gateway to a remote network.

To close the tunnel, find the background ssh process and kill it

1pgrep -af 'ssh -f -N -L 2222'
2kill <pid>

Related

Browse all topics