16

I have a SOURCE host which is remote and behind NAT. I want to connect to SOURCE via ssh from a TARGET host which is in my home network. Thus I issue the following command on SOURCE:

ssh -R 2222:localhost:22 TARGET -N

Now from TARGET I can connect to SOURCE via ssh -p 2222 localhost. Fine

With TABLET in the same network of TARGET I would expect this to work:

ssh -p 2222 TARGET

Instead it looks like TARGET only accept connections on port 2222 from localhost. The following is on TARGET:

user@TARGET:~/$ netstat -l | grep 2222
tcp        0      0 localhost:2222          *:*                     LISTEN     
tcp6       0      0 localhost:2222          [::]:*                  LISTEN 

Is there a way to make the remote side of a ssh -R accept connections from all its interfaces?

Jack
  • 525
  • 1
  • 5
  • 13

1 Answers1

24

By default, it will listen on localhost (loopback interface) only. You need to specify the bind_address as 0.0.0.0 in your command:

ssh -R 0.0.0.0:2222:localhost:22 TARGET -N

You need also to configure SSH daemon on target host to allow client to specify the bind_address. It is prohibited by default to listen to all interfaces. So, you will always find it listening on loopback even if you specifiy 0.0.0.0 as bind_address.

You need to have a line like the following in /etc/ssh/sshd_config to allow client to specify the bind address.

GatewayPorts clientspecified

When done, you can verify using netstat -lntp on target machine.

Khaled
  • 36,533
  • 8
  • 72
  • 99
  • Actually I already tried that, but with no success. Does this work for you? This is the output: `tcp 0 0 127.0.0.1:2222 0.0.0.0:* LISTEN - ` - `tcp6 0 0 ::1:2222 :::* LISTEN -` – Jack Jul 13 '17 at 08:16
  • Anyway I thought that the HOST in PORT:HOST:PORT was referring from caller side – Jack Jul 13 '17 at 08:18
  • @Jack: Sorry, my answer was not complete. I updated it! – Khaled Jul 13 '17 at 08:49
  • Fantastic, I didn't know about the HOST:PORT:HOST:PORT syntax. Very useful. Also sshd configuration was missing. Thanks a lot – Jack Jul 13 '17 at 09:11
  • Actually `0.0.0.0` at the beginning of a parameter can be omitted. This is a shortcut. It means that `-R :2222:localhost:22` will do the same. Please note and don't forget semicolon right before `2222`. – Victor Yarema Oct 23 '20 at 14:54
  • Thanks and if you want to listen on all interfaces (both IPv4 and IPv6), I read you have to use something like `ssh -R \*:2222:localhost:22 user@remote.server.ip.address`. Source: https://www.linuxwave.info/2020/08/ssh-tunnelling-to-bind-on-all-interfaces.html Maybe it has the same effect as @VictorYarema comment? Edit: in fact I did not need to listen on all interfaces, localhost was enough to use reverse VNC if I use local port forwarding on the server and remote port forwarding on the client. It have the advantage to use an end-to-end SSH tunnel to secure VNC. – baptx Mar 04 '21 at 15:02