Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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

sudo git clone

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

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 '14 at 16:40
I've just shut down the test servers, but I will try that later tonight. Thanks – ing0 Jun 27 '14 at 16:41

Your Answer


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.