Passwordless SSH Login to a Remote Server with Visual Studio Code

ssh vscode keys

The following article describes how to enable you to log in to a remote server with the industry-standard SSH network protocol by using a key and Visual Studio Code, so you do not have to enter your user/password every time you want to connect. It was written for/on MacOS, but the procedure should be the same on Linux and similar on Windows.

Preparation

At first download, install and open VS Code.

Then install the „Remote – SSH“ extension by changing to the Extensions tab, searching for it and clicking on „Install“.

Fun with keys

Next, open a terminal session by the option „New Terminal“.

At the terminal prompt (normally shown at the bottom of your window), connect to your server by entering the following line and pressing the enter key.

ssh -p PORT USERNAME@SERVER

So, for example, your line could be like ssh -p 22 heinz@mydomain.com. Then accept/confirm any dialogues you are prompted to for confirmation, as well as entering the password.

Now, just leave the ssh session by entering the following command to the terminal:

exit

Next you create a key for ssh (in case you do not already have one) on your local PC. Be sure you really exited/disconnected from the server before:

ssh-keygen

Once done, you can copy the public key part to the server by utilizing the following command:

ssh-copy-id -p PORT USERNAME@SERVER

The next time you log in to the server with ssh you will not be asked for the password anymore, but the key will be automatically verified in the background.

Setting up the environment

So let's try that by logging in at the server (ssh -p PORT USERNAME@SERVER) in the terminal again and by creating a workspace directory for the files you want to edit/control with VS Code later on.

mkdir DIRECTORYNAME

The directory name can be anything you prefer, so e.g. mkdir data could be suitable. Jump into this directory by sending the command:

cd DIRECTORYNAME

e.g. cd data. Then print the full directory path to the terminal screen by:

pwd

The result will be something like /home/heinz/data. Copy this path to your clipboard or note/remember it. You are more than 50% done, now.

Configure VS Code

Open the Remote SSH tab in VS Code and click on „+“ at SSH in the Remote Explorer.

Enter the same command you used before to connect to your server at the new prompt shown on top of the window:

ssh -p PORT USERNAME@SERVER

Choose the local ssh config file to update; normally the first proposal is fine.

Now, in the Remote Explorer you should see the new option for connection, like:

If it's not directly shown, just click the refresh button as shown on the top right of the screenshot. Then click on the „→“ arrow to connect.

Finally, switch to the File Explorer tab and click on „Open Folder“. At the prompt you enter or select the directory you remembered or copied before and click on „OK“:

In case you are asked whether you trust the authors of the directory, it makes sense to choose yes, since you are the author 🙂

We are done. You most likely see an empty folder structure since we just created the folder, and you can create new files of any kind, like the README.md in the next screenshot, and folders as you wish.

It's also possible to copy files from your PC to the server by drag-and-drop, as well as downloading files/folders by a right-click on them in the VS Code File Explorer.

Result

The next time you open VS Code the connection will be established automatically. In case you switch between different environments you can always go back to the Remote Explorer tab of VS Code and connect to the server. Be sure to select the folder for connection and not the server itself, otherwise you will be prompted to select a folder at the File Explorer tab again.

I hope that short tutorial helped you out a bit 🙂