Fix ssh auth sock errors: find the broken agent link
In this article
SSH authentication socket errors happen when your SSH client cannot connect to the SSH agent because the socket file it was told to use is missing, stale, or pointing to the wrong location. The fix is to find that broken link—usually an environment variable like SSH_AUTH_SOCK—and either recreate the socket or update the variable to point to a live agent.
What the SSH agent and its socket actually do
The SSH agent is a background program that holds your decrypted private keys in memory so you don't have to type your passphrase every time you connect. When you run ssh-add or connect to a server, your SSH client talks to the agent through a Unix domain socket—a special file that acts as a communication endpoint. The path to that socket is stored in the SSH_AUTH_SOCK environment variable.
On Linux and macOS, the socket is usually a file under /tmp or /run/user/<uid>/, with a name like agent.12345. On Windows with OpenSSH, it is a named pipe, but the same principle applies: the client needs a valid, reachable endpoint.
When the agent starts, it creates the socket and sets SSH_AUTH_SOCK for that session. If the agent dies, is restarted, or the session is resumed without re-running ssh-agent, the socket file can disappear while the environment variable still points to it. That mismatch is the classic source of ssh auth sock errors.
Why the link breaks: common scenarios
- Agent killed or crashed – The process that owned the socket is gone, but your shell still has the old path in
SSH_AUTH_SOCK. - Session resumed – Using
tmuxorscreenafter a reboot or reconnection often leaves stale environment variables. - Multiple agents – A second agent started without cleaning up the first can create a new socket, but your shell still points to the old one.
- SSH forwarded socket – When you use
ssh -A, the remote side gets a temporary socket path. If the forwarding is interrupted, that path becomes invalid. - Systemd user environment – On Linux with systemd, the graphical session may set a socket path that is not available in a non-graphical login.
How to find the broken link
First, check what your client thinks the socket is:
- Run
echo $SSH_AUTH_SOCKto see the current path. - Check whether that file exists:
ls -l $SSH_AUTH_SOCK(orTest-Path $env:SSH_AUTH_SOCKon PowerShell). - If the file is missing, you have found the broken link.
- If the file exists, test the agent itself with
ssh-add -l. If it saysError connecting to agent, the socket is stale even though the file exists.
On Linux, you can also look for live agent sockets with find /tmp -name 'agent.*' -type s or ls /run/user/$(id -u)/. On macOS, the default is /tmp/com.apple.launchd.*/Listeners, but the environment variable is the authoritative source.
Fixing the broken link: step-by-step
The exact fix depends on why the link broke, but the general approach is:
- Restart the agent – Run
eval "$(ssh-agent -s)"on Linux/macOS, orStart-Service ssh-agenton Windows (if the service is set to manual). This creates a new socket and setsSSH_AUTH_SOCKfor the current shell. - Re-add your keys – After restarting, run
ssh-addand enter your passphrase(s). - Fix tmux/screen sessions – If you are in a detached session, you need to update the environment variable inside that session. One method is to run
tmux setenv SSH_AUTH_SOCK $SSH_AUTH_SOCKbefore starting new windows, or use a tool likessh-find-agentthat locates the live socket. - Remove stale variables – If you do not need the agent, you can simply
unset SSH_AUTH_SOCKand use key files directly (though this means typing your passphrase each time). - Check forwarding – If the error appears on a remote server, ensure you used
ssh -A(orForwardAgent yesin your config) and that the remote agent is running.
Preventing the problem from recurring
Once you have fixed the immediate error, you can reduce the chances of it happening again:
- Use a systemd user service for the agent on Linux, so it persists across logins.
- Add a shell alias or function that re-evaluates
ssh-agentwhen you notice the error. - In tmux, use the
update-environmentoption to refreshSSH_AUTH_SOCKon attach. - On macOS, the default agent is usually managed by launchd, so avoid starting a second one manually.
- If you frequently use forwarded agents, consider using
ssh -o ForwardAgent=yesexplicitly and verify that the remoteSSH_AUTH_SOCKis set.
When you don't need the agent at all
If you rarely use SSH keys with passphrases, you can bypass the agent entirely. Set SSH_AUTH_SOCK to an empty string or unset it, and SSH will prompt for your key passphrase directly. This is less convenient but eliminates socket errors. For automation, you can also use ssh-agent in a single command with ssh-add and then run your command, but that is only suitable for short-lived tasks.
Another alternative is to use a key without a passphrase (not recommended for security) or to use a hardware token like a YubiKey, which manages keys without a traditional agent socket.
Frequently Asked Questions
Why does SSH_AUTH_SOCK point to a file that doesn't exist?
Because the environment variable is set when the agent starts, but the agent may have been killed or the system may have cleaned up the temporary directory. The variable is not automatically updated when the agent stops.
Can I just delete the socket file?
If the file is stale, deleting it is harmless, but it will not fix the error because the variable still points to that path. You need to either restart the agent (which creates a new socket) or update the variable to a live socket.
Why does my SSH work in one terminal but not another?
Each terminal has its own environment. If you started the agent in one terminal, that terminal has the correct SSH_AUTH_SOCK. Other terminals may not have the variable set, or may have an old value from a previous session.
What does "Error connecting to agent: No such file or directory" mean?
It means the SSH client tried to open the socket file at the path given by SSH_AUTH_SOCK, but no file exists there. This is the most common manifestation of the broken link.
Does this happen on Windows too?
Yes, but the socket is a named pipe rather than a Unix file. The error message is usually the same, and the fix is similar: ensure the ssh-agent service is running and that SSH_AUTH_SOCK points to the correct pipe.
How do I find the live agent socket if I have multiple?
On Linux, list sockets with find /tmp -type s -name 'agent.*' 2>/dev/null and test each with SSH_AUTH_SOCK=/path/to/socket ssh-add -l. The one that lists keys without error is the live one. You can then set SSH_AUTH_SOCK to that path.