🇨🇿 cs

Using ssh-agent with Visual Studio Code in WSL

I recently started to use WSL for local development instead of a Linux virtual machine. Visual Studio Code has an extension to connect to running WSL instance so you can code in WSL while running VS Code in Windows.

If you use Git via SSH (or you use SSH a lot), you might find handy using ssh-agent. Unlike on the full Linux desktop, I could not find a simple way how to run one ssh-agent and reuse it for all open terminals and also for VS Code server (the VS Code part running inside the WSL).

The solution is really simple. Just using the Keychain. In Debian-based distro, run following command to install the utility.

sudo apt-get install keychain

Add following code to your .profile or .bash_profile (or whatever file works for you).

eval `keychain --noask --eval id_rsa`

You can omit the --noask argument if you want to be asked for a passphrase when the first terminal is opened (I just don’t, I can run ssh-add anytime later).

eval `keychain --eval id_rsa`

To give VS Code server access to the same ssh-agent, you have to get it to the server environment as well. The WSL extension has it’s data in ~/.vscode.

You can modify the startup environment via the .vscode-server/server-env-setup file. This file is run by the shell invoking the server so you can add the same line into it (use the --noask variant since this is non-interactive shell).

eval `keychain --noask --eval id_rsa`

Once the keychain is started, it tries to find running ssh-agent socket and modify environment so SSH-related commands can use the agent.

Comments