Linux Blog

Forwarding Ports over an active SSH connection

Filed under: General Linux,Linux Software,Quick Linux Tutorials — TheLinuxBlog.com at 12:01 am on Tuesday, October 7, 2008

There is no doubt about it that SSH is a handy administration and remote access tool. Have you ever wanted to add port forwarding or cancel port forwarding from within an active SSH connection? Well, a feature that many do not know about is the ssh escape feature. With this pseudo terminal you can do some nifty things, such as forward ports over an active SSH connection.

To do this, follow these instructions:

Open up the ssh pseudo terminal control with the ssh escape function and open up a command line as follows:

[user@TheLinuxBlog.Com ~]$ ~C
ssh> help
Commands:
-L[bind_address:]port:host:hostport    Request local forward
-R[bind_address:]port:host:hostport    Request remote forward
-KR[bind_address:]port                 Cancel remote forward

As you can see from the ssh command line that the syntax is very similar to if you were forwarding a port before making the ssh connection. I normally do a local forward so I most often use the -L [port]:[host][host] port] syntax. You will need to issue the ~C again to get back into ssh command mode once you’ve viewed the help to actually issue your forwarded port.

So an actual local port forward over an active SSH connection would look like this:

[user@TheLinuxBlog.Com ~]$ ~C
ssh> -L 8080:192.168.0.10:80

The above opens port 8080 locally (if allowed) and forwards it to the 192.168.0.10 host on port 80.

Hope this helps, happy forwarding!