Using SSH Keys With Visual Studio Code


This is a blog about using SSH Keys With Visual Studio Code

This is a blog about using SSH Keys With Visual Studio Code. If you’d like to know more about SSH keys, click on this link to read the article I wrote about them. This article will focus specifically on using them with VS code as I’ve found it to be a little unintuitive and therefore not well documented.

I’ve been using VS code for quite some time now and had always been curious as to whether or not it was possible to use SSH keys with it. Turns out there is but it’s not straight forward so I thought I would write an article about how I got it set up.

Pre-requisites:

VS Code Installed and Open

SSH Key Set Up (click here if you need help)

Installing the Remote VSCode Plugin:

Step 1) Go to View –> Command Palette… and type “install” in the search bar:

Step 2) Click on the “Remote-SSH: Connect To Host…” command from the list of options that appear:

Step 3) Now type in your host IP/URL, username, and port (if applicable). If you have an SSH key set up then you can omit the password field

In this blog post I will show you how to create an SSH key, upload it to Gitlab and use it with Visual Studio Code.

Why?

Well, because SSH is a great way to authenticate yourself to a remote server. It is more secure than using a password alone because even if someone figures out your password they can’t login to your account without also having the private key that you keep on your local computer.

We have 2 options here:

Password Authentication – it’s pretty bad for many reasons but mostly because people are terrible at picking passwords.

Key Based Authentication – we will use this in this blog post. It is more secure than password authentication because the private key lives and dies on your local computer and only your local computer, so there is no way for anyone to ever get access to it.

If you are using Visual Studio Code there is very high probability that you are also using Git as your main version control system. If you are using some other type of remote source control like SVN or TFS, the Visual Studio Code remote extensions are for you.

In this post we will see how to use SSH keys with VS Code.

Git has a built-in SSH client (installed as part of Git) which can be used to connect to any SSH server. This is the default authentication protocol used by most Git hosting providers like Bitbucket, Github, Gitlab and more.

The standard way to authenticate with such hosts is by providing a user name and password. But if you have generated an SSH key pair which you are already using to connect to your server or another third party hosting service, then it makes sense to continue using the same key pair on your local machine with VS Code as well.

This is a guide for setting up SSH keys with Visual Studio Code so that you can access remote servers without having to use a password.

Why you would want to do this:

Using an SSH key is more secure and convenient than using a password alone.

It will save you from having to type in your password every time you connect to your server.

It will make it easier to work on projects with other developers.

See how easy it is? Just copy the public key into your authorized_keys file and you’re all set! Right? Wrong! What if you have more than one computer? If you just copy the same key to each computer, they will all have the same unique identifier. Then when you connect to your server, it won’t know which computer it’s connecting from, so won’t be able to tell them apart. It will just get confused and think something’s wrong. Bummer!

But I already have SSH keys setup! No problem! You can generate additional keys whenever you like. Just follow the steps below and make sure you give them a different name than your existing key.

What is SSH?

SSH (Secure Shell) is a cryptographic network protocol that provides secure communications over unsecure channels. It has been around since 1995 and is widely used today.

What does it have to do with Visual Studio Code?

Visual Studio Code Remote Development allows you to use a container, remote machine, or the Windows Subsystem for Linux (WSL) as a full-featured development environment. Setup is seamless and VS Code will automatically update your local settings to match those on the remote machine. This allows you to edit files on the remote machine seamlessly from your local machine.

SSH Keys

We will not be going into great detail here as there are many resources available online that do this very well. Essentially ssh keys allow you to authenticate without entering a password when establishing an ssh connection. Instead of entering a password, you use your ssh key which is securely stored on your local system.

How can I set this up with VS Code?

The setup process is quite simple and takes only a few minutes with the help of the Visual Studio Code Remote – SSH extension. Follow these steps:

Have you ever used the terminal in VSCode to SSH into a remote server? I think it’s pretty awesome. When you want to SSH into a server with a key pair, the most common way to do it is to use ssh-agent. You can do this by running eval $(ssh-agent), adding your key pair with ssh-add , and then logging in with ssh user@host. But I have also found that there is another way of doing things. In this post, I am going to show you how to use SSH keys directly with VSCode.

First off, if you are using Windows 10, you will need OpenSSH installed on your computer. This can be downloaded from here if you would like to use it. You will also need an SSH key pair (generate one here) for the server that you are connecting to.

Open up ~/.ssh/config on your local computer and add an entry for the remote host like this:

To get this working, we’ll need two things: a VSCode extension called Remote – SSH and a host to connect to. I’ll be using my personal Digital Ocean box as the host, but you can use any Unix-like system with an SSH server installed. Note that while this tutorial is written for Ubuntu 18.04, the steps should be very similar on other versions or distributions of Linux.

The first thing we’ll need to do is install the extension on our local workstation. Open up VSCode and press F1 or Ctrl + Shift + P to open the command palette, type ext install , then type remote-ssh when the list appears. Then press enter or click the Remote – SSH: Install extension entry to install it.

Next, we’ll need to generate an SSH key pair for authenticating your VSCode sessions. On Linux and MacOS systems, you can use the ssh-keygen command in terminal to generate your key pair: ssh-keygen -t rsa . Just follow the prompts; you can accept all of the defaults by pressing enter at each one (you don’t even need to enter a passphrase).

We’re almost done! Now


Leave a Reply

Your email address will not be published. Required fields are marked *