PowerShell, msysgit 1.7.9 and Permission denied (publickey) Errors

TLDR: Add "$env:home = resolve-path ~" to your PowerShell profile.

I recently updated my work Virtual Machine to the latest release of msysgit 1.7.9 to resolve some issues I was having with global settings not being obeyed. After the installation I noticed that I was no longer able to update repositories from PowerShell. The output I was getting looked something like this:

GIT [dirkdiggler] on [master] (clean) | C:\projects\foo
-> git pull
Permission denied (publickey).
fatal: The remote end hung up unexpectedly

This was unexpected and the first thing I thought of was the recent security issue with GitHub, and maybe my work key needed to be validated. I checked GitHub and everything seemed to be set up correctly. I even went so far as to generate new keys with no success.

Next up, it occurred to me to try connecting via git bash.

dirkdiggler@DIRKDIGGLER-VM /c/projects/foo (master)
$ ssh git@github.com
Hi dirkdiggler! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.

Bash seems to be working fine. I then started troubleshooting my connection from PowerShell. I tried testing ssh first with the following command.

GIT [dirkdiggler] on [master] (clean) | C:\projects\foo
-> ssh git@github.com
Permission denied (publickey).

So it looks like the problem was not with git but with establishing an ssh connection to GitHub. I wanted to see exactly what was happening when trying to connect via ssh, so I ran the following command with enables verbose logging of the connection.

GIT [dirkdiggler] on [master] (clean) | C:\projects\foo
-> __ssh -v git@github.com__
OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
debug1: Connecting to github.com [207.97.227.239] port 22.
debug1: Connection established.
debug1: identity file /.ssh/identity type -1
debug1: identity file /.ssh/id_rsa type -1
debug1: identity file /.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.1p1 Debian-5github2
debug1: match: OpenSSH_5.1p1 Debian-5github2 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.6
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Host 'github.com' is known and matches the RSA host key.
debug1: Found key in /.ssh/known_hosts:1
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Trying private key: /.ssh/identity
debug1: Trying private key: /.ssh/id_rsa
debug1: Trying private key: /.ssh/id_dsa
debug1: No more authentication methods to try.
Permission denied (publickey).

This output did not give me any immediate ideas on the problem but I thought I might try the same command from git bash. I won't include the full output here, but I did notice something different right away. Check out the following lines from the output. Compare them to lines 6-8 above.

debug1: identity file /c/Users/MGALFAPAIR/.ssh/identity type -1
debug1: identity file /c/Users/MGALFAPAIR/.ssh/id_rsa type 1
debug1: identity file /c/Users/MGALFAPAIR/.ssh/id_dsa type -1

So it looks like ssh running under PowerShell is looking for my public/private key pair in a different directory than under bash. Doing a quick google search I found that an environment variable named home is used when determining the path to look for keys. I went back to PowerShell and checked for the environment variable like so.

GIT [dirkdiggler] on [master] (clean) | C:\projects\foo
-> Write-Host $env:home

No home variable set. So I set it like so.

GIT [dirkdiggler] on [master] (clean) | C:\projects\foo
-> $env:home = Resolve-Path ~

GIT [dirkdiggler] on [master] (clean) | C:\projects\foo
-> Write-Host $env:home
C:\Users\MGALFAPAIR

Running the ssh test again, I am now able to connect. Adding the command to my PowerShell profile sets it automatically every time I start PowerShell resolving the problem completely.

Follow me on Mastodon!