This blog post is primarily for my own reference, to avoid having to dig through the manual to look up the occasional edge case.
How to use SSH to do port forwarding. These assume you're on a Unix-like system (Linux or OS X) and not using some lame Windows client like PuTTy.
ssh -R 9022:localhost:22 remotehost.comThis will open port
9022
on remotehost.com (loopback only; you can only connect to 9022 from the local remotehost.com, not from elsewhere on the internet), and forward it to "localhost:22
", where "localhost" refers to your computer at the office, and 22 is of course the SSH port.
By default the remote host only would make port 9022 available on the loopback address, so from your home PC you can do ssh -p 9022 localhost
and connect to it, but you can't do e.g. ssh -p 9022 remotehost.com
and connect to it from somewhere else on the Internet.
To open the port on all interfaces (thus making it available on the internet too):
ssh -R *:9022:localhost:22 remotehost.comReplace the
*
with any other bind address if you want.
ssh -L 8080:localhost:80 remotehost.comHere,
8080
is opened on your office computer, for the loopback interface only, and localhost:80
refers to port 80 on the remote (home) computer. It's the reverse of ssh -R
.
Then you open Firefox and go to http://localhost:8080/
and yer in.
Another example: you have a VNC (remote desktop) server running on remotehost.com, but the VNC protocol itself is insecure, and you don't want your password being sent across the network in clear text to log in. So, you need your VNC traffic to be encrypted via SSH.
Here, remotehost.com is listening on port 5900
(the VNC port). You want to open a port on your local computer to the same number, so that you connect a VNC client to "localhost:5900" and it really connects you to "remotehost.com:5900" over a secure SSH tunnel:
ssh -L 5900:localhost:5900 remotehost.comThen with your VNC client, just connect it to "localhost".
ssh -L 5900:10.10.1.101:5900 remotehost.comHere "remotehost.com" goes to the main PC which I can access.
This opens up a listening port 5900 on my local (office PC) -- the first 5900 in the command -- and if I connect to it, it will use remotehost.com as a jumping off point to connect onward to 10.10.1.101:5900 (the laptop with a private LAN IP address on the remote network).
Then I point my VNC client at "localhost" and I end up with remote desktop on the laptop.
ssh -D 8080 remotehost.comNow you can configure your programs (e.g. Pidgin, Firefox) to use a SOCKS 5 proxy and have them connect to
localhost:8080
. All their internet traffic will be routed through the SSH tunnel to remotehost.com, secured, and then enter the cloud from there.Additionally, this can be used to reach other devices on the remote server's LAN that you otherwise couldn't get to. For example, turn on your proxy settings in Firefox and you can navigate to http://192.168.1.1/ to log into the router from the remote LAN (as opposed to a router on your local LAN). The SOCKS 5 proxy would cause Firefox (or any other app configured to use it) to use "remotehost.com" as a jumping off point into the internet, so it can connect to other local network devices on its end just the same.
There are 5 comments on this page. Add yours.
nice summary. thank you
Very helpful and also the use of "-D" is cool
I've tried the above, but I seem to not do something right. I've got a remote machine that I know its IP/domain and I can SSH into it. On my local computer I've got a local webserver. Is it possible to expose the local webserver on the remote domain?
Local computer forwards a port to remote. Remote exposes a port to forward the local port (which is forwarded from the local).
So http://localhost:8080/ on my local computer can be accessed from, say http://my-domain.com:9090/
Hi Buddy, I tried your "Penetrating" the Remote Network tutorial. I can't seem to make it work. Here's my setup:
Computer-A - Windows7 with putty.exe Computer-B - ssh server (linux) (124.xxx.xxx.xx) Computer-C - linux connected to server via ssh (10.95.143.22)
computer-C has VNC server running on port 5905. I wanted to VNC computer-C using my computer-A. computer-A can connect to computer-B via ssh. here is what I did
putty.exe -L 5905:10.95.143.22:5905 124.xxx.xxx.xx
I was asked for computer-B user & password, i logged in
then I open vnc viewer from computer-A and tried to access localhost:5905. i got an error "the connection closed unexpectedly".
do you have any ideas to fix this?
Hey this is an awesome summery of what you can do with SSH. Thanks for this!
0.0118s
.