VS Code IDE with Passphrased Git SSH Keys

Access Denied (by https://www.flickr.com/photos/raidokaldma/)I’m just kicking the tires with Visual Studio Code. So far, it seems like a nice little editor! One thing that tripped me up a bit was the Git integration… permission denied errors due to git ssh keys that use passphrases.

Here’s how I solved it…

Environment

For some of my hosted repo’s, I use an SSH key instead of a username/password. That key has a passphrase on it.

Git Error: Permission Denied

When I opened that codebase in VS Code, the Git screens complained that it could not connect. The output from VS Code showed:

git fetch
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).
fatal: Could not read from remote repository.

Step 1: SSH-AGENT

My first step was to make sure that, from a windows command prompt, I could execute “git fetch” without any authorization prompts. SSH-AGENT is supposed to make this happen.

This works fine in the bash window, but is more troublesome from a standard command prompt. A bit of digging lead me to the “start-ssh-agent.cmd” batch file that’s installed with Git. That launches the ssh-agent and prompts me for the passphrases to the keys in my .ssh folder.

I enter the passphrase once and then git stops prompting me. Beautiful.

But…

VS Code still threw permission denied errors at me. Investigating the SSH-AGENT situation a bit, it looks like the agent uses some environment variables. Those are set on the current command prompt, but VS code was launched with a separate environment. So VS Code didn’t seem to pick up or use the SSH-AGENT.

Step 2: Launch VS Code from Command Line

My solution was to follow this flow:

  1. Open a command prompt.
  2. Run “start-ssh-agent” and answer passphrase prompts.
  3. Run “code” to launch VS Code from that environment.

Works!

I’d love to hear if there is a better, cleaner, simpler way to make this work. Drop comment if you have a thought.

Cheers!

(Photo by https://www.flickr.com/photos/bill_harrison/)

5 thoughts on “VS Code IDE with Passphrased Git SSH Keys

Add yours

  1. I found a solution to make it work without starting VScode through a terminal:

    A comment by penguin359 to this answer https://serverfault.com/a/254510 mentions:

    “The recommended way to use ssh-agent is not in Autostart, but use it kinda like sudo to run startkde. For example, “ssh-agent startkde” Before startkde runs, SSH_AUTH_SOCK is setup and when startkde exits, so does ssh-agent. It’s more reliable than depending on Autostart, but trickier to set up correctly. Ubuntu has support to make this easy to do and will even generate a chain of commands like this. Looking at ps, I see ‘ssh-agent gpg-agent dbus-launch gnome-session'”

    This led me in the right direction: **Let the ssh-agent start the desktop environment (or window manager) so it is available to all programs in the user session.**

    For NixOS (which I’m currently using) it was as easy as setting

    `programs.ssh.startAgent = true;`

    in the configuration file (`/etc/nixos/configuration.nix`), the rebuilding (`sudo nixos-rebuild switch`) and then rebooting (although a logoff and login should have been sufficient).

    For Ubuntu there seems to be a setting as well and other Linux distributions may also have support for this.

    The important thing here (IMHO) is to know that this is possible in the first place so one can search the internet for a solution for a specific distribution.

    THANK YOU Bernhard, your post made me realize why the ssh-agent did not work in my VScode and made me find the correct answer. ❤

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: