How to fix ssh-add: Could not add identity: communication with agent failed in SSH

SSHBEGINNERMEDIUMUpdated Jan 4, 2026ยท 282 views

This error occurs when the ssh-add command cannot communicate with the SSH agent process. The agent may not be running, the socket file may be missing, or environment variables may be misconfigured. This is a common issue on new terminal sessions or after system restarts.

Quick fix

Fixes ssh-add: Could not add identity: communication with agent failed

ps aux | grep ssh-agent
  1. Check if SSH agent is running
  2. Start SSH agent (Linux/macOS)
  3. Start SSH agent (Windows PowerShell)
  4. Add SSH agent initialization to shell configuration (Linux/macOS)
ssh โ€” error
ps aux | grep ssh-agent
ssh-add: Could not add identity: communication with agent failed

What this error means

SSH agent is a background process that manages your SSH keys in memory, eliminating the need to enter passphrases repeatedly. When you run ssh-add, it sends commands to the agent via a Unix socket (on Linux/macOS) or named pipe (on Windows). This error means ssh-add cannot find or communicate with that socket. Either the agent process is not running, the socket file has been deleted, or the SSH_AUTH_SOCK environment variable points to an invalid location.

Typical symptoms

  • ssh-add command fails immediately with 'communication with agent failed'
  • In a new terminal session after opening a terminal or SSH connection
  • Works in one terminal tab but fails in another tab
  • Works on Linux/macOS but fails on Windows (PowerShell or Git Bash)
  • SSH_AUTH_SOCK environment variable is empty or missing
  • SSH key authentication works manually with explicit -i flag but ssh-add fails

Common causes

  1. SSH agent is not running in the current session (most common)
  2. SSH_AUTH_SOCK environment variable is not set or points to a non-existent socket
  3. Agent socket file was deleted or cleaned up by the system
  4. Running in a new terminal/shell that hasn't initialized the agent
  5. Starting a GUI application directly (not via shell) so it doesn't inherit agent variables
  6. SSH_ASKPASS or SSH_ASKPASS_REQUIRE misconfiguration blocking the agent
  7. On Windows: OpenSSH Authentication Agent service is not running
  8. On GNOME Linux: GNOME Keyring may be interfering with SSH agent
  9. Permissions issue with the SSH socket file or ~/.ssh directory

How to fix "ssh-add: Could not add identity: communication with agent failed"

1Check if SSH agent is running

First, verify whether ssh-agent is actually running in your current session.

On Linux/macOS:

bash
ps aux | grep ssh-agent

Look for a line that shows ssh-agent (not grep). If you don't see it, the agent is not running.

Alternatively, check the environment variables:

bash
echo $SSH_AUTH_SOCK
echo $SSH_AGENT_PID

Both should be set. If they're empty, the agent is not initialized.

On Windows (PowerShell):

powershell
Get-Service ssh-agent | Select-Object Status

Should show Running. If it shows Stopped, proceed to Step 2.

2Start SSH agent (Linux/macOS)

If ssh-agent is not running, start it with:

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

This command:
- Starts ssh-agent in the background
- Sets SSH_AUTH_SOCK and SSH_AGENT_PID environment variables
- Makes them available to all subsequent commands in that shell

You should see output like:

bash
Agent pid 1234

Now try ssh-add again:

bash
ssh-add ~/.ssh/id_rsa

It should work. Verify with:

bash
ssh-add -l

This lists all identities currently managed by the agent.

3Start SSH agent (Windows PowerShell)

If you're on Windows and the ssh-agent service is stopped, start it.

As a one-time fix (for current session):

powershell
Start-Service ssh-agent

Then try ssh-add:

powershell
ssh-add C:\Users\YourUsername\.ssh\id_rsa

To make it automatic (recommended):

powershell
Set-Service -Name ssh-agent -StartupType Automatic
Start-Service ssh-agent

This sets ssh-agent to start automatically on every reboot, so you won't encounter this issue again.

4Add SSH agent initialization to shell configuration (Linux/macOS)

To avoid this error in future terminal sessions, add the agent initialization to your shell startup file.

For Bash, add this to ~/.bashrc:

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

For Zsh, add this to ~/.zshrc:

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

Then reload your shell:

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

Now every time you open a new terminal, SSH agent will be automatically started if it's not already running.

5Check socket file permissions (Linux/macOS)

If the agent is running but ssh-add still fails, check the socket file permissions.

bash
ls -la $SSH_AUTH_SOCK

The socket should be owned by your user with restrictive permissions:

bash
srwx------ 1 youruser staff 0 Jan  4 10:30 /tmp/ssh-XXXXXXXXXX/agent.12345

If permissions are wrong, fix them:

bash
chmod 700 $(dirname $SSH_AUTH_SOCK)
chmod 600 $SSH_AUTH_SOCK

If the socket file doesn't exist, restart the agent:

bash
eval "$(ssh-agent -s)"
6Handle GNOME Keyring conflict (Linux)

On GNOME Desktop (Ubuntu, Fedora), the display manager may set SSH_AUTH_SOCK to GNOME Keyring instead of ssh-agent, causing conflicts.

Check which agent is active:

bash
echo $SSH_AUTH_SOCK
file $SSH_AUTH_SOCK

If it shows a GNOME Keyring path instead of /tmp/ssh-*/agent.*, that's the problem.

Solution: Start SSH agent in your shell configuration AFTER login. Add to ~/.bashrc or ~/.zshrc:

bash
if [ -S "$SSH_AUTH_SOCK" ]; then
  # Check if current SSH_AUTH_SOCK is GNOME Keyring
  if file "$SSH_AUTH_SOCK" | grep -q "socket"; then
    # It's a real socket, keep it
    true
  else
    # Start our own ssh-agent
    eval "$(ssh-agent -s)"
  fi
else
  eval "$(ssh-agent -s)"
fi

Then open a new shell (important: use a shell command, not a GUI shortcut) and test:

bash
ssh-add ~/.ssh/id_rsa
7Add SSH key to agent after starting it

Once SSH agent is running, add your private keys.

Basic usage:

bash
ssh-add ~/.ssh/id_rsa

If your key has a passphrase, you'll be prompted to enter it once. The agent caches it in memory for the session.

Add all keys in ~/.ssh:

bash
ssh-add ~/.ssh/id_*

Verify keys are loaded:

bash
ssh-add -l

Should list your keys with their fingerprints.

Remove a key from agent (optional):

bash
ssh-add -d ~/.ssh/id_rsa

Remove all keys from agent (optional):

bash
ssh-add -D

Advanced notes

Advanced troubleshooting:

SSH_ASKPASS and SSH_ASKPASS_REQUIRE: In some environments, these variables are set to prevent interactive prompts. If ssh-add fails silently, check if these are set:

bash
echo $SSH_ASKPASS
echo $SSH_ASKPASS_REQUIRE

Unset them if they're causing issues:

bash
unset SSH_ASKPASS
unset SSH_ASKPASS_REQUIRE

Debugging agent communication: To see detailed logs, use:

bash
ssh-add -vvv ~/.ssh/id_rsa

The verbose output shows exactly where the communication fails (e.g., socket not found, permission denied, etc.).

SSH agent socket cleanup: On some systems, stale socket files in /tmp accumulate. If you see many agent processes but ssh-add fails, manually clean up:

bash
pkill -9 ssh-agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_rsa

Remote SSH sessions: If you're SSH'd into another machine and can't use ssh-add on that remote machine, you need to forward your local agent. Use -A flag:

bash
ssh -A user@remote-host

Then ssh-add on the remote machine will use your local agent.

Frequently asked questions

What does "ssh-add: Could not add identity: communication with agent failed" mean?
SSH agent is a background process that manages your SSH keys in memory, eliminating the need to enter passphrases repeatedly. When you run ssh-add, it sends commands to the agent via a Unix socket (on Linux/macOS) or named pipe (on Windows). This error means ssh-add cannot find or communicate with that socket.
How do I fix "ssh-add: Could not add identity: communication with agent failed"?
This error occurs when the ssh-add command cannot communicate with the SSH agent process. The agent may not be running, the socket file may be missing, or environment variables may be misconfigured. This is a common issue on new terminal sessions or after system restarts.
What causes "ssh-add: Could not add identity: communication with agent failed"?
SSH agent is not running in the current session (most common) SSH_AUTH_SOCK environment variable is not set or points to a non-existent socket Agent socket file was deleted or cleaned up by the system
Is the "ssh-add: Could not add identity: communication with agent failed" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading