SSH Port Forwarding – Comprehensive Guide

port-forwardingssh

I want to connect to my computer(local) behind NAT through a public accessible server(public).

On local:

ssh -g -R 8000:localhost:22 user@public

Then on public:

ssh -p 8000 user@public 

But I am getting error: Connection refused.

When I login to the public server, I can verify that the tunnel is working by:

ssh -p 8000 localhost

Which opens ssh on the local computer.

Am I suspecting something wrong that the public server should act as transparent proxy? Or how to make it work like that.

Best Answer

SSH remote port forwards will default binding to localhost/loopback for security purposes. It's not often preferable to allow other hosts access to your forwarded ports.

To override this behaviour you will need to do two things:

  • Enable the GatewayPorts option on the server.
  • Specify a bind address, or * to bind to all addresses, on the client.

    ssh -R \*:8000:localhost:22 user@public
    

Escape the asterisk to ensure that your shell doesn't expand it.

Related Topic