I recently ran into this post: https://hackertarget.com/ssh-examples-tunnels/ It explains a lot about ssh connections and port forwarding, have a look, you will learn a lot :)
Here is the bit that is relevant to you:
SSH Tunnel (port forward)
In its simplest form an SSH tunnel simply opens a port on your local system that connects through to another port at the other end of the tunnel.
localhost:~$ ssh -L 9999:127.0.0.1:80 user@remoteserver
Lets break down the -L parameter. Think of -L as the Local listening side. So in our example above the port 9999 is listening on localhost and port forwards through to port 80 on remoteserver, note that the 127.0.0.1 refers to localhost on the remote server!
Lets take it up a notch. In this following example the port that is listening can be connected to from other hosts on the local network.
localhost:~$ ssh -L 0.0.0.0:9999:127.0.0.1:80 user@remoteserver
In these examples the port we are connecting is a listening web server. It could also be a proxy server or any other TCP service.
Now, the above will open an active SSH shell for you, and, as we only want to forward the port this is unwanted. You can add the -N argument to the command to skip executing any remote commands (the opened shell is a command too). The
ssh man page explains this as well:
-N Do not execute a remote command. This is useful for just forwarding ports.
So, the following script will open your port until you ctrl-c/kill it:
echo "Forwarding ports"
ssh -N -L 0.0.0.0:9999:127.0.0.1:80 user@remoteserver
You can also open a port for a few seconds by executing a sleep command and removing the -N argument:
ssh -L 0.0.0.0:9999:127.0.0.1:80 user@remoteserver "sleep 10"
These SSH tunnels have their pitfalls. If you want to keep the forwarded port open for a long time, you might want to check out the following thread, specifically the bit about