Did You Start ssh-agent
?
You might need to start ssh-agent
before you run the ssh-add
command:
eval `ssh-agent -s`
ssh-add
Note that this will start the agent for msysgit Bash on Windows. If you’re using a different shell or operating system, you might need to use a variant of the command, such as those listed in the other answers.
Why do we need to use eval
instead of just ssh-agent
?
SSH needs two things in order to use ssh-agent: an ssh-agent instance running in the background, and an environment variable set that tells SSH which socket it should use to connect to the agent (SSH_AUTH_SOCK
IIRC). If you just run ssh-agent
then the agent will start, but SSH will have no idea where to find it.
Public vs Private Keys
Also, whenever I use ssh-add
, I always add private keys to it. The file ~/.ssh/id_rsa.pub
looks like a public key, I’m not sure if that will work. Do you have a ~/.ssh/id_rsa
file? If you open it in a text editor, does it say it’s a private key?
Could not open a connection to your authentication agent? Solution here.
I tried the other solutions to no avail. I made more research and found that the following command worked. I am using Windows 7 and Git Bash.
eval $(ssh-agent)
Answer #2:
The following command worked for me. I am using CentOS.
exec ssh-agent bash
Could not open a connection to your authentication agent? Answer #3:
To resolve this error:
bash:
$ eval `ssh-agent -s`
tcsh:
$ eval `ssh-agent -c`
Then use ssh-add
as you normally would.
Hot Tip:
I was always forgetting what to type for the above ssh-agent commands, so I created an alias in my .bashrc
file like this:
alias ssh-agent-cyg='eval `ssh-agent -s`'
Now instead of using ssh-agent
, I can use ssh-agent-cyg
E.g.
$ ssh-agent-cyg
SSH_AUTH_SOCK=/tmp/ssh-n16KsxjuTMiM/agent.32394; export SSH_AUTH_SOCK;
SSH_AGENT_PID=32395; export SSH_AGENT_PID;
echo Agent pid 32395;
$ ssh-add ~/.ssh/my_pk
Answer #4:
MsysGit or Cygwin
If you’re using Msysgit or Cygwin:
- Add a file called
.bashrc
to your home folder. - Open the file and paste in:
#!/bin/bash eval `ssh-agent -s` ssh-add
- This assumes that your key is in the conventional
~/.ssh/id_rsa
location. If it isn’t, include a full path after thessh-add
command. - Add to or create file
~/.ssh/config
with the contentsForwardAgent yes
In the original tutorial theForwardAgent
param isYes
, but it’s a typo. Use all lowercase or you’ll get errors. - Restart Msysgit. It will ask you to enter your passphrase once, and that’s it (until you end the session, or your ssh-agent is killed.)
Mac/OS X
If you don’t want to start a new ssh-agent every time you open a terminal, check out the solution below using Keychain in Mac OS:
Update: A better solution on Mac is to add your key to the Mac OS Keychain:
ssh-add -K ~/.ssh/id_rsa
Answer #5:
ssh-add and ssh (assuming you are using the openssh implementations) require an environment variable to know how to talk to the ssh agent. If you started the agent in a different command prompt window to the one you’re using now, or if you started it incorrectly, neither ssh-add nor ssh will see that environment variable set (because the environment variable is set locally to the command prompt it’s set in).
You don’t say which version of ssh you’re using, but if you’re using cygwin’s, you can use this recipe from SSH Agent on Cygwin:
# Add to your Bash config file
SSHAGENT=/usr/bin/ssh-agent
SSHAGENTARGS="-s"
if [ -z "$SSH_AUTH_SOCK" -a -x "$SSHAGENT" ]; then
eval `$SSHAGENT $SSHAGENTARGS`
trap "kill $SSH_AGENT_PID" 0
fi
This will start an agent automatically for each new command prompt window that you open (which is suboptimal if you open multiple command prompts in one session, but at least it should work).
Answer #6:
I faced the same problem for Linux, and here is what I did:
Basically, the command ssh-agent starts the agent, but it doesn’t really set the environment variables for it to run. It just outputs those variables to the shell.
You need to:
eval `ssh-agent`
and then do ssh-add.
Answer #7:
Instead of using ssh-agent -s
, I used eval `ssh-agent -s`
to solve this issue.
Here is what I performed step by step (step 2 onwards on Git Bash):
- Cleaned up my .ssh folder at
C:\user\<username>\.ssh\
- Generated a new SSH key:
ssh-keygen -t rsa -b 4096 -C "xyz@abc.com"
- Check if any process id(ssh agent) is already running.
ps aux | grep ssh
- (Optional) If found any in step 3, kill those
kill <pids>
- Started the SSH agent
$ eval `ssh-agent -s`
- Added SSH key generated in step 2 to the SSH agent
ssh-add ~/.ssh/id_rsa
Could not open a connection to your authentication agent in Windows- Answer #8:
In Windows 10 I tried all answers listed here, but none of them seemed to work. In fact, they give a clue. To solve a problem, simply you need three commands. The idea of this problem is that ssh-add needs the SSH_AUTH_SOCK and SSH_AGENT_PID environment variables to be set with the current ssh-agent sock file path and pid number.
ssh-agent -s > temp.txt
This will save the output of ssh-agent in a file. The text file content will be something like this:
SSH_AUTH_SOCK=/tmp/ssh-kjmxRb2764/agent.2764; export SSH_AUTH_SOCK;
SSH_AGENT_PID=3044; export SSH_AGENT_PID;
echo Agent pid 3044;
Copy something like “/tmp/ssh-kjmxRb2764/agent.2764” from the text file and run the following command directly in the console:
set SSH_AUTH_SOCK=/tmp/ssh-kjmxRb2764/agent.2764
Copy something like “3044” from the text file and run the following command directly in the console:
set SSH_AGENT_PID=3044
Now when environment variables (SSH_AUTH_SOCK and SSH_AGENT_PID) are set for the current console session, run your ssh-add command and it will not fail again to connect to ssh agent.
Hope you learned something from this post.
Follow Programming Articles for more!