How to use API in remote server when you cannot use server_ip directly

Background

There is a service in remote server (for example, running in 0.0.0.0:8001)

You want to use this api, however, you cannot use server_ip:port to connect directly.

Method

Plan A: SSH Tunnel

Assume:

  • You want to access this API on your local machine via port 8001 as well
ssh -L 8001:localhost:8001 user@remote_gpu_machine

After running this command, any request you make to localhost:8001 on your local machine will be forwarded through the SSH tunnel to localhost:5000 on your remote server

Plan B: VSCode Tunnel

Developing with Remote Tunnels

Install VSCode cli in remote server:

curl -L -o vscode-cli.tar.gz "https://code.visualstudio.com/sha/download?build=stable&os=cli-alpine-x64"
tar -xzf vscode-cli.tar.gz

Run VSCode tunnel:

./code tunnel

In the progress, you need to authenticate(login with your Github account)

Also you need to login with the same Github account in your local VSCode

And you can connect to the tunnel with VScode:

image

In the end, set the port forward and set it pulic:

image

So you can use the forwarded address(same as localhost:8001 in remote server)

Plan C: Microsoft Azure Dev Tunnel

Create and host a tunnel - Microsoft dev tunnels | Microsoft Learn

Install Dev Tunnels CLI in remote server:

curl -sL https://aka.ms/DevTunnelCliInstall | bash

Login:

devtunnel user login

Run the tunnel to make localhost:8001 public:

devtunnel host -p 8001

You can also add --allow-anonymous to make this public, or only the creater can get access to it.

And a public link like https://<tunnel_id>.region.devtunnels.ms:8001/ will be shown.

ngrok

so easy, just one command you can have a public share link of your local service.

Other Plans

Other Tunnel is also available, this article is just a intro, have fun!