Strovent SystemsSign in
Reference

ssh-add "Could not open a connection to your authentication agent": start the agent

ssh-add finds the agent only through SSH_AUTH_SOCK. The error means it is not set in this shell. Start one with eval "$(ssh-agent -s)", then run ssh-add.

ssh-add and ssh find the agent through one environment variable, SSH_AUTH_SOCK. It holds the path of the socket the agent listens on. Each message tells you how far ssh-add got:

  • "Could not open a connection to your authentication agent." SSH_AUTH_SOCK is not set, or is empty, in this shell. Either no agent was started, or one was started without setting the variable here.
  • "Error connecting to agent: No such file or directory". The variable is set, but no socket exists at that path. The agent has ended, the computer restarted, or the value is left over from an older session.
  • "Error connecting to agent: Connection refused". The socket file is there, but no agent is listening on it, for example after the agent was killed and could not remove its socket.
  • "Error connecting to agent: Permission denied". The socket belongs to another user, usually after sudo or su. See SSH agent not available after sudo or su.

In all four cases ssh-add exits with status 2. It exits with 1 when it reaches the agent but the agent has no keys or refuses the request.

Start an agent in this shell

Running ssh-agent on its own does not fix it. The agent starts in the background and prints the shell commands that would set SSH_AUTH_SOCK and SSH_AGENT_PID, but nothing runs those commands, so your shell never learns where the agent is. Let the shell run them with eval:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

The first line prints Agent pid and a number. -s makes the agent print commands for sh, bash and zsh. In csh or tcsh, use -c instead of -s. Run ssh-add -l to list the keys the agent now holds.

The agent belongs to the shell that started it

The variables exist only in that shell and in programs started from it. A new terminal window or tab does not have them, so ssh-add there gives the same error. Each eval "$(ssh-agent -s)" also starts another agent process, and closing the terminal does not stop it.

  • To stop the agent the current shell uses, run eval "$(ssh-agent -k)". ssh-agent -k kills the process named in SSH_AGENT_PID and prints the commands that unset both variables.
  • To check whether something already started an agent for your login, run echo "$SSH_AUTH_SOCK" before starting another. A desktop session or keyring may already have started one and set the variable for every window.

One agent for every shell

To reuse a single agent at a fixed path, put this in ~/.bashrc or ~/.zshrc:

export SSH_AUTH_SOCK="$HOME/.ssh/agent.sock"
ssh-add -l > /dev/null 2>&1
if [ $? -eq 2 ]; then
    rm -f "$SSH_AUTH_SOCK"
    eval "$(ssh-agent -a "$SSH_AUTH_SOCK" -s)" > /dev/null
fi

ssh-add -l returns 2 only when no agent answers on that path, so a new agent starts only then. -a tells the agent which socket path to create. The rm -f removes a stale socket file, which would otherwise make the agent fail to bind.

To have keys added on first use instead of running ssh-add yourself, set AddKeysToAgent yes in ~/.ssh/config. That needs a reachable agent too.

Where the socket lives

OpenSSH 10.1 and later create the agent socket in ~/.ssh/agent/ by default; older versions use a /tmp/ssh- directory. ssh-agent -T puts it back in /tmp. Either way, ssh-add uses only the path in SSH_AUTH_SOCK, so a value copied from another session or another computer points at nothing.

Inside tmux or screen, a reattached session can keep an old SSH_AUTH_SOCK from an earlier login. That gives "No such file or directory" even though an agent is running. See Check if a shell is running over SSH, inside tmux, or inside screen.

Windows

The OpenSSH client built into Windows works differently:

  • The agent is a Windows service named ssh-agent, and it is disabled by default. Microsoft's instructions enable and start it from an administrator PowerShell window:
  Get-Service ssh-agent | Set-Service -StartupType Automatic
  Start-Service ssh-agent
  • Windows ssh-add talks to the service through the named pipe \\.\pipe\openssh-ssh-agent, but only when SSH_AUTH_SOCK is not set. If another tool has set SSH_AUTH_SOCK to a Unix-style path such as /tmp/ssh-..., Windows ssh-add tries that path and reports "No such file or directory" even with the service running. Remove the variable from that window and from your user environment variables.
  • Git Bash, Cygwin and WSL normally run their own Unix-style ssh-add. In those shells the rules above apply, and keys held by the Windows service are not visible there.

On the remote host

If you see the message on a server after ssh -A, the agent you want is the one on your own computer, reached through forwarding. Starting a new agent on the server is the wrong fix. See SSH agent forwarding not working.

Sources

Spotted a mistake? Email [email protected].