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

I have used this bitbucket tutorial for providing ssh access to the git repo:

But now each time I want to use the git it says I have no permissions until I do:

ssh-agent /bin/bash
ssh-add ~/.ssh/id_rsa

why is this happening? I did it easily on my local machine, but the remote server is not working good.


when I do


I get this

SSH_AUTH_SOCK=/tmp/ssh-blGr6z5dJjit/agent.14183; export SSH_AUTH_SOCK;
echo Agent pid 14184;

but than I try to do git fetch, I get this:

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

even after I add the key

ssh-add ~/.ssh/bitbucket

I get this error

Could not open a connection to your authentication agent.
share|improve this question
up vote 3 down vote accepted

The question was a bit unclear, so this answer may really for a different question. But since the answer may still prove useful to others, I think it is worthwhile keeping around.

When you type ssh-agent /bin/bash an instance of ssh-agent is started, which will keep running for as long as that shell is active. By default that ssh-agent will be used only by that shell, and it will terminate once you close that shell.

If instead you only type ssh-agent, an agent will be started in the background, which keeps running, even if you close the shell. The ssh-agent command will print commands on stdout, which can be typed in to start using that ssh-agent. You would have to type those commands each time.

You can start a background ssh-agent and start using it in the current shell by typing eval $(ssh-agent). However you would have to store the SSH_AUTH_SOCK environment variable somewhere and set it if you log in again.

With that background information in place, I have three suggestions as to how to go about always having the agent available.

  • Use a screen session on the server. When you start the screen type eval $(ssh-agent) ; screen, that agent will stay running on the server, and if you connect to that screen again later, it will still remember which agent to use.
  • Insert commands in your ~/.bash_profile file to find the active agent, if one exists and set the SSH_AUTH_SOCK environment variable.
  • Use ssh agent forwarding to use the agent on the client rather than one on the server. Make sure you understand the security implications of that, before you use it.

Finding the ssh-agent from ~/.bash_profile could be done with a script looking like this


for P in /tmp/ssh-*/agent.*
    if [ -O "$P" ] && [ -O "$(dirname "$P")" ]
        L=$(SSH_AUTH_SOCK="$P" timeout 1s ssh-add -l > >(wc -l))
        case $? in
                echo "$L $P"
                echo "0 $P"
    fi &
done | sort -rn | if read N P
    echo "SSH_AUTH_SOCK='$P'; export SSH_AUTH_SOCK"

Which could be invoked from ~/.bash_profile like this eval $(find-agent) (assuming the script was named find-agent.)

share|improve this answer
thanks for your help, but when I did -->me# ssh-agent it said it open the agent but when I do -->me# ssh-add ~/.ssh/pr_key it says: Could not open a connection to your authentication agent. – Tzook Bar Noy May 5 '14 at 6:04
can you write the commandes you suggest to do line by line... Im a newbie so it will help me a lot – Tzook Bar Noy May 5 '14 at 6:16
Typing only ssh-agent will start an agent and print out the commands you need to type in, before you can communicate with it. Those commands you can type again in another shell. If you type eval $(ssh-agent) it will automatically run the needed commands instead of printing them on the screen. I forgot the eval part the first time around. – kasperd May 5 '14 at 8:02
why exactly do I need that ssh-agent? my ssh key has no password... I want to perform a simple ssh connection – Tzook Bar Noy May 5 '14 at 8:17
Then it is not clear to me, what you say is working and what you say is not working. What did you do, that worked on the local machine? And what server does it not work on? And how did you see that it does not work on that server? – kasperd May 5 '14 at 8:43

An ssh command may fail with the error message Permission denied (publickey)., but work if you load the same key into an agent beforehand, if you have misconfigured the ssh client.

By default ssh and ssh-add will load keys from the same places, so either both will find the needed key, or neither will find it. However in /etc/ssh/ssh_config and ~/.ssh/config, you can override the path used by ssh through the IdentityFile setting.

If ~/.ssh/config contains an incorrect IdentityFile setting, you will see the symptoms described in your post.

share|improve this answer

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.