ssh-add: Could not open a connection to your authentication agent
Frank Zhang
- 5 minutes read - 2293 wordsTL;DR
The title sentence means this process has no SSH_AUTH_SOCK. A set path that fails to connect is a different line. Print the variable, then start an agent only in the shell that will run ssh-add.
Match Could not open a connection to your authentication agent. before you generate a new key, edit UseKeychain, or paste a .pub anywhere. It shows up in Terminal on a Mac, and in git, ssh-add, or a GUI client on that Mac. Linux and Windows OpenSSH clients print the same sentence. This is not a Mac-only bug.
On the OpenSSH portable tree read for this page on 2026-11-24, that sentence means SSH_AUTH_SOCK is unset or empty in this process. ssh-add has not opened a socket path.
If the line is Error connecting to agent: …, the variable is set and the Unix-socket connect failed. Stay on this page. That is a different branch.
If the line is The agent has no identities., the agent is reachable and holds no keys. That is not this error. Adding a key and comparing fingerprints is the publickey note.
Print SSH_AUTH_SOCK first. Only then decide whether to start an agent in this shell with eval "$(ssh-agent -s)" for Bourne or zsh. Do not start a second agent if a live socket is already in the environment.
These commands were not re-run on a Mac, a VPS, WSL, or Git Bash for this page. Flag meanings and the exact strings come from the OpenSSH portable master files and the OpenBSD manuals linked below, as read on 2026-11-24, and from the GitHub docs fetched the same day. macOS ships its own build of OpenSSH. If man or the text on the machine in front of you disagrees, that copy wins.
What this page is for
Match the title sentence, the sibling Error connecting to agent: …, and the third sentence The agent has no identities. ssh-add prints the title sentence. ssh, git, and scp need the same SSH_AUTH_SOCK when they talk to an agent. This page does not claim that git always prints this exact ssh-add sentence.
People paste that English string from WSL, Git Bash, Windows, Linux, and Mac Terminal. One page covers the string. It does not become five environment guides.
In the ssh-add.c main read for this page, after getopt and log_init, the comment is “First, get a connection to the authentication agent.” The switch is:
switch (r = ssh_get_authentication_socket(&agent_fd)) {
case 0:
break;
case SSH_ERR_AGENT_NOT_PRESENT:
fprintf(stderr, "Could not open a connection to your "
"authentication agent.\n");
exit(2);
default:
fprintf(stderr, "Error connecting to agent: %s\n", ssh_err(r));
exit(2);
}
Both failure branches exit 2. ssh-add(1) says exit status 2 means ssh-add is unable to contact the authentication agent. That status covers both branches. The sentence on the screen is what separates them.
What it means
ssh-add(1), as read on 2026-11-24, says: “The authentication agent must be running and the SSH_AUTH_SOCK environment variable must contain the name of its socket for ssh-add to work.” ENVIRONMENT there says SSH_AUTH_SOCK “Identifies the path of a Unix-domain socket used to communicate with the agent.”
ssh-agent(1) ENVIRONMENT, same reading: when the agent starts, it stores the agent’s PID in SSH_AGENT_PID and the pathname of a Unix-domain socket in SSH_AUTH_SOCK. The socket “is accessible only to the current user, but is easily abused by root or another instance of the same user.” Do not share that socket with another user. FILES says $HOME/.ssh/agent/s.* should be readable only by the owner and removed when the agent exits. -T binds the socket in a randomised subdirectory of $TMPDIR instead.
In authfd.c ssh_get_authentication_socket, same reading:
authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
if (authsocket == NULL || *authsocket == '\0')
return SSH_ERR_AGENT_NOT_PRESENT;
return ssh_get_authentication_socket_path(authsocket, fdp);
ssh.h defines SSH_AUTHSOCKET_ENV_NAME as "SSH_AUTH_SOCK". On this tree the title sentence is printed only when that variable is unset or empty in this process. ssh-add has not tried to connect to a path. A stale socket is not this sentence.
ssh_get_authentication_socket_path opens an AF_UNIX SOCK_STREAM socket and connect()s to the path. If socket() fails, it returns SSH_ERR_SYSTEM_ERROR at once. If fcntl(sock, F_SETFD, FD_CLOEXEC) or connect() fails, it saves errno, closes the descriptor, restores errno, and returns SSH_ERR_SYSTEM_ERROR. That return is the default branch: Error connecting to agent: plus ssherr.c ssh_err. For SSH_ERR_SYSTEM_ERROR, ssh_err returns strerror(errno). The remainder is the C library’s text for that errno. A missing path is often No such file or directory. A path with nothing listening is often Connection refused. The words on your screen win. This Connection refused is a Unix-socket connect to the agent, not ssh: connect to host … port 22: Connection refused.
| Reader’s sentence | Layer | Where to go |
|---|---|---|
Could not open a connection to your authentication agent. | SSH_AUTH_SOCK unset or empty in this process | This page |
Error connecting to agent: … | Variable set; Unix-socket connect failed | This page, the other branch |
The agent has no identities. | Agent reachable, no keys | Permission denied (publickey) for git@github.com on a Mac |
ssh-add: illegal option -- apple-use-keychain / Bad configuration option: usekeychain | Which ssh / ssh-add is on PATH | UseKeychain on a Mac |
WARNING: UNPROTECTED PRIVATE KEY FILE | That private key file was ignored | UNPROTECTED PRIVATE KEY FILE |
Permission denied (publickey) | Server refused the offered keys | publickey note |
Too many authentication failures | Too many keys on this connection | Too many authentication failures |
ssh: connect to host … port 22: Connection refused | TCP, not the agent socket | Connection refused, from a Mac or into a Mac |
kex_exchange_identification / host-key warnings | Later or earlier network layers | kex_exchange_identification and REMOTE HOST IDENTIFICATION HAS CHANGED |
A tunnel whose address and port kept changing is the Chinese incident 一次ssh排错经历. That page is the tunnel and port layer. A Mac app launched from the Dock and a shell inside Terminal can see different values of SSH_AUTH_SOCK. The publickey note already records that split.
ssh-add.c main on this tree connects only through ssh_get_authentication_socket(). The file does not mention IdentityAgent. readconf.c appears once, in a comment about default known-hosts files, not as a parse of ~/.ssh/config. ssh_config(5) IdentityAgent, as read the same day, “overrides the SSH_AUTH_SOCK environment variable” for ssh. Setting it can make ssh work in a shell where ssh-add still prints the title sentence, because ssh-add looks only at the environment.
Read the variable first
In bash or zsh:
printf '%s\n' "${SSH_AUTH_SOCK-}"
If that prints a path:
ls -l "$SSH_AUTH_SOCK"
ssh-add -l
If it prints nothing, ssh-add -l on this tree prints the title sentence and exits 2. If it prints a path and the connect fails, the line is Error connecting to agent: and the exit status is also 2. Read the sentence. ls -l on a missing path is enough to show that the path is gone. This page does not invent ps or lsof output.
Working with SSH key passphrases, fetched on 2026-11-24, documents a Git for Windows auto-launch snippet and this comment: 0 means the agent is running and holds a key, 1 means the agent is running without a key, 2 means the agent is not running. That 2 matches both exit(2) branches above. The agent has no identities. is the empty agent: list_identities returns -1, and main returns 1. This page does not paste the script.
Branch: SSH_AUTH_SOCK is unset or empty
Start an agent in the shell that will run ssh-add. A subshell throws the variables away.
The OpenBSD ssh-agent(1) read for this page describes two main ways. At the start of an X session, run a command as a child of the agent, for example ssh-agent xterm &. When the command terminates, so does the agent. For a login session, ssh-agent prints shell commands and the calling shell evaluates them. The manual’s example puts ssh-agent -s in backticks inside eval. That is the same idea as the quoted form. GitHub’s generating-a-key page, fetched the same day, uses the quoted Bourne form for Linux and macOS:
eval "$(ssh-agent -s)"
-s generates Bourne shell commands, the default if SHELL does not look like csh. -c generates C-shell commands, the default if SHELL looks like csh. Use -c for csh or tcsh. Do not write eval $(ssh-agent) without quotes.
The portable ssh-agent.1 on GitHub master, read the same day, adds a third way: socket activation from a supervisor such as systemd, when ssh-agent is started with -d or -D, no -a address, and both LISTEN_FDS and LISTEN_PID are supplied. The OpenBSD HTML manual’s “two main ways” paragraph did not include that method. This page does not invent a systemd unit.
The agent starts with no private keys. Keys are added with ssh-add, or by ssh when AddKeysToAgent is set. This page does not walk through AddKeysToAgent or UseKeychain.
printf '%s\n' "${SSH_AUTH_SOCK-}"
ssh-add -l
The agent has no identities. is progress: this process opened the agent, and the agent holds no keys. Adding a key is the publickey note and GitHub’s generating-a-key page. Do not generate a new key until this process can talk to an agent. That page tells macOS to use Apple’s ssh-add and --apple-use-keychain. If that flag is the line on your screen, it is the UseKeychain page. This page stops before that flag.
For fish, or any shell that is neither Bourne nor csh, the agent still prints -s or -c commands. Follow that shell’s own manual to export SSH_AUTH_SOCK in the same session. This page does not invent a fish one-liner.
Branch: Error connecting to agent:
The path is set and connect() failed. The text after Error connecting to agent: is strerror(errno) on that machine.
This is general guidance. This page has no recorded session of these lines. The agent may have exited and left a stale path. This shell may have inherited an old SSH_AUTH_SOCK. A subshell, a container, or a CI job may have received a path that is not valid here.
If ls -l shows the path is gone, start a new agent in this shell with the same eval. If a known-good agent is already running in another terminal, copy that terminal’s SSH_AUTH_SOCK only for the same user on the same machine. Do not paste someone else’s socket. Do not chmod 777 the socket or ~/.ssh. Do not kill -9 every ssh-agent as the first step. That can drop keys another session still needs.
ssh-agent -k kills the agent named by SSH_AGENT_PID in this environment. ssh-agent(1) says -k will “Kill the current agent (given by the SSH_AGENT_PID environment variable).” It does not kill every ssh-agent on the machine.
Branch: Windows, Git Bash, and WSL
GitHub’s generating-a-key page starts the Windows OpenSSH ssh-agent service from an elevated PowerShell, then runs ssh-add from a terminal that is not elevated:
Get-Service -Name ssh-agent | Set-Service -StartupType Manual
Start-Service ssh-agent
Run that only after SSH_AUTH_SOCK in this process is unset or empty. Do not generate a key as that first step. Those lines were not re-run for this page. How the Windows service publishes the socket is not in the portable files read above.
The same page’s Troubleshooting SSH agent conflicts in Windows says Windows OpenSSH and Git for Windows’s bundled ssh.exe can both be installed, and that Git can still ask for a passphrase when it uses the bundled ssh.exe and therefore does not talk to the Windows ssh-agent service. This page does not extend that note into a service guide.
WSL is a Linux userland. SSH_AUTH_SOCK has to name a socket that exists inside that WSL session. This page does not describe a relay from the Windows agent into WSL. Git for Windows auto-launch stays on the passphrases page, including the exit-status comment already quoted.
Branch: the agent is up and empty, or a third-party agent
The agent has no identities. means this is no longer a missing socket. Add a key and compare fingerprints on the publickey note.
1Password and Bitwarden are a different agent. SSH_AUTH_SOCK in this shell has to point at that agent’s socket. This page is not a setup guide for either, and it links no unofficial page for them.
Where this page stops
- No session here reproduces these lines. The commands were not re-run on a Mac, a VPS, WSL, or Git Bash.
manon the machine wins. - This page states no search volume.
- A new key, a pasted
.pub,ssh-keygen -R, and aUseKeychainedit are not the first step. - No fish one-liner, no 1Password or Bitwarden setup, no CachyOS notes, and no container recipe beyond the inherited-path case above.
- Do not turn on
ForwardAgentorssh -Ato paper over a missing local socket. ssh-agent(1) points at the caveats in ssh(1). Forwarding does not createSSH_AUTH_SOCKin the shell that lacks it. - Do not
chmod 777the agent socket. Do not shareSSH_AUTH_SOCKacross users.
Other paths on the site start at Start Here. The Chinese writeup is ssh-add: Could not open a connection to your authentication agent:先看 SSH_AUTH_SOCK.
If this post helps, you can sponsor the blog. This is voluntary personal support, not a charitable donation.