ssh -L forward multiple ports

Exactly what NaN answered, you specify multiple -L arguments. I do this all the time. Here is an example of multi port forwarding:

ssh remote-host -L 8822:REMOTE_IP_1:22 -L 9922:REMOTE_IP_2:22

Note: This is same as -L localhost:8822:REMOTE_IP_1:22 if you don't specify localhost.

Now with this, you can now (from another terminal) do:

ssh localhost -p 8822

to connect to REMOTE_IP_1 on port 22

and similarly

ssh localhost -p 9922

to connect to REMOTE_IP_2 on port 22

Of course, there is nothing stopping you from wrapping this into a script or automate it if you have many different host/ports to forward and to certain specific ones.

Hope this helps.


The -L option can be specified multiple times within the same command. Every time with different ports. I.e. ssh -L localPort0:ip:remotePort0 -L localPort1:ip:remotePort1 ...


For people who are forwarding multiple port through the same host can setup something like this in their ~/.ssh/config

Host all-port-forwards Hostname 10.122.0.3 User username LocalForward PORT_1 IP:PORT_1 LocalForward PORT_2 IP:PORT_2 LocalForward PORT_3 IP:PORT_3 LocalForward PORT_4 IP:PORT_4

and it becomes a simple ssh all-port-forwards away.


You can use the following bash function (just add it to your ~/.bashrc):

function pfwd {
  for i in ${@:2}
  do
    echo Forwarding port $i
    ssh -N -L $i:localhost:$i $1 &
  done  
}

Usage example:

pfwd hostname {6000..6009}