Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

We have a server with some of our git repositories on and I would like to be able to clone one to our webserver (which runs debian). I ran ssh-keygen on the webserver and copied the public key into authorized_keys on the gitserver and so now I can successfully ssh to the gitserver from the webserver:

ssh -T user@gitserver.com

And it will log me in. However, if I call git clone on the webserver:

sudo git clone user@gitserver.com:/dir/repo.git

I get this response:

Permission denied (publickey).
fatal: The remote end hung up unexpectedly

Does anyone know why this is happening? I read that I should allow ForwardAgent in /etc/ssh_config on my local machine (running Max OS X), which I've done but didn't fix the issue.

share|improve this question

1 Answer 1

You're not calling git clone; you're calling sudo which switches to a completely different Unix account (root) and only then calls git clone.

The result, as you'd also notice with sudo ssh -v ..., is that ssh is looking for private keys in directories belonging to root, not you. Since sudo cleans the environment by default, ssh cannot use your SSH agent either, as the SSH_AUTH_SOCK environment variable gets removed.

As you have agent forwarding enabled, try sudo -E ssh ... to make it preserve all environment variables.

share|improve this answer
    
Ah very true. Doh! –  ing0 Jun 27 at 16:40
    
I've just shut down the test servers, but I will try that later tonight. Thanks –  ing0 Jun 27 at 16:41

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.