---
title: "Add SSH Key to VS Code and Connect to a Host"
description: "Learn how to generate a new SSH key, add it to VS Code, and set up key exchange authentication for multiple user connections."
canonical: "https://adamtheautomator.com/add-ssh-key-to-vs-code/"
---

# Add SSH Key to VS Code and Connect to a Host

> Learn how to generate a new SSH key, add it to VS Code, and set up key exchange authentication for multiple user connections.

Source: https://adamtheautomator.com/add-ssh-key-to-vs-code/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Add SSH Key to VS Code and Connect to a Host](https://adamtheautomator.com/wp-content/uploads/2020/12/Setting-up-an-SSH-Key-Exchange-Connection-with-VS-Code-and-SSH.jpg)

# Add SSH Key to VS Code and Connect to a Host

[![](https://secure.gravatar.com/avatar/d651a7bb1f4308be67d95c5cb50560ed0ee3e700256bd027cb08fbea6655b457?s=192&d=mm&r=g)Christopher Bisset](https://adamtheautomator.com/author/christopher-b/)17 December 20205 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Linux](/tag/linux/)[Visual Studio Code](/tag/visual-studio-code/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Creating the SSH Key](#h-creating-the-ssh-key)
*   [Uploading the Public Key to the SSH Host and Associating your SSH user](#h-uploading-the-public-key-to-the-ssh-host-and-associating-your-ssh-user)
*   [Associating the Public Key with the root User](#h-associating-the-public-key-with-the-root-user)
*   [Graphically Adding your Public Key to the root user using YaST](#h-graphically-adding-your-public-key-to-the-root-user-using-yast)
*   [Authenticating with the Key](#h-authenticating-with-the-key)
*   [Configuring VS Code to use Either User](#h-configuring-vs-code-to-use-either-user)

If you need to add SSH key to VS Code to set up an SSH connection using key exchange, this tutorial is for you. In this tutorial, you’re going to learn, step-by-step, how to configure VS code to connect over SSH via key exchange for both a user with `sudo` rights (`homelab` in this guide) and the built in `root` user.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

## Prerequisites

*   [Visual Studio Code](https://code.visualstudio.com/download) for Windows. This tutorial will be using version 1.5.1.1.
*   Windows 10 (SSH is needed and comes pre-installed)
*   A remote SSH host – This tutorial will be using an OpenSUSE host with [SSH enabled](https://adamtheautomator.com/ssh-command-in-linux/).
*   You’ve already set up an SSH Host in VS Code ([see this article](https://adamtheautomator.com/vs-code-remote-ssh/)).

## Creating the SSH Key

To begin, you’ll first need to generate a private and public SSH key on your Windows machine.

1.  Open up PowerShell on your local computer and run _ssh-keygen_. The default path for your keys is _C:\\users\\<user>\\.ssh._
2.  Provide the folder path to save the private and public key. The default is _C:\\Users\\<user>\\.ssh\\id\_rsa_.
3.  Provide an optional passphrase. If you provide a passphrase, this passphrase will be used to encrypt the private key.

![generating a key using ssh-keygenAdd SSH key to Vs Code : generating a key using ssh-keygen](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T101318.827.png)

Add SSH key to Vs Code : generating a key using ssh-keygen  

When complete, you’ll now have two files (keys) in the folder you saved the keys into called _id\_rsa.pub_ (public key) and _id\_rsa_ (private key). By default, these keys will be in the _C:\\Users\\<user>\\.ssh_ folder.

## Uploading the Public Key to the SSH Host and Associating your SSH user

You’ll next need to transfer the public key (_id\_rsa.pub)_ to the remote SSH user’s authorized keys location. By using VSCode, you can use its built-in explorer to upload the key.

**_Related: [Setting up an SSH Key Exchange Connection with VS Code and SSH](https://adamtheautomator.com/vs-code-remote-ssh/)_**

1.  If you’ve already set up an SSH host in VS Code, open up the home folder of the user. In this case, the tutorial is using the `homelab` user.
2.  Create the _.ssh_ folder in your home directory and upload the _id\_rsa.pub_ file.
3.  Rename the file to _authorized\_keys_ (lower case). You can see this example below.

![Uploading the Public Key to the SSH Host](https://adamtheautomator.com/wp-content/uploads/2020/12/K4ezHylPnw.gif)

Uploading the Public Key to the SSH Host

You have now successfully associated your public key with your SSH user. You should be able to reconnect without requiring a password.

## Associating the Public Key with the root User

Associating the same key with the `root` user gives you the ability to SSH in as either your regular user or the built-in `root` user.

For the `root` user this will be copying _~/.ssh/authorized\_keys_ to _/root/.ssh/authorized\_keys_. You can do so by running the below command:

```bash
sudo mkdir -p /root/.ssh && sudo cp ~/.ssh/authorized_keys /root/.ssh/authorized_keys
```

![Copying your public key from your administrative user's folder to the root user's folder](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T133338.557.png)

Copying your public key from your administrative user’s folder to the root user’s folder

> _Associating your key with the `root` user requires `sudo` administrative rights. You can read more about [sudo here](https://en.opensuse.org/SDB:Administer_with_sudo)._

You should now be able to log into the root user via SSH without requiring a password in Windows.

### Graphically Adding your Public Key to the root user using YaST

If you are more of a visual learner, you can perform the same task using OpenSUSE’s YaST configuration utility. This will be functionally the same as copying the _authorized\_keys_ file to the root user, in a more friendly way.

> _YaST is a configuration tool that is specific to the [OpenSUSE distribution](https://www.opensuse.org/)._

_**Related post: [A Windows Guy in a Linux World: YaST and the Desktop](https://adamtheautomator.com/yast/)**_

1.  Navigate to the VS Code terminal and upload your _id\_rsa.pub_ file to the home directory:

![VS Code terminal](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T133500.358.png)

VS Code terminal

2\. Run `sudo yast` in the VSCode Terminal.

3\. Once in YaST, navigate to **Security and Users —> User and Group Management** using the arrow keys. Press **Enter** to enter the **Users and Group Management** section. This step brings you to YaST’s user management screen.

![Navigating to User and Group Management](https://adamtheautomator.com/wp-content/uploads/2020/12/omVVMijKgc.gif)

Navigating to User and Group Management

4\. Press **Alt+S** in YaST to change the filter from normal users to system users.

5\. Navigate down to `root`. Hit **alt+i** to edit and then **alt+s** again to move to the keys section. You can see that in the animation below:

![Getting to the SSH Public Key Settings](https://adamtheautomator.com/wp-content/uploads/2020/12/WnbIZVDnVD.gif)

Getting to the SSH Public Key Settings

6\. Press **alt+a** and navigate to the _id\_rsa.pub_ file you uploaded earlier and hit **alt+o** to **OK**. You should see the fingerprint added to the root user.

7\. Keep hitting **alt+o** to OK out of the menus and finally **alt+q** to quit. You can see that in the below animation:

![Keep hitting alt+o to OK](https://adamtheautomator.com/wp-content/uploads/2020/12/JahzrsE9tO.gif)

Keep hitting alt+o to OK

At this stage, you’ve successfully performed a key exchange for root! Now delete that uploaded _/home/<user>/id\_rsa.pub_ on your Linux host, if you wish.

## Authenticating with the Key

Finally, you should test out the connection using the newly created key. To do that:

Open up a PowerShell console and try running `ssh <user>@<ip>` to test logging in as both your user and the root user. This guide is using the `homelab` user as the normal user. If all goes well, you should immediately be logged in to either account. You can see this below:

![Testing that key authentication is successful for both users. No password prompt is required.](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T133820.322.png)

Testing that key authentication is successful for both users. No password prompt is required.

You can now SSH in as root and the terminal didn’t even ask for a password. SSH for Windows knew where to look for your private key (_C:\\users\\<user>\\.ssh\\id\_rsa_), and used that automatically to authenticate.

## Configuring VS Code to use Either User

What if you need to connect to your remote SSH host using both the `root` user account and a standard admin user? You can set this scenario up using two different configurations in VS Code.

1.  In VS Code, click on the **Remote Explorer** icon in the sidebar and click on the cog under **SSH Targets** as shown below.

![Opening the SSH target config file](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T134620.115.png)

Opening the SSH target config file

2\. VS Code will ask for a file to choose. Press Enter to to choose the default file you generated earlier in this article. VS Code will open up a config file to edit as shown below.

![Editing the SSH Target config file](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T134701.932.png)

Editing the SSH Target config file

3\. Copy the first three lines of the config and paste them below separated with a blank line.

4\. Change the second user from <your user> (`homelab` in the below screenshot) to `root`. Also, give them more descriptive names (The `Host` parameter) so you can tell the two configurations apart. Press C**ontrol+s** to save:

![Setting up two separate targets (even though it is the same machine)](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T134741.932.png)

Setting up two separate targets (even though it is the same machine)

> _The `Host` parameter is sensitive to spaces and special characters. Name your host only using hyphens and alphanumeric letters. `Hostname` is equivalent to either IP or DNS name. The above example is using an IP of `172.25.179.190`_

You can now see (in the screenshot below) you have two SSH targets: **test-machine**, and **test-machine-root**.

![Two example SSH targets for connecting](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T134846.114.png)

Two example SSH targets for connecting

5\. Now connect to either host by right-clicking the SSH target and clicking **Connect to Host in Current Window** as shown below.

> _If you connect as root, you have full access rights to your machine — including protected files._

![Connect to Host](https://adamtheautomator.com/wp-content/uploads/2020/12/Untitled-2020-12-07T134956.146.png)

Connect to Host

> _If you **Connect to Host in New Window**, VSCode will launch a separate instance with the new connection. This lets you have both `root` and `<user>` open simultaneously in two separate sessions. Do not mix up which is which!_

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fadd-ssh-key-to-vs-code%2F&text=Add%20SSH%20Key%20to%20VS%20Code%20and%20Connect%20to%20a%20Host)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fadd-ssh-key-to-vs-code%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fadd-ssh-key-to-vs-code%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2020/12/A-Windows-Guy-in-a-Linux-World_-VS-Code-and-Remote-SSH.jpg)

### [VS Code and Remote SSH: A Windows Admin's Guide](/vs-code-remote-ssh/)

Learn how to connect VS Code to a remote SSH host and edit files on Linux over an SSH connection in this tutorial.

![](https://adamtheautomator.com/wp-content/uploads/2024/12/DALL·E-2024-12-10-16.09.53-A-developer-writing-PowerShell-code-in-Visual-Studio-Code.-The-scene-shows-a-modern-workspace-with-a-computer-monitor-displaying-PowerShell-script-in-.webp)

### [Setting Up VS Code for PowerShell Development: A Beginner’s Guide](/vs-code-powershell-setup/)

Learn how to set up Visual Studio Code (VS Code) for PowerShell development in this step-by-step guide. You'll learn how to install VS Code, configure the PowerShell extension, set up the integrated terminal, and explore key features to enhance your PowerShell scripting experience.

![](https://adamtheautomator.com/wp-content/uploads/2022/09/Practical-Linux-Unix-Tee-Commands-for-the-Linux-Admin.jpg)

### [Master Unix tee Commands for Real-World Linux Admin Tasks](/unix-tee/)

Simplify Linux output management and streamline your workflow with the “Unix tee” command. This tutorial guides you through its practical applications.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
