This question already has an answer here:

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!

marked as duplicate by slm, Anthon, jasonwryan, derobert, terdon Feb 19 '14 at 19:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @slm Really? Neither "local" nor "remote" appears in the whole text and that is supposed to be a duplicate...? – Hauke Laging Feb 19 '14 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 '14 at 15:48
up vote 326 down vote accepted

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
  • 1
    Thank you for your detailed answers. But I still feel confused about the data flow. In example 1, someone connects to my computer and send a request to me, then I send the request to SUPERSERVER, then SUPERSERVER send me the response data and I send the data to someone? right? – user2886717 Feb 20 '14 at 2:23
  • 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 '14 at 14:02
  • great work! Thanks! – user2886717 Mar 10 '14 at 3:12
  • 32
    Excellent! These sketches communicate way more than the 10+ blog posts I just read, and much faster. – devth Oct 7 '15 at 15:01
  • 1
    @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 '17 at 1:16

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 or ask your own question.