Tunneling Web Request to Your Local Machine

Published 2019-10-07

Sometimes the easiest way to test or debug a service is to have it running on your local machine. Oftentimes however, your service may be handling requests from an external system that you cannot run locally. Since your local machine is not usually accessible from the public internet, the easies thing to do is route requests from some public facing server to your local machine.

In the past I have done this using ngrok. However, I recently needed a solution that I could host myself. I couldn’t run my test traffic through an external company’s infrastructure. After some reading, I was pleasantly surprised that this can be very easily achieved using good old SSH.

How to Tunnel Using SSH/SSHD

The following instructions assume you have a Debian Virtual Machine with a public facing IP address. The goal is to route any requests sent to port 5000 on the virtual machine to port 8080 on your local machine.

  • On the VM: edit [/etc/ssh/sshd_config] and replace [#GatewayPorts no] with [GatewayPorts yes] (I added the brackets for clarity, they are not included in the file)
  • On the VM: restart SSHD with [systemctl restart ssh.service]
  • On your local machine: Run [ssh -N -R 5000:localhost:8080 <your connection details>]

You should now be able to see the same thing if you make a request to [localhost:8080] or [vm_ip_address:5000]. Pretty neat and useful!

comments powered by Disqus