How to fix Error connecting to agent: Connection refused in SSH
This error occurs when SSH cannot communicate with the ssh-agent service, typically because the agent isn't running. Restart the agent using eval "$(ssh-agent -s)" to resolve the issue.
Quick fix
Fixes Error connecting to agent: Connection refused
eval "$(ssh-agent -s)"- Start the SSH agent in your current shell session
- Verify the agent is running
- Add your SSH key to the agent
- Make SSH agent permanent (optional but recommended)
eval "$(ssh-agent -s)"Error connecting to agent: Connection refused
What this error means
The SSH agent is a background service that manages your SSH keys and handles authentication. When you run ssh-add or attempt SSH operations, they communicate with the agent through a Unix socket. This error means the SSH client cannot connect to that socket because the agent process isn't running or the socket path is incorrect. The agent is essential for SSH key-based authentication, allowing you to authenticate without entering passwords. Without it, ssh-add cannot add keys, and SSH connections may fail if not properly configured.
Typical symptoms
- ssh-add fails with 'Error connecting to agent: Connection refused'
- SSH commands fail without an agent running (in some configurations)
- Private key authentication doesn't work without manually adding keys
- SSH_AUTH_SOCK environment variable points to a non-existent socket
- CI/CD pipelines fail at SSH authentication steps
Common causes
- The SSH agent process (ssh-agent) is not running on the system
- SSH_AUTH_SOCK environment variable is unset or points to the wrong location
- The ssh-agent socket was created but the agent process crashed or was killed
- Shell configuration removed agent startup commands (e.g., from .bashrc or .zshrc)
- Using a desktop environment (GNOME, KDE) that doesn't properly manage ssh-agent
How to fix "Error connecting to agent: Connection refused"
Run the appropriate command for your shell to start the SSH agent:
For bash/zsh:
eval "$(ssh-agent -s)"For tcsh/csh:
eval `ssh-agent -c`For fish:
eval (ssh-agent -c)This command starts the agent in the background and sets the SSH_AUTH_SOCK and SSH_AGENT_PID environment variables in your current session. You should see output like:
Agent pid 12345Check that SSH_AUTH_SOCK is set and points to a valid socket:
echo $SSH_AUTH_SOCKYou should see a path like /tmp/ssh-XXXXXXXXXX/agent.xxxxx (on Linux/macOS) or similar. If empty, the agent didn't start properly.
Also verify the agent process is running:
ps aux | grep ssh-agentNow that the agent is running, add your private key:
ssh-add ~/.ssh/id_rsaReplace ~/.ssh/id_rsa with your actual private key path if different. If your key is passphrase-protected, you'll be prompted to enter it once. After that, the agent holds the key in memory and uses it for authentication automatically.
Verify the key was added:
ssh-add -lTo avoid starting the agent manually every time you open a new shell, add it to your shell's startup file:
For bash (add to ~/.bashrc):
if [ -z "$SSH_AUTH_SOCK" ]; then
eval "$(ssh-agent -s)" > /dev/null
fiFor zsh (add to ~/.zshrc):
if [ -z "$SSH_AUTH_SOCK" ]; then
eval "$(ssh-agent -s)" > /dev/null
fiFor fish (add to ~/.config/fish/config.fish):
if test -z $SSH_AUTH_SOCK
eval (ssh-agent -c)
endAfter adding the code, reload your shell configuration:
source ~/.bashrc
# or
source ~/.zshrcAdvanced notes
For GNOME/KDE desktop users: If you're using GNOME or KDE, your desktop environment may manage ssh-agent through GNOME Keyring or KDE Wallet instead. This can cause SSH_AUTH_SOCK to point to a different location. If you experience persistent issues, you can disable the desktop's agent and use the standalone ssh-agent instead.
For oh-my-zsh users: If you use oh-my-zsh with the ssh-agent plugin, ensure you haven't enabled agent-forwarding with zstyle :omz:plugins:ssh-agent agent-forwarding on, as this can interfere with local agent functionality.
SSH_AUTH_SOCK in containers/remote environments: When using SSH agent forwarding to Docker containers, Kubernetes pods, or remote machines via SSH, the agent socket path must be forwarded correctly. The socket is specific to your local system and isn't directly accessible in remote environments—you need to explicitly forward it using -v flags in Docker or -R flags in SSH.
Agent forwarding security: Never forward your SSH agent to untrusted systems, as having access to the agent socket grants access to all your SSH keys.
Frequently asked questions
- What does "Error connecting to agent: Connection refused" mean?
- The SSH agent is a background service that manages your SSH keys and handles authentication. When you run ssh-add or attempt SSH operations, they communicate with the agent through a Unix socket. This error means the SSH client cannot connect to that socket because the agent process isn't running or the socket path is incorrect.
- How do I fix "Error connecting to agent: Connection refused"?
- This error occurs when SSH cannot communicate with the ssh-agent service, typically because the agent isn't running. Restart the agent using eval "$(ssh-agent -s)" to resolve the issue.
- What causes "Error connecting to agent: Connection refused"?
- The SSH agent process (ssh-agent) is not running on the system SSH_AUTH_SOCK environment variable is unset or points to the wrong location The ssh-agent socket was created but the agent process crashed or was killed
- Is the "Error connecting to agent: Connection refused" error serious?
- It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.