300

I feel confused about ssh port forwarding and the difference between ssh local and remote port forwarding. Could you please explain them in detail and with examples? Thanks!

2
  • 2
    @slm Really? Neither "local" nor "remote" appears in the whole text and that is supposed to be a duplicate...? Feb 19, 2014 at 15:42
  • @HaukeLaging - thanks I linked the wrong one, this is the one I meant. How does reverse SSH tunneling work?. Sorry about that.
    – slm
    Feb 19, 2014 at 15:48

2 Answers 2

755

I have drawn some sketches

ssh tunnel starting from local


ssh tunnel starting from remote

Introduction

  1. local: -L Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.

    ssh -L sourcePort:forwardToHost:onPort connectToHost means: connect with ssh to connectToHost, and forward all connection attempts to the local sourcePort to port onPort on the machine called forwardToHost, which can be reached from the connectToHost machine.

  2. remote: -R Specifies that the given port on the remote (server) host is to be forwarded to the given host and port on the local side.

    ssh -R sourcePort:forwardToHost:onPort connectToHost means: connect with ssh to connectToHost, and forward all connection attempts to the remote sourcePort to port onPort on the machine called forwardToHost, which can be reached from your local machine.

Examples

Example for 1

ssh -L 80:localhost:80 SUPERSERVER

You specify that a connection made to the local port 80 is to be forwarded to port 80 on SUPERSERVER. That means if someone connects to your computer with a webbrowser, he gets the response of the webserver running on SUPERSERVER. You, on your local machine, have no webserver running.

Example for 2

ssh -R 80:localhost:80 tinyserver

You specify, that a connection made to the port 80 of tinyserver is to be forwarded to port 80 on your local machine. That means if someone connects to the small and slow server with a webbrowser, he gets the response of the webserver running on your local machine. The tinyserver, which has not enough diskspace for the big website, has no webserver running. But people connecting to tinyserver think so.

More examples

Other things could be: The powerful machine has five webservers running on five different ports. If a user connects to one of the five tinyservers at port 80 with his webbrowser, the request is redirected to the corresponding webserver running on the powerful machine. That would be

ssh -R 80:localhost:30180 tinyserver1
ssh -R 80:localhost:30280 tinyserver2
etc.

Or maybe your machine is only the connection between the powerful and the small servers. Then it would be (for one of the tinyservers that play to have their own webservers):

ssh -R 80:SUPERSERVER:30180 tinyserver1
ssh -R 80:SUPERSERVER:30280 tinyserver2
etc
21
  • 3
    The request is sent to your port 80. Your ssh is listening on that port and takes the data (the request) and moves it secretly through your ssh connection to the SUPERSERVER’s sshd. The SUPERSERVER sshd resends this data (request) to port 80 of localhost (which is SUPERSERVER, because localhost refers to the local machine). Then all the way back. Maybe I should add a drawing.
    – erik
    Feb 20, 2014 at 14:02
  • 107
    Excellent! These sketches communicate way more than the 10+ blog posts I just read, and much faster.
    – devth
    Oct 7, 2015 at 15:01
  • 2
    So when someone is behind a firewall that forbids access to, say, bbc.co.uk, they can create a remote port forward to bypass this restriction, right? This is a nice answer but I feel it could be improved by stating the problems it tries to solve Nov 8, 2016 at 13:44
  • 2
    So calling SUPERSERVER$ ssh -R 80:SUPERSERVER:30180 tinyserver is in fact equal to calling tinyserver$ ssh -L 80:localhost:30180 SUPERSERVER I mean it achieves the same thing - all connections to tinyserver:80 will be forwarded to SUPERSERVER:30180, right?
    – redacted
    May 31, 2017 at 8:33
  • 2
    @RobinNemeth: Yes, both local and remote forwarding achive the same if started from the remote or local machine. But your first command could be even easier, because if you start it from SUPERSERVER you can just reference to it’s localhost (the localhost of SUPERSERVER). I.e. SUPERSERVER$ ssh -R 80:localhost:30180 tinyserver (my third image).
    – erik
    Jun 6, 2017 at 1:16
32

Local Port forwarding

ssh creates an additional local port which it will forward to a port on the remote system.

example

ssh -L 8080:127.0.0.1:80 user@webserver

Then in your browser on local use URL http://localhost:8080/

it will connect to local machines port 8080, which ssh will forward on to remote ssh, and it will then make a request to 127.0.0.1:80. Note 127.0.0.1 is actually the remote server's localhost, but it could have been a host/IP available at the remote machine's network.

Remote forward

Asks ssh to create a listening port on the remote machine which it will forward back (Reverse) to the local ssh to forward on.

ssh -R 10123:127.0.0.1:123 user@webserver

So, after ssh connects to webserver, the remote ssh creates and lsitens on a port 10123. A process on webserver connecting to 10123, ssh will pick it up and send it back to the local machine's ssh, which sends it on to 127.0.01:123 port.

Not the answer you're looking for? Browse other questions tagged .