How to Add SSH Key to Visual Studio Code

Adding an SSH key to Visual Studio Code (VS Code) is a fundamental skill for developers particularly when managing remote repositories and securing your Git operations. This allows developers to work on projects hosted on remote machines directly from their local VS Code environment.

In this article, we will guide you through the process of generating an SSH key pair adding it to your SSH agent, and configuring VS Code to use it for remote development.

Getting Started

Visual Studio Code (VS Code) is one of the most popular code editors today, known for its flexibility extensive extensions marketplace, and user-friendly interface. When working with version control systems like Git securing your connections to remote repositories is important. One effective way to achieve this is by using SSH keys.

Understanding SSH Keys

SSH (Secure Shell) keys are a pair of cryptographic keys used to authenticate a user’s identity in the digital world. They are commonly used for secure access to remote servers and repositories. An SSH key pair consists of:

  1. Public Key: This key is added to the remote system for example GitHub and GitLab and is shared publicly. It is used to verify the identity of the private key holder.
  2. Private Key: This key remains on your local machine and is never shared. It should be kept secure as it allows access to the systems where the corresponding public key is registered.

Prerequisites

Before adding an SSH key to Visual Studio Code ensure you have the following prerequisites:

  1. Make sure Git is installed on your local machine. If you are a Windows user refer to our guide on installing Git on Windows system.
  2. If you have not already, install Visual Studio Code on Windows system.
  3. If you don’t have an SSH key pair yet you will need to generate one.

Generate SSH Key

Here’s how you can generate an SSH key for Windows, Linux, and Mac operating systems:

Windows

Launch Git Bash from the Start menu. Run the following command to generate an SSH key:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

Replace “[email protected]” with your email address. This command generates a new SSH key using the RSA algorithm with a 4096-bit key length.

You will be prompted to choose a file location to save the key. Press Enter to accept the default location which is C:\Users\your_user_name.ssh\id_rsa.

ssh Git

You can add an extra layer of security by entering a passphrase or press Enter to skip this step.

Linux and macOS

Launch Terminal from the Applications or Utilities folder. Run the following command to generate the key:

ssh-keygen -t rsa -b 4096 -C "[email protected]"

Press Enter to accept the default file location. Optionally, add a passphrase for added security.

ssh Linux

Add SSH Key to SSH Agent

Once you have generated the SSH key, the next step is to add it to the SSH agent, which manages your SSH keys and passphrases.

Windows

Open Git Bash and run the following command to start the SSH agent:

eval $(ssh-agent -s)
ssh agent git

Add your SSH private key to the SSH agent by running:

ssh-add ~/.ssh/id_rsa
ssh add key

Linux and macOS

Open Terminal and run the following command:

eval "$(ssh-agent -s)"
ssh agent linux

Add the SSH private key to the SSH agent:

ssh-add ~/.ssh/id_rsa

Add SSH Key to Git Hosting Services

Now that your SSH key is generated and added to the SSH agent, you need to add the public key to your Git hosting service for example GitHub, GitLab, and Bitbucket. Here we discuss two of them:

GitHub

Copy your SSH public key to the clipboard. You can use the following command in Git Bash or Terminal:

cat ~/.ssh/id_rsa.pub

Select and copy the output.

ssh public key

Go to GitHub’s website and log in to your account. In the top-right corner, click on your profile picture and select “Settings”.

In the left sidebar, click “SSH and GPG keys”. Click “New SSH key”, enter a title, and paste your public key into the “Key” field. Click the “Add SSH key” to save it.

add SSH key GitHub

GitLab

Use the same command to copy your SSH public key:

cat ~/.ssh/id_rsa.pub

Go to GitLab’s website and log in to your account. Click on your profile picture in the top-right corner and select “Settings”.

In the left sidebar, click “SSH Keys”. Paste your public key into the “Key” field and click “Add key”.

Configure Visual Studio Code

With your SSH key set up and added to your Git hosting service, it’s time to add SSH key Visual Studio code.

Launch Visual Studio Code in your system. Press Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (macOS) to open the Command Palette.

Type Git: Clone and select Git: Clone from the list.

git clone vscode

Enter the SSH URL of your repository. Enter the SSH URL of your repository [email protected]:username/repository.git and press Enter.

Select a local folder where you want to clone the repository.

Verify Setup

To verify that your SSH key is set up correctly, you can run a simple test:

Run the following command to test your SSH connection:

ssh -T [email protected]

You should see a message like this if everything is set up correctly:

ssh vscode

Troubleshooting

If you encounter issues while setting up your SSH key, here are some common troubleshooting steps:

Check SSH Key Permissions

Ensure your SSH private key has the correct permissions:

chmod 600 ~/.ssh/id_rsa

Restart SSH Agent

If the SSH agent is not running, restart it using:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Correct SSH URL

Double-check that you are using the correct SSH URL for your Git hosting service.

Conclusion

Adding an SSH key to Visual Studio Code enhances the security and efficiency of your development workflow. By following the steps outlined in this guide you can seamlessly configure your development environment to use SSH keys, ensuring secure connections to your remote repositories.

When you install Visual Studio Code on Windows to connect SSH, it is essential for setting up a robust development environment. Pairing this setup with Ultahost’s best Windows hosting can further enhance your workflow by providing a reliable and powerful platform for your development projects, ensuring optimal performance and scalability.

FAQ

What is an SSH key in Visual Studio Code?
Why should I add an SSH key in Visual Studio Code?
How do I add an SSH key to Visual Studio Code?
Can I use an existing SSH key with Visual Studio Code?
Where can I find the SSH settings in Visual Studio Code?
Do I need to install any extension to use SSH in Visual Studio Code?
How do I know if my SSH key is working in Visual Studio Code?

Related Post

How to Manage Databases with SSH Command Line

SSH stands for Secure Shell is a powerful network proto...

How to Fix “Connection reset by peer

The "connection reset by peer" problem is an issue that...

How to Install Visual Studio Code on Linux

Visual Studio Code (VS Code) is a widely used code edit...

How to Install Visual Studio Code on Windows

Visual Studio Code (VS Code) is a free and open-source ...

How to Fix the SSH Connection Refused Error

SSH, short for Secure Shell, is a cryptographic network...

How to Install PuTTY in Windows

PuTTY is a free and open-source terminal emulator that ...

Leave a Comment