How to add an SSH key to VS Code and connect to a host - The Load Guru (2023)

SSH keys are the first line of defense against brute force attacks on your server and are essential for any serious developer. Learn how to add an SSH key in VS Code and connect to a host without creating a password.

Los"visual studio code connect to remote server” is a tool that allows users to connect to a remote computer using SSH. This tutorial will show you how to add an SSH key and use the command line tool in VS Code.

This guide will show you how to add an SSH key to VS Code so you can set up an SSH connection via key exchange. In this article, you'll learn how to configure Visual Studio Code to connect via SSH by exchanging keys for a user with sudo permissions (in this case, homelab) and the built-in root user.

previous requirements

  • Windows version of Visual Studio Code. This lesson will use version 1.5.1.1.
  • Windows 10 (SSH is required and comes pre-installed)
  • A remote SSH host – For this tutorial, an OpenSUSE host with SSH enabled will be used.
  • In VS Code, you've already created an SSH host (see this article).

SSH key generation

To get started, you'll need to create a public and private SSH key on your Windows computer.

  1. Open PowerShell on your local computer and run ssh-keygen. The default path for your keys is C:users<username>.ssh.
  2. Provide the folder path to save the public and private keys. The default is C:Users<user>.sshid_rsa.
  3. An optional pass is provided. If you specify a password, the private key will be encrypted with that password.

How to add an SSH key to VS Code and connect to a host - The Load Guru (1)Using ssh-keygen to generate a key

When finished, you will now have two files (keys) in the folder where you saved the keys, called id_rsa.pub (public key) and id_rsa (private key). By default, these keys will be in the C:Users<user>.ssh folder.

Associate your SSH user and load the public key from the SSH host

Then the public key (id rsa.pub) must be transferred to the remote SSH user's authorized keys directory. You can load the key using VSCode's built-in browser.

(Video) Using Visual Studio Code for Remote Development

Using VS Code and SSH to set up an SSH key exchange connection

  1. Open the user's home folder if you previously set up an SSH host in VS Code. The statement uses the homelab user in this scenario.
  2. In your home directory, create a .ssh folder and upload the id rsa.pub file.
  3. Change the file name to authorized keys (lower case). This is an example that you can see below.

How to add an SSH key to VS Code and connect to a host - The Load Guru (2)Uploading SSH host public key

Your public key has now been successfully connected to your SSH user. Without a password, you will be able to sign in again.

Associating the root user with the public key

You can SSH in as your regular user or as the built-in root user if you assign the same key to both.

This will transfer the /.ssh/authorized keys to /root/.ssh/authorizedkeys for the root user. You can achieve this using the following command:

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

How to add an SSH key to VS Code and connect to a host - The Load Guru (3)transfer the administrative user's public key to the root user's folder

Sudo administrative permissions are required to associate your key with the root user. You can find more information about sudo here.

On Windows, you should now be able to log into the root account via SSH without having to provide a password.

Using YaST, add your public key to the root user graphically.

If you want to learn visually, you can use the OpenSUSE YaST configuration software to do the same. This will do the same thing as moving the authorized key file to the root user, but in a friendlier way.

(Video) How to use FTP Simple in Visual Studio Code - Connect, Edit, Upload

YaST is a configuration tool for the OpenSUSE Linux distribution.

A Windows Guy in a Linux World: YaST and the Desktop is a related post.

  1. Upload your id rsa.pub file to your home directory using VS Terminal Code:

How to add an SSH key to VS Code and connect to a host - The Load Guru (4)Terminal VS Code

2. In VSCode Terminal, type sudo yast.

3. Once in YaST, go to Security and Users —> User and Group Management using the arrow keys. Press Enter to enter the User and Group Management section. This step takes you to the YaST user administration screen.

How to add an SSH key to VS Code and connect to a host - The Load Guru (5)How to access the user and group management page

4. In YaST, press Alt+S to change the filter from regular users to system users.

5. Go to the root. To modify, press alt+i and then alt+s to access the keys section. This is shown in the following animation:

How to add an SSH key to VS Code and connect to a host - The Load Guru (6)How to access SSH public key settings

6. Press alt+a to go to the id rsa.pub file you uploaded earlier and press alt+o to confirm. The fingerprint must have been assigned to the root user.

(Video) Setup Git for using GitLab (including SSH key) - Windows 10

7. Keep pressing alt+o to exit the menus, then alt+q to exit. This is shown in the following animation:

How to add an SSH key to VS Code and connect to a host - The Load Guru (7)To accept, keep pressing alt+o.

At this stage, you have successfully performed a key exchange to root! Now delete the /home/<user>/id_rsa.pub loaded on your Linux host if you wish.

Using the key to authenticate

Finally, you need to use the newly produced key to test the connection. For that, first:

Open a PowerShell console and try running ssh <user>@<ip> to test login as your user and root user. This guide uses the home lab user as a normal user. If all goes well, you should immediately log into either account. You can see this below:

How to add an SSH key to VS Code and connect to a host - The Load Guru (8)Test whether both users' key authentication is successful. There is no password prompt.

Now you can 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>.sshid_rsa) and automatically used it to authenticate.

Using any of the users in Visual Studio Code

What if you need to connect to your remote SSH server with root and main administrator user accounts? In VS Code, you can create this situation using two possible configurations.

  1. In VS Code, go to the sidebar and click on the Remote Browser button, then the gear under SSH Destinations as shown below.

How to add an SSH key to VS Code and connect to a host - The Load Guru (9)Open the target configuration file for SSH

2. VS Code will ask you to choose a file. To choose the default file created earlier in this article, press Enter. As illustrated below, VS Code will open a configuration file for you to modify.

(Video) How to Set Up Live Server and Browser Auto Refresh In Visual Studio Code

How to add an SSH key to VS Code and connect to a host - The Load Guru (10)Editing the SSH target configuration file

3. Cut and paste the first three lines of the configuration below, separated by a blank line.

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

How to add an SSH key to VS Code and connect to a host - The Load Guru (11)Create two different destinations (even if it's the same machine)

Spaces and special characters affect the Host parameter. Use only hyphens and alphanumeric letters to name your host. The hostname is the same as the IP or DNS name. The IP address used in the example above is 172.25.179.190.

You now have two SSH targets: test-machine and test-machine-root, as shown in the image below.

How to add an SSH key to VS Code and connect to a host - The Load Guru (12)Examples of SSH targets to connect

5. Right-click on the SSH target and choose Join Host in Current Window from the context menu as shown below.

You have full access to your system if you log in as root, including protected files.

How to add an SSH key to VS Code and connect to a host - The Load Guru (13)Join the host

(Video) How to Setup an FTP Server on Windows 10

If you join the host in a new window, VSCode will start a separate instance with the new connection. This allows you to have root and <user> open simultaneously in two separate sessions. Don't confuse which is which!

Los"vscode remote ssh permission denied (public key)” is a message you might see if you are trying to connect to a host using SSH. This article will explain how to add an SSH key and connect to your host.

Related tags

  • add ssh key to visual studio 2019
  • visual studio ssh remote code
  • config file vs ssh code
  • vscode-remote ssh keeps asking for password
  • vscode-remote ssh could not establish connection

FAQs

How do I add a SSH key to VScode? ›

Add SSH key to your VM

Select Use existing public key in the dropdown for SSH public key source so that you can use the public key you just generated. Take the public key and paste it into your VM setup, by copying the entire contents of the id_ed25519.pub in the SSH public key.

How do I manually add a SSH key? ›

Manually Copy Your Public Key
  1. Begin by copying the contents of your public SSH key on your local computer. ...
  2. Once you have copied that text, use the command ssh to add the key. ...
  3. Create the ~/.ssh directory and authorized_keys file if they don't already exist: mkdir -p ~/.ssh && touch ~/.ssh/authorized_keys.
Feb 1, 2023

How do I add an existing SSH key? ›

ssh/config file to automatically load keys into the ssh-agent and store passphrases in your keychain. First, check to see if your ~/. ssh/config file exists in the default location. $ open ~/.
...
  1. Start the ssh-agent in the background. ...
  2. Add your SSH private key to the ssh-agent. ...
  3. Add the SSH key to your account on GitHub.

Can you SSH from VS code? ›

VS Code Remote SSH

You can connect over SSH into another machine from Visual Studio Code and interact with files and folders anywhere on that remote filesystem. If you have an app located on a different computer, you could use SSH to connect to it and access your app, view its files, and even modify, run, and debug it.

Where does VS code look for SSH keys? ›

Check to see if you already have an SSH key on your local machine. This is typically located at ~/.ssh/id_ed25519.pub on macOS / Linux, and the .ssh directory in your user profile folder on Windows (for example C:\Users\your-user\.ssh\id_ed25519.pub ).

How to configure SSH key for Git in vscode? ›

How to set up Visual Studio Code Remote SSH with GitHub
  1. Step1: Install and configure git.
  2. Step2: Generate SSH key pair.
  3. Step3: Setup SSH access between client and GitHub.
  4. Step4: Create new GitHub repository (Optional)
  5. Step5: Clone GitHub repo to Linux Client.
  6. Step6: Install Visual Studio Code.

Where do I put my SSH key on my server? ›

Install the public key in the user account on the server.
  1. In the home directory of the account that the client will use to access the server, create a directory called . ssh if it doesn't already exist.
  2. Save the key file as authorized_keys in . ssh. ...
  3. Configure permissions on the key.

How to copy SSH key to remote host? ›

Copying Public Keys to Remote Servers
  1. Use the ssh-copy-id command to append the public key in the local ~/.ssh/id_rsa.pub file to the ~/.ssh/authorized_keys file on the remote system, for example: Copy. ssh-copy-id remote_user@host.
  2. When prompted, enter your password for the remote system.
Apr 13, 2022

How do I activate an SSH key? ›

How to set up SSH keys
  1. Create the ssh key pair using ssh-keygen command.
  2. Copy and install the public ssh key using ssh-copy-id command on a Linux or Unix server.
  3. Add yourself to sudo or wheel group admin account.
  4. Disable the password login for root account.
Aug 11, 2022

Do you need to restart SSH after adding key? ›

Description. Restarting the SSH service is necessary if you made changes to your SSH configuration file and a good idea if you are troubleshooting your SSH connection.

How do you create a SSH key and add it to git? ›

The process for creating an SSH key is the same between them.
  1. execute the following to begin the key creation. ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ...
  2. You will then be prompted to "Enter a file in which to save the key." ...
  3. The next prompt will ask for a secure passphrase. ...
  4. Add the new SSH key to the ssh-agent.

Can you SSH with Visual Studio? ›

In the latest version of Visual Studio, users are now able to leverage the integrated terminal to access their remote targets when developing for remote machines from Windows. This updated terminal includes an interactive SSH shell.

How do I SSH into a server? ›

How to connect via SSH:
  1. Open the list of your servers and click on the one you need. Click the Instructions button. ...
  2. Open a terminal (for Linux) or a command line (for Windows) on your computer. Enter the command: ssh [username]@[server IP] ...
  3. The connection will prompt you for a password.

Can you DDoS with SSH? ›

If your SSH server is exposed to the Internet, then it is vulnerable to DDoS attacks.

How to execute SSH keygen in vscode? ›

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>\. ...
  2. Provide the folder path to save the private and public key. ...
  3. Provide an optional passphrase.
Dec 17, 2020

How do I assign a SSH key to a Git repository? ›

How to Add an SSH Key to your Github Account
  1. Log into your GitHub account.
  2. Click your avatar and choose Settings.
  3. Select SSH and GPG keys.
  4. Click New SSH key.
  5. Enter a title in the field.
  6. Paste your public key into the Key field.
  7. Click Add SSH key.
Feb 21, 2022

How to use SSH key for Git? ›

GitHub SSH key setup steps
  1. Create a GitHub SSH key pair with the ssh-keygen command.
  2. Copy the value of the public SSH key to the clipboard.
  3. Login to GitHub and navigate to your account settings.
  4. Click on the SSH and GPG link.
  5. Click Add Key to register the public SSH key with your account.
Jan 16, 2022

How do I use Git credentials in VS Code? ›

From the Visual Studio Code Command Palette, select: GitHub Pull Requests: Sign out of GitHub. Then sign in with your new credentials. Save this answer.

How to add SSH key to local Git? ›

The process for creating an SSH key is the same between them.
  1. execute the following to begin the key creation. ssh-keygen -t rsa -b 4096 -C "your_email@example.com" ...
  2. You will then be prompted to "Enter a file in which to save the key." ...
  3. The next prompt will ask for a secure passphrase. ...
  4. Add the new SSH key to the ssh-agent.

Can I have a SSH key without an passphrase? ›

When creating SSH keys, you can create them with or without a passphrase. If you do create a key with passphrase, you will be asked for passphrase every time you try to communicate with your Git repository in Beanstalk. Using passphrases increases the security when you are using SSH keys.

How to set SSH key in Linux? ›

Set up personal SSH keys on Linux
  1. Install OpenSSH on your device.
  2. Start the SSH Agent.
  3. Create an SSH key pair.
  4. Add your key to the SSH agent.
  5. Provide Bitbucket Cloud with your public key.
  6. Check that your SSH authentication works.

Videos

1. How to Make a QR Code For Your Wi-Fi (And Impress Your Friends)
(ThioJoe)
2. How To Add Eclipse Project To GitHub | How to Commit, Push, Pull from Eclipse to GitHub
(Automation Step by Step)
3. How to connect to EC2 w/ PuTTY (Windows)
(Linux Academy)
4. How to connect to GitHub From AWS EC2 Instance?
(Cloud Guru)
5. AWS Site To Site VPN - New video with improved steps (Part 1)
(AWS Training Center)
6. HTML Tutorial: Installing VS Code & Live Server | Web Development Tutorials #2
(CodeWithHarry)

References

Top Articles
Latest Posts
Article information

Author: Melvina Ondricka

Last Updated: 06/11/2023

Views: 5651

Rating: 4.8 / 5 (68 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Melvina Ondricka

Birthday: 2000-12-23

Address: Suite 382 139 Shaniqua Locks, Paulaborough, UT 90498

Phone: +636383657021

Job: Dynamic Government Specialist

Hobby: Kite flying, Watching movies, Knitting, Model building, Reading, Wood carving, Paintball

Introduction: My name is Melvina Ondricka, I am a helpful, fancy, friendly, innocent, outstanding, courageous, thoughtful person who loves writing and wants to share my knowledge and understanding with you.