253

Referring back to this question, I am executing the below via OpenSSH (Client: Mac OS X 10.6 | Server: Linux Mint), however the port that is being tunneled is not working publicly:

ssh -R 8080:localhost:80 -N [email protected]
  • The purpose is so the local port can be opened on the remote computer
  • It seems as if the remote side binds only on localhost, instead of to all interfaces
  • It works when opening the port on localhost on the remote computer, but when trying to access the public IP of the remote computer from my local computer, the port doesn’t seem to be open

How would I make the tunnel public on the IP for anyone to access?

1
  • What does public IP mean? If you are trying to connect to a local computer thru the router and via the Internet, most routers will not allow such loopback.
    – harrymc
    Commented Apr 30, 2013 at 5:53

8 Answers 8

477
+50

If you check the man page for ssh, you'll find that the syntax for -R reads:

-R [bind_address:]port:host:hostport

When bind_address is omitted (as in your example), the port is bound on the loopback interface only. In order to make it bind to all interfaces, use

ssh -R \*:8080:localhost:80 -N [email protected]

or

ssh -R 0.0.0.0:8080:localhost:80 -N [email protected]

or

ssh -R "[::]:8080:localhost:80" -N [email protected]

The first version binds to all interfaces individually. The second version creates a general IPv4-only bind, which means that the port is accessible on all interfaces via IPv4. The third version is probably technically equivalent to the first, but again it creates only a single bind to ::, which means that the port is accessible via IPv6 natively and via IPv4 through IPv4-mapped IPv6 addresses (doesn't work on Windows, OpenBSD).  (You need the quotes because [::] could be interpreted as a glob otherwise.)

Note that if you use OpenSSH sshd server, the server's GatewayPorts option needs to be enabled (clientspecified, or, in rare cases, to yes) for this to work (check file /etc/ssh/sshd_config on the server). Otherwise (default value for this option is no), the server will always force the port to be bound on the loopback interface only.

21
  • 5
    Yeah, that's exactly why I always prefer 0.0.0.0 - it's IPv4 only, but it'll do most of the time :) Commented May 6, 2013 at 7:51
  • 1
    fe80::1 hahaha Commented May 6, 2013 at 7:54
  • 67
    GatewayPorts yes solved my problem.
    – Sunry
    Commented Oct 25, 2016 at 8:18
  • 4
    GatewayPorts = yes (on the remote sshd config) fixed it for me too
    – Phil_1984_
    Commented Dec 5, 2016 at 13:29
  • 3
    "GatewayPorts yes" made my day, thanks @StefanSeidel
    – karser
    Commented Oct 27, 2017 at 10:40
47

Edit:

-g works for local forwarded ports, but what you want is a reverse/remote forwarded port, which is different.

What you want is this.

Essentially, on example.com, set GatewayPorts=clientspecified in /etc/ssh/sshd_config.

--- previous (incorrect) answer ---

Use the -g option. From ssh's man page:

-g     Allows remote hosts to connect to local forwarded ports.
7
  • doesn't seem to be working... it starts up but i cant connect remotely Commented Apr 28, 2013 at 6:21
  • 2
    Try running netstat -elnpt from a separate tty to figure out what ports are bound to what address. Without -g, a port should be bound to 127.0.0.1:PORT. With -g, it should be bound to 0.0.0.0:PORT, which makes it accessible remotely.
    – snapshoe
    Commented Apr 28, 2013 at 14:58
  • pastebin.com/q6f4kJyd Commented Apr 28, 2013 at 16:11
  • 3
    GatewayPorts=clientspecified or GatewayPorts clientspecified Commented Apr 29, 2013 at 0:01
  • and do i add that to the client or remote? Commented Apr 29, 2013 at 0:04
19

Here's my answer for completion:

I ended up using ssh -R ... for tunneling, and using socat on top of that for redirecting network traffic to 127.0.0.1:

tunnel binded to 127.0.0.1: ssh -R mitm:9999:<my.ip>:8084 me@mitm

socat: mitm$ socat TCP-LISTEN:9090,fork TCP:127.0.0.1:9999

Other option is to do a local-only tunnel on top of that, but i find this much slower

mitm$ ssh -L<mitm.ip.address>:9090:localhost:9999 localhost

2
  • I like the fact that I don't have to deal with the sshd configuration and that I can do all of it without sudo. Plus I learn that socat exists. Thanks!
    – BrutusCat
    Commented Jun 12, 2014 at 14:00
  • 2
    +up you go good sir. I tried to tell ssh to bind to 0.0.0.0 without success.. then saw the * syntax, tried that, no dice. I imagine it may be some sort of security feature on sshd config or something that isn't allowing it. Finally saw this post and socat worked awesome. Super useful, putting this in my back pocket ;]
    – Jaime
    Commented Jun 24, 2019 at 23:45
17

You can also use a double forward if you won´t or can change /etc/ssh/sshd_config.

First forward to temporary port (e.g. 10080) on loopback device on the remote machine, then use local forward there to redirect port 10080 to 80 on all interfaces:

ssh -A -R 10080:localhost_or_machine_from:80 [email protected] "ssh -g -N -L 80:localhost:10080 localhost"
4
  • 4
    This actually works to bypass forwarding rules! Commented Dec 3, 2016 at 18:49
  • 1
    Love this solution. Great workaround when you don't want the change the config on the machine
    – Grezzo
    Commented Aug 18, 2017 at 14:54
  • It works, I have no idea WHY it works Commented Jan 30, 2022 at 2:20
  • if you got errors "Permission denied, please try again. Permission denied, please try again. usr@localhost: Permission denied (publickey,password)." like me, use this: stackoverflow.com/a/16651742/2051938
    – mixalbl4
    Commented Aug 31, 2023 at 9:48
8

Use the "gateway ports" option.

ssh -g -R REMOTE_PORT:HOST:PORT ...

In order to use that, you probably need to add "GatewayPorts yes" to your server's /etc/ssh/sshd_config.

1
  • Actually this worked. What I do is that I use an EC2 instance as a forwarder to my REST server. This way, I don't need to stick my server in the DMZ and I don't need a public IP. Funny enough, with the first EC2 instance I created, ssh -R remote_port:localhost:port xxx@ec2xxx worked just fine but then I had to create another instance later on for some reason and from that point on, I was always getting: connection refused. Used tcpdump to look at what I was getting and there wasn't much info. -g plus GatewayPorts yes did the trick.
    – E.T
    Commented Jan 24, 2017 at 19:12
4

I don't have enough reputation to comment at the appropriate sections, so I'm adding a new answer.

Warning: if you set GatewayPorts to yes this will make sshd bind your forwardings to any interface - regardless of the client configuration (-R, etc.). This can become quite a security issue if the client assumes she has limited her forwardings to f.e. localhost. Therefore, setting GatewayPorts to clientspecified is usually what you want.

3

Jump hosts are a fairly recent addition to OpenSSH. This requires SSH access to the intermediate, but should work without additional configuration.

ssh -J [email protected] remoteuser@localhost -p 8080

This command instructs SSH to first connect to [email protected], and then, from that machine, to initiate a connection to port 8080 at localhost (i.e., the port that is tunneled from the jump host to the remote host) under the remoteuser user name.

0

If you'd like to put the configuration in you ~/.ssh/config instead of using command line parameters, you can try something like

Host REMOTE_HOST_NAME RemoteForward \*:8080 127.0.0.1:80

Remember to have you remote host's firewall allow connections to 8080 and ensure that the GatewayPorts option of your /etc/ssh/sshd config isnt set to no

You must log in to answer this question.

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