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
- Windows 8.1
- Git for Windows / msysgit (w/ git stuff in the path)
- Visual Studio “Code” IDE
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:
- Open a command prompt.
- Run “start-ssh-agent” and answer passphrase prompts.
- 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/)
Seems like somebody’s gotten this working with PuTTY’s agent – tried it and it works for me, without the need to start VSCode from the command line. Here’s the process: http://www.cgranade.com/…/ssh-keys-in-vscode.html
LikeLike
THK! Man!!! U helped me a lot!!!!
LikeLike
Brilliant! Thank you!
I tried in 2019 and wonder why MS has not fixed this yet.
LikeLike
Worked for me!! Thanks.
Is there any way to add this in vs code short-cut to start it every time before starting vscode?
LikeLike
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. ❤
LikeLike