Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh -L forward multiple ports

People also ask

Can SSH run on different ports?

By default, the SSH server still runs in port 22. However, there are occasions when it is run in a different port. Testing use is one reason. Running multiple configurations on the same host is another.

How do I SSH to a different port than 22?

To use ssh to port other than 22 on your server, you need to change specific parameters within the sshd_config file. First, you need to uncomment the line by removing the number-sign ( # ) and changing 22 to the new port number that you want to use. Next, save the changes and then exit the text editor. That's all.


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 ...


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.


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}

jbchichoko and yuval have given viable solutions. But jbchichoko's answer isn't a flexible answer as a function, and the opened tunnels by yuval's answer cannot be shut down by ctrl+c because it runs in the background. I give my solution below solving both the two flaws:

Defing a function in ~/.bashrc or ~/.zshrc:

# fsshmap multiple ports
function fsshmap() {
  echo -n "-L 1$1:127.0.0.1:$1 " > $HOME/sh/sshports.txt
  for ((i=($1+1);i<$2;i++))
  do
    echo -n "-L 1$i:127.0.0.1:$i " >> $HOME/sh/sshports.txt
  done
  line=$(head -n 1 $HOME/sh/sshports.txt)
  cline="ssh "$3" "$line
  echo $cline
  eval $cline
}

A example of running the function:

fsshmap 6000 6010 hostname

Result of this example:

You can access 127.0.0.1:16000~16009 the same as hostname:6000~6009


In my company both me and my team members need access to 3 ports of a non-reachable "target" server so I created a permanent tunnel (that is a tunnel that can run in background indefinitely, see params -f and -N) from a reachable server to the target one. On the command line of the reachable server I executed:

ssh root@reachableIP -f -N  -L *:8822:targetIP:22  -L *:9006:targetIP:9006  -L *:9100:targetIP:9100

I used user root but your own user will work. You will have to enter the password of the chosen user (even if you are already connected to the reachable server with that user).

Now port 8822 of the reachable machine corresponds to port 22 of the target one (for ssh/PuTTY/WinSCP) and ports 9006 and 9100 on the reachable machine correspond to the same ports of the target one (they host two web services in my case).