SSH keys are the first line of defense against brute-force attacks on your server and are a must have for any serious developer. Learn how to add an SSH key in VS Code and connect to a host without creating a password.
The “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 set up Visual Studio Code to connect through SSH using key exchange for both a user with sudo permissions (in this case, homelab) and the built-in root user.
- Windows version of Visual Studio Code. Version 18.104.22.168 will be used in this lesson.
- Windows 10 (SSH is needed 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 private and public SSH key on your Windows computer.
- Open up PowerShell on your local computer and run ssh-keygen. The default path for your keys is C:users<user>.ssh.
- Provide the folder path to save the private and public key. The default is C:Users<user>.sshid_rsa.
- An optional pass is provided. If you specify a password, the private key will be encrypted with that password.
Using ssh-keygen to generate a key
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.
Associating your SSH user and Uploading the SSH Host’s Public Key
The public key (id rsa.pub) must then be transferred to the authorized keys directory of the remote SSH user. You may upload the key using the built-in explorer in VSCode.
Using VS Code and SSH to Set Up an SSH Key Exchange Connection
- Open the user’s home folder if you’ve previously set up an SSH host in VS Code. The instruction uses the homelab user in this scenario.
- In your home directory, create a.ssh folder and upload the id rsa.pub file.
- Change the file’s name to authorized keys (lower case). This is an example that you can see below.
Uploading the SSH Host’s Public Key
Your public key has now been successfully connected with your SSH user. Without a password, you should be able to rejoin.
Associating the root user with the public key
You can SSH in as either your normal user or the built-in root user if you associate the same key with both of them.
This will transfer /.ssh/authorized keys to /root/.ssh/authorized keys for the root user. You may accomplish so by using the command below:
sudo mkdir -p /root/.ssh && sudo cp ~/.ssh/authorized_keys /root/.ssh/authorized_keys
transferring your administrative user’s public key to the root user’s folder
Sudo administrative permissions are required to associate your key with the root user. More information about sudo may be found here.
In Windows, you should now be able to get into the root account over 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 may use OpenSUSE’s YaST configuration software to do the same operation. This will do the same thing as moving the authorized keys file to the root user, but in a more user-friendly manner.
YaST is a setup tool for the OpenSUSE Linux distribution.
A Windows Guy in a Linux World: YaST and the Desktop is a related post.
- Upload your id rsa.pub file to the home directory using the Terminal VS Code:
Terminal VS Code
2. In the VSCode Terminal, type sudo yast.
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.
Getting to the User and Group Management page
4. In YaST, press Alt+S to switch the filter from regular to system users.
5. Go down to the root. To modify, press alt+i, then alt+s to get to the keys section. This is shown in the animation below:
How to Access SSH Public Key Settings
6. Press alt+a to go to the id rsa.pub file you previously submitted, then press alt+o to confirm. The fingerprint should have been assigned to the root user.
7. Continue pressing alt+o to exit the menus, then alt+q to exit. This is shown in the animation below:
To OK, keep pressing alt+o.
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.
Using the Key to Authenticate
Finally, you should use the newly produced key to test the connection. To do so, first:
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 both users’ key authentication is successful. There is no password prompt.
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>.sshid_rsa), and used that automatically to authenticate.
Using Either User in Visual Studio Code
What if you need to connect to your remote SSH server with both the root and regular admin user accounts? In VS Code, you may build up this situation utilizing two possible setups.
- In VS Code, go to the sidebar and click the Remote Explorer button, then the cog under SSH Targets, as seen below.
Opening the target configuration file for SSH
2. VS Code will prompt you to choose a file. To choose the default file created previously in this article, press Enter. As illustrated below, VS Code will open a config file for you to modify.
SSH Target Configuration File Editing
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 below screenshot) to root. Also, give them more descriptive names (The Host parameter) so you can tell the two configurations apart. Press Control+s to save:
Creating two distinct objectives (even though it is the same machine)
Spaces and special characters affect the Host parameter. Only use 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 previous example is 172.25.179.190.
You now have two SSH targets: test-machine and test-machine-root, as seen in the image below.
SSH target examples for connecting
5. Right-click the SSH target and choose Join the Host in Current Window from the context menu, as shown below.
You have complete access to your system if you login as root, including protected files.
Join the Host
If you Join the 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!
The “vscode remote ssh permission denied (publickey)” is a message that you may 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.
- add ssh key to visual studio 2019
- visual studio code remote ssh
- vs code ssh config file
- vscode-remote ssh keep asking for password
- vscode-remote ssh could not establish connection