Connect to your remote servers from Visual Studio Code

Sujay Pillai
4 min readJun 19, 2019

--

VS Code Remote Development is one of the latest feature released with Version 1.35. It allows you to use a container, remote machine, or the Windows Subsystem for Linux (WSL) as a full-featured development environment.

In this article I will show you how to connect to a remote machine/VM using SSH from VS Code using the Remote — SSH extension.

Image Credit — https://github.com/Microsoft/vscode-docs/blob/master/docs/remote/remote-overview.md

I have couple of servers spinned up on Linux Academy playground as shown below and we will be connecting to sujayopillai2c.mylabserver.com server from VS Code. Visual Studio Code uses SSH configuration files and requires SSH key based authentication to connect to your host.

Remote VM’s running on Linux Academy Playground

Installing the extension

You may install the Remote Development extension pack (this would install all 3 extensions — remote-ssh, remote-containers, remote-wsl) OR each individually as per your convenience.

Remote Development extension pack
Search for Remote — SSH for installing only this extension

Configuring key based authentication

Using SSH public-key authentication to connect to a remote system is a robust, more secure alternative to logging in with an account password or passphrase. SSH public-key authentication relies on asymmetric cryptographic algorithms that generate a pair of separate keys (a key pair), one “private” and the other “public”.

A private key, is usually named id_rsa. The private key is stored on your local computer and should be kept secure, with permissions set so that no other users on your computer can read the file.

A public key, is usually named id_rsa.pub. The public key is placed on the server you intend to log in to. You can freely share your public key with others. If someone else adds your public key to their server, you will be able to log in to that server.

Create a new key pair :

$ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rsa_linuxacademy

Upload your public key to the server (sujayopillai2c.mylabserver.com in this case) to which the connection has to be established

$ssh-copy-id -f -i ~/.ssh/id_rsa_linuxacademy.pub cloud_user@sujayopillai2c.mylabserver.com

Test whether you can login to the server with your key.

ssh -i ~/.ssh/id_rsa_linuxacademy cloud_user@sujayopillai2c.mylabserver.com

If you are successful in login to the server your local public SSH key is added to ~/.ssh/authorized_keys on the host.

Connect to a remote host

Navigate to the Command Palette > Search for ‘Remote-SSH: Connect to Host…’

Configure the list of hosts you want to connect from within VS Code by navigating to Command Palette > Remote-SSH: Open Configuration File… > Configure SSH Hosts and add the host to the file (/Users/sujay/.ssh/config) using the SSH config file format.

List of hosts available for connection depending on the entries in ssh config file

Once you have the file saved you may see the name of the server on the Connections pane. Right click on the appropriate hostname and click on any one of the option available (Connect to Host in New Window in this case)

Terminal attached to the remote host
Remote folders accessible directly with in VS Code

--

--