Last Updated: February 25, 2016
·
3.883K
· sheerun

Allow for selecting interface when port forwarding

According to sshd_config, the GatewayPorts option:

Specifies whether remote hosts are allowed to connect to ports forwarded for the client. By default, sshd(8) binds remote port forwardings to the loopback address. This prevents other remote hosts from connecting to forwarded ports. GatewayPorts can be used to specify that sshd should allow remote port forwardings to bind to non-loopback addresses, thus allowing other hosts to connect. The argument may be “no” to force remote port forwardings to be available to the local host only, “yes” to force remote port forwardings to bind to the wildcard address, or “clientspecified” to allow the client to select the address to which the forwarding is bound. The default is “no”.

So to enable selecting binding interface for port forwarding it's enough to add following ling to sshd_config:

GatewayPorts clientspecified

And restart sshd:

sudo restart ssh

Now you can forward port on local interface:

ssh user@example.com -R 127.0.0.1:8080:google.pl:80

Or on Internet-connected one:

ssh user@example.com -R 0.0.0.0:8080:google.pl:80

Bonus: my step by step understanding how to set up SSH tunneling