VS Code with Remote SSH

Riya Punjabi
5 min readApr 18, 2023

--

Code Development in a Remote Environment

Have you been using MobaXterm or Putty to ssh into a remote linux machine? Have you been still using the vim text editor to edit/debug your code in a remote linux machine?

Or already have a Code Editor on your local machine but have been wasting a lot of time in copying code back and forth between local code editor and remote machine terminal?

Stop right there !! :(

Why not use the Best Code Editor connected to your remote linux machine?

VS Code with Remote SSH :)

To get started, you need to

  1. Install an OpenSSH compatible SSH client on your local machine, if one is not already present.

To check if OpenSSH is already installed, open Powershell window, type ssh and press enter.

this might error out if openssh is not installed

Steps to install OpenSSH Client

Type Apps & Features in Windows search box. Go to Optional Features [highlighted in yellow]
click on Add a Feature [highlighted in yellow] and install OpenSSH Client

2. Install Visual Studio Code.

Download VS Code latest version from the below link:

https://code.visualstudio.com/download

3. Install the Remote-SSH extension.

go to the Extensions tab in the left corner
search for Remote SSH and install Remote - SSH extension

Steps to connect to remote machine

  1. Verify you can connect to the SSH host by running the following command from a VS Code terminal replacing user@hostname as appropriate.
ssh user@hostname
# Or for Windows when using a domain / AAD account
ssh user@domain@hostname

2. In VS Code, select Remote-SSH icon at the bottom left of the window.

Connect to Host… from the Command Palette (F1, Ctrl+Shift+P) and use the same user@hostname as in step 1.

I would reocmmend to define your hosts with username, etc in a VS Code SSH Config File [C:\Users\$user\.ssh\config]

Host $remote_linux_machine
HostName $ip_address
User $user

If VS Code cannot automatically detect the type of server you are connecting to, you will be asked to select the type manually.

Once you select a platform, it will be stored in VS Code settings under the remote.SSH.remotePlatform property so you can change it at any time.

After a moment, VS Code will connect to the SSH server and set itself up. VS Code will keep you up-to-date using a progress notification and you can see a detailed log in the Remote - SSH output channel.

At times, you might see some failures while VS Code tries to connect to SSH server ..

[14:49:55.406] > tar: Skipping to next header
[14:49:55.417] > tar: Exiting with failure status due to previous errors
[14:49:55.423] > WARNING: tar exited with non-0 exit code

In case, you observe an above error in the logs when VS Code fails to connect to SSH server, follow the below steps

1. Go to Help → About in VS Code window.

Check the VS Code commit version in the box that popped up

$commit_version

2. SSH into the remote linux machine via MobaXterm or Putty [or any other SSH Client already used]

3. In your home directory, go to directory .vscode-server/bin

$commit_version is the one you got from the above VS Code box.

cd .vscode-server/bin/$commit_version 

You might notice a .tar.gz file of vscode-server exists in the above path.

Basically, VS Code tries to install on the remote linux machine under your home directory. At times, it fails to extract the file due to old versions of tar.

The solution is to

Either update tar to the latest version [≥ 1.26] on remote linux machine

OR

Manually extract vscode files inside the ~/.vscode-server/$commit_version path

tar xvzf vscode-server-linux-x64.tar.gz -C ~/.vscode-server/$commit_version
--strip-components=1

cd ~/.vscode-server/commitversion

ls

bin extensions LICENSE node node_modules out package.json product.json
server.sh

Check if server.sh is present in the path ~/.vscode-server/$commit_version

Close all previously open VS Code windows.

Re-open a new VS Code and try connecting to Remote SSH. It should work this time :)

To use passwordless SSH to remote linux machine, follow the below steps

  1. Open a terminal in MobaXterm or Putty [or any other SSH Client already used]
  2. Type, ssh-keygen to generate new ssh keys for your local windows machine.
  3. Enter the path to store the ssh keys.

Preferrably, try to use C:\Users\$user\.ssh and hit Enter.

ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/mobaxterm/.ssh/id_rsa):
C:\Users\$user\.ssh

You might see two files generated under the path C:\Users\$user\.ssh

  1. id_rsa
  2. id_rsa.pub

Copy contents of id_rsa.pub to ~/.ssh/authorized_keys in your remote linux machine that you are trying to connect from local VS Code.

Once copied, try to Remote SSH from VS Code, this time it won’t prompt for password :)

Hopefully, this blog helped you to remotely connect to VS Code.

In case, you face any new issues. Please add your issues as comments.

We can expand this blog with further solutions.

Happy Coding :)

--

--

No responses yet