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!





Random Man Pages:
sethostname
latin2
sane-epson
dup2

5 Comments »

Comment by John R

June 1, 2009 @ 5:46 am

Thanks for that – I was just reading the man pages trying to work out the psuedo console. You’ve really helped me get started with it!

Cheers.

Comment by Linuxlover

October 23, 2010 @ 9:38 am

what is ssh

Comment by Rob

May 24, 2011 @ 3:57 pm

Interesting post, I have everdone port forwarding is on the netgear router.

Comment by TheLinuxBlog.com

May 24, 2011 @ 4:05 pm

@rob – Yes, by opening port 22 on your router and letting openssh forward ports, you can tunnel any port through ssh.

Comment by Mythnick

May 9, 2012 @ 8:54 am

hello

can i ssh portforward two times?

soo i am in another country. i connect withh ssh to a server at home and after i am connected i can acces the internet like i where in my house.

but in my house i have another router in the netwrok that is connected to my neighbour’s internet. and threw the first router i can ssh to the second. but my internet ip is still my own, not my neighbours internet.

how can i double ssh tunnel port forwarding :D?

RSS feed for comments on this post. TrackBack URI

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>