Could not open a connection to your authentication agent. from
ssh-add means no SSH agent was reachable from the shell you ran it in. Start one
in that same shell with eval "$(ssh-agent -s)", then run ssh-add
again.
The eval matters: without it, an agent started in this test and
ssh-add still printed the same error.
The error
$ echo SSH_AUTH_SOCK=$SSH_AUTH_SOCK
SSH_AUTH_SOCK=
exit=0
$ ssh-add /root/.ssh/id_ed25519
Could not open a connection to your authentication agent.
exit=2
Why it happens
The shell had no SSH_AUTH_SOCK variable, as the first command shows, and
ssh-add exited with code 2. This is what a fresh container shell looked like in
this test: no agent had been started there. The key file itself was fine; the same key was
added without complaint a moment later, once an agent was running in that shell. So the
message is about the agent, not about the key you named on the command line.
Running ssh-agent -s on its own was not enough. It started an agent and printed
shell commands that set SSH_AUTH_SOCK and SSH_AGENT_PID, but nothing
ran them, so the variable stayed empty and ssh-add failed again
(devkey is the same key as id_ed25519, copied under another name
for a different test):
$ ssh-agent -s > /root/agent.out; cat /root/agent.out; ssh-add /root/.ssh/devkey; echo SSH_AUTH_SOCK=$SSH_AUTH_SOCK
SSH_AUTH_SOCK=/tmp/ssh-XkJekDpiZ9q1/agent.3438; export SSH_AUTH_SOCK;
SSH_AGENT_PID=3439; export SSH_AGENT_PID;
echo Agent pid 3439;
Could not open a connection to your authentication agent.
SSH_AUTH_SOCK=
exit=0
That is why the fix wraps the command in eval, which runs those lines in the
current shell. (That stray agent was stopped afterwards with ssh-agent -k.)
The fix
$ eval "$(ssh-agent -s)"; ssh-add /root/.ssh/id_ed25519; ssh-add -l; ssh -o BatchMode=yes dev@sshd hostname; ssh-agent -k
Agent pid 3289
Identity added: /root/.ssh/id_ed25519 (dev-client)
256 SHA256:hDINVqjvB1wu3zFtsdx6W7oPRkPrPjHqDP/8nZy5LEM dev-client (ED25519)
9aec0d80cdf9
unset SSH_AUTH_SOCK;
unset SSH_AGENT_PID;
echo Agent pid 3289 killed;
exit=0
In one shell, eval started the agent (Agent pid 3289),
ssh-add added the key, ssh-add -l listed it with its fingerprint,
and an SSH login to the test server worked. ssh-agent -k stopped the agent at
the end of the test. The commands run on one line because each bash -c in this
test is a new shell; in a terminal, run them one after the other in the same window.
How it was reproduced
Reproduced inside ubuntu:24.04 containers on one user-defined Docker network on
Docker engine 29.9.3, with OpenSSH_9.6p1 Ubuntu-3ubuntu13.20 as client and server (apt
packages openssh-client and openssh-server). The client's Ed25519 key
was made in its container with ssh-keygen; the server had a test user
dev with that public key. The containers and the network were removed
afterwards.
Frequently asked
- How do I fix Could not open a connection to your authentication agent?
- Start an agent in the same shell with eval "$(ssh-agent -s)", then run ssh-add again. In this test the key was added and the login worked right after.
- Why does ssh-add say it cannot connect to the agent?
- In both failing runs of this test SSH_AUTH_SOCK was empty in the shell where ssh-add ran, once with no agent at all and once with an agent started without eval.
- Why do I need eval with ssh-agent -s?
- ssh-agent -s prints the commands that set SSH_AUTH_SOCK and SSH_AGENT_PID. In this test, without eval the variable stayed empty and ssh-add failed; with eval it worked.
If the agent holds many keys and the server disconnects, see SSH: Too many authentication failures. More decoded errors in the Fixes category.