Connect HPC in Visual Studio Code Using SSH

Keren Xu

2020/05/24

This note is about how to connect USC HPC in Visual Studio Code using SSH in windows setting.

VS Code already has a great post on remote connecting using SSH here. First step is to get the Visual Studio Code Remote - SSH extension, which help us to open folders on remote hosts with a running SSH server. We can interact with files and folders in VS Code environment.

The most confusing step is configure key authentication.

SSH public key authentication is a convenient, high security authentication method that combines a local “private” key with a “public” key that you associate with your user account on an SSH host. This section will walk you through how to generate these keys and add them to a host.

We can create a private key and a public key by using following codes:

$USER_AT_HOST="user@[hpc-login3.usc.edu](http://hpc-login3.usc.edu/)"

$PUBKEYPATH="$HOME\.ssh\id_rsa.pub"

$pubKey=(Get-Content "$PUBKEYPATH" | Out-String); ssh "$USER_AT_HOST" "mkdir -p ~/.ssh && chmod 700 ~/.ssh && echo '${pubKey}' >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"

Before running these codes, check if your “private key” is in $HOME\.ssh and it is called id_rsa.pub. The “public key” on the remote host is ~/.ssh/authorized_keys.

Since USC hpc requires two-factor authentication. VS Code should prompt you to enter password and DUO PUSH. When you did not see the prompt, enable remote.SSH.showLoginTerminal setting in VS Code. This option helps you see the terminal while connecting remotely so that you can complete two-factor authentication in terminal, otherwise terminal won’t appear. Also select Linux when VS Code is asking what the environment is on your remote host.
drawing

After connecting to HPC, HPC address will appear on the Status bar. You can always refer to here to see which host you are connected to.
drawing

Remote x11 extension can help enable X11 forwarding, but currently does not support two-factor authentication.