How to fix Error connecting to agent: Connection refused in SSH

SSHBEGINNERMEDIUMUpdated Jan 4, 2026· 254 views

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)"
  1. Start the SSH agent in your current shell session
  2. Verify the agent is running
  3. Add your SSH key to the agent
  4. Make SSH agent permanent (optional but recommended)
ssh — error
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

  1. The SSH agent process (ssh-agent) is not running on the system
  2. SSH_AUTH_SOCK environment variable is unset or points to the wrong location
  3. The ssh-agent socket was created but the agent process crashed or was killed
  4. Shell configuration removed agent startup commands (e.g., from .bashrc or .zshrc)
  5. Using a desktop environment (GNOME, KDE) that doesn't properly manage ssh-agent

How to fix "Error connecting to agent: Connection refused"

1Start the SSH agent in your current shell session

Run the appropriate command for your shell to start the SSH agent:

For bash/zsh:

bash
eval "$(ssh-agent -s)"

For tcsh/csh:

bash
eval `ssh-agent -c`

For fish:

bash
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:

bash
Agent pid 12345
2Verify the agent is running

Check that SSH_AUTH_SOCK is set and points to a valid socket:

bash
echo $SSH_AUTH_SOCK

You 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:

bash
ps aux | grep ssh-agent
3Add your SSH key to the agent

Now that the agent is running, add your private key:

bash
ssh-add ~/.ssh/id_rsa

Replace ~/.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:

bash
ssh-add -l
4Make SSH agent permanent (optional but recommended)

To 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):

bash
if [ -z "$SSH_AUTH_SOCK" ]; then
  eval "$(ssh-agent -s)" > /dev/null
fi

For zsh (add to ~/.zshrc):

bash
if [ -z "$SSH_AUTH_SOCK" ]; then
  eval "$(ssh-agent -s)" > /dev/null
fi

For fish (add to ~/.config/fish/config.fish):

fish
if test -z $SSH_AUTH_SOCK
  eval (ssh-agent -c)
end

After adding the code, reload your shell configuration:

bash
source ~/.bashrc
# or
source ~/.zshrc

Advanced 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.

Official resources & further reading