How to Add SSH Key to VSC

November 16, 2023

Introduction

Visual Studio Code (VSC) allows users to set up a remote development environment using an extension that is a part of the default VSC installation. Using the SSH protocol, developers can connect to a remote system, such as a computer or a container/virtual machine.

This tutorial shows how to add an SSH key to VSC and access a remote environment without a password.

How to add SSH key to VSC.

Prerequisites

  • Command-line access to the remote host system.
  • SSH server installed on the host.
  • VS Code installed on the local system.

Step 1: Connect to Remote Host via SSH in VSC

Before adding an SSH key, connect to the remote system using a password. Follow the steps below to establish an SSH connection with a remote host in VSC:

1. Select the remote connection button in the bottom left corner of the VSC window. The remote connection search bar appears.

2. Type ssh in the search bar and select the SSH option from the search results.

Choosing an SSH connection in VSC.

3. Select Add New SSH Host.

Adding a new SSH host in VSC.

4. Type the SSH username and address you want to connect to and press Enter.

Providing SSH host details in VSC.

5. Select the SSH configuration file where the SSH connection information will be stored.

Selecting the SSH configuration file to update.

VSC creates a connection to the remote host.

6. Select Continue to confirm the remote host fingerprint.

Confirming the host fingerprint.

7. Type the password of the remote user and press Enter.

Typing the password to the remote host.

VSC connects and sets up the remote host by installing the VSC server. When the setup finishes, a new window opens for the remote environment. The IP address of the remote host is visible in the bottom-left part of the window.

An active SSH connection in VSC.

Step 2: Generate SSH Key Pair

Once you established an SSH connection, generate an SSH key on your local machine. You will use this key to authenticate with the host and avoid typing the password in the future.

To create an SSH key pair, open the terminal and type the following command.

ssh-keygen -t rsa -b 4096

The command creates a strong 4096-bit-long RSA key. Follow the prompts to complete the procedure. When asked for a passphrase, leave the field blank by pressing Enter.

Generating an SSH key pair in Ubuntu.

Step 3: Add SSH Key to Host in VSC

To enable public key authentication, copy the public SSH key from your local system to the host system. You can perform this action by typing the following command in your local terminal:

ssh-copy-id -i [path-to-ssh-key] [username]/[remote-host-ip-address]
copying the public ssh key from local system

Alternatively, follow the steps below to perform the same action in VSC:

1. Select Open Folder in the VSC remote connection window.

Opening folder in VSC via SSH.

2. In the directory tree view, find the .ssh directory of the remote user.

3. Return to the local VSC window and locate the public key you generated in the previous step.

4. Drag and drop the public key from the local machine to the .ssh directory on the remote system.

Copying file from the local machine to the SSH host.

5. Rename the file you copied from [ssh-key].pub to authorized_keys.

Renaming a file in VSC.

You can now establish an SSH session without a password.

Note: Double-check that the key was transferred correctly. You will not be able to access the remote system with an incorrect key and will be greeted with an error message, instead.
If that happens, check out our guide on how to fix the Permission denied (publickey,gssapi-keyex,gssapi-with-mic) error.

Conclusion

After reading this tutorial, you should be ready to set up a remote development environment in VSC and enable passwordless access with an SSH key pair. Using public key authentication with SSH allows for a seamless experience when developing software remotely.

Next, learn to use SSH to connect to a remote server in Linux or Windows.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How to Install VSC on Ubuntu
May 12, 2022

VSC supports an abundance of extensions acts as a version control system, thus facilitating project collaboration.
Read more
How to Use Docker for VSC
September 28, 2023

This article shows how to install the Docker VSC extension, create a container image, and run the Docker container in VSC.
Read more
Best Python IDEs And Code Editors
November 6, 2023

This article lists the best Python IDEs and code editors and defines the difference between IDEs and Code Editors.
Read more
16 Best Java IDEs
November 10, 2021

Java Integrated Development Environments (Java IDEs) are useful applications that combine software development tools...
Read more