👤

Client Setting (SSH Tunnel)

You may need to connect to a more powerful server or workstation to run your code. However, sometimes the server is not in your reach or you are not connecting within the same domain. Generally, the server can be accessed directly if it has a public IP address. Otherwise, you will need to connect via a VPN, which is safe but pretty annoying.

In this post, you can find how to connect your server without VPN but using SSH tunnel (given the server has been set up with the tunneling mechanism).

Use SSH to connect

Install Cloudflared via the URL below:

https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/

Make sure you have SSH installed on your client PC (any SSH product would be find, e.g., OpenSSH).

Then, you can add a new SSH configuration (Suppose the hostname is “tunnel.markmeng.com”, and the SSH configuration is named “gpu-server”):

Host gpu-server
  ProxyCommand cloudflared access ssh --hostname tunnel.markmeng.com

Now you can initiate a connection by typing in your username and password:

ssh <username>@gpu-server

Use public key to authenticate

Using a public key to authenticate the server connection can get rid of typing in password for every connection. Sometime, using public key is set by default and mandatory for security purposes. To do this, you will need to generate an SSH key pair unless you already have one.

Follow the instruction on GitHub to generate a key pair for your own PC (i.e. client):

https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent

After this step, you shoud have a key pair in your “.ssh” directory. Suppose your key is named “rsa_id”, the file named “rsa_id” is the private key, and the file named “rsa_id.pub” is the public key. While you should never delete them, you must keep your private key safe and not exposed to anyone.

Next, you need to add your public key to the server’s authorised key list.

First, on the terminal of your own PC, you need to upload your public key file to the server (e.g., the “Downloads” folder). You can use “scp” command to do so:

scp <path_of_your_public_key_file> <username>@gpu-server:Downloads

Next, log into the server (e.g., by SSH), and add the public key to the authorized key file:

cat <your_public_key_file_path_on_the_server> >> ~/.ssh/authorized_keys

After that, you can use public key to connect to the server without typing in your password by updating the SSH configuration:

Host gpu-server
  ProxyCommand cloudflared access ssh --hostname tunnel.markmeng.com
  IdentityFile <path_of_your_private_key_file_on_your_own_pc>

Establish development environment on local VS Code

You can of course write your code and run it on your local IDE, exactly like what you did on your local machine before. VS Code is the most ideal IDE as there are many great extensions available to install.

To enable development on local VS Code, you need to download two extensions, namely Remote Development, and Cloudflare Tunnel.

After installing these extensions, you can find the remote development tab on the left panel, and then you need to modify profile settings in Remote development tab.

You can add a connection configuration like below (assume the host is “tunnel.markmeng.com” and the username is “mark”):

Host gpu-server
ProxyCommand cloudflared access ssh --hostname tunnel.markmeng.com
User mark

If you have already upload your public key to the destination server and add it to the authorized keys, you can also configure your connection as below to avoid entering password during each logging in.

Host gpu-server-ssh
ProxyCommand cloudflared access ssh --hostname tunnel.markmeng.com
IdentityFile <path_of_your_private_key_file_on_your_own_pc>