1

The problem that I have is trying to understand the use of port forwarding with the ssh command as shown below. The confusion is based on the use of localhost where my machine is localhost but it appears that it may be referencing the remote server. Because of this it is unclear as to what is going on and so I am looking for information and or links to documents that can clarify what is occurring within the ssh command below.

ssh -f myUserID@someip -L [bind_address:]port:host:hostport
  • The details for the above taken from a manual: Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side.

With regards to the ssh command, I'm told the left side is considered the local (client).

So with a syntax of the following

ssh -f myUserID@someip -L 1234:localhost:9200

where the left side does not contain an IP is localhost assumed to then be at at port 1234?

And I have no idea how to interpret the 2nd half of localhost:9200?

How should I be interpreting the above ssh command?

[ bind_address:]port:host:hostport Specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side. This works by allocating a socket to listen to port on the local side, optionally bound to the specified bind_address. Whenever a connection is made to this port, the connection is forwarded over the secure channel, and a connection is made to host port hostport from the remote machine. Port forwardings can also be specified in the configuration file. IPv6 addresses can be specified with an alternative syntax: [bind_address/]port/host/hostport or by enclosing the address in square brackets. Only the superuser can forward privileged ports. By default, the local port is bound in accordance with the GatewayPorts setting. However, an explicit bind_address may be used to bind the connection to a specific address. The bind_address of ''localhost'' indicates that the listening port be bound for local use only, while an empty address or '*' indicates that the port should be available from all interfaces.

I need clarity on the format I'm posting. Because the way the definition reads and the way the command is that I'm posting it sounds like I'm port forwarding a localhost 1234 to the localhost 9200, which makes zero sense as to how it is allowing me to communicate to the remote server.

  • -f and -L options are fairly explained in ssh's man page. linux.die.net/man/1/ssh – zee Dec 28 '17 at 18:26
  • I don't think you read the question. I'm aware of -f and its the -L that is throwing me. – Elijah Dec 28 '17 at 18:36
  • What is it binding the localhost of the terminal that I just SSH into to the localhost of the client? If so that is not clear at all because localhost is the machine your on. If i'm in the terminal of the ssh then I would think that localhost is now the ssh machine and not the local machine I'm using to ssh from. – Elijah Dec 28 '17 at 18:40
  • I did NOT down vote your question, I was merely making sure that you have read the man page :) – zee Dec 28 '17 at 18:42
  • The others did manage to clarify it as the man does not cover the confusing aspect of referring to 'localhost' from the perspective of the remote system. Glad this is cleared up as it was driving me crazy with localhost to localhost. – Elijah Dec 28 '17 at 19:15
0

I just googled quickly and fould this ...

https://www.howtogeek.com/168145/how-to-use-ssh-tunneling/

Please have a read through this and go to the part:

"Remote Port Forwarding: Make Local Resources Accessible on a Remote System"

This is exactly what your command is doing.

Maybe google a bit more next time. All I had to put into the search box was ssh "-L" so answers with -L will come back.

best regards

bob

  • Though your response comes off as arrogant, I up-voted because the link you provided was very descriptive and I appreciate the link as it clearly explained the confusing point of the use of 'localhost'. Thank you. – Elijah Dec 28 '17 at 19:13
3

-L1234:localhost:9200

  • -L : perform local forwarding (forwarding from the local client to the remote)
  • 1234 : listen on tcp/1234 on the local machine; the one from which you are executing ssh
  • localhost:9200 : the destination to which to forward traffic. The destination is described from the perspective of the target machine ("someip" in your example). So localhost here means someip, and 9200 means tcp/9200 on that remote machine.

Your local machine will accept connections on port 1234 and tunnel that traffic to port 9200 on the remote host.


Note that the the destination can be any machine which is reachable from the remote, not only localhost from the perspective of the remote. So if you wanted to port forward to a machine reachable from someip but not (directly) from your local machine, you might use an argument such as

-L1234:otherip:9200
  • Thank you for clarifying the localhost, it was exactly what I needed for clarity. – Elijah Dec 28 '17 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.