Let’s say there is a remote service – maybe a Postgres or MySQL database server – running on a network that doesn’t expose the service port. If you have SSH access to a host on that network – let’s call it jump – and jump can access the service, it is possible to create what is called an SSH tunnel through jump to the remote service. Here’s how.
Let’s use the example of accessing Postgres on its usual port 5432 on host pgsrv on the same network as jump. The following command opens port 5432 on localhost and tunnels traffic to that port to pgsrv via jump. We omit the fully qualified hostnames in this example for the sake of clarity.
$ ssh -f -N -L localhost:5432:pgsrv:5432 jump
Option -f tells ssh to go to background just before command execution. Option -N is used to select port forwarding only; no remote command is executed. Option -L localhost:5432:pgsrv:5432 specifies that port 5432 on bind address localhost should be forwarded to port 5432 on remote host pgsrv.
Now confirm the tunnel is working by configuring your local database client to connect to localhost:5432.