Strovent SystemsSign in
Reference

SSH agent forwarding not working: Could not open a connection to your authentication agent

Why ssh -A or ForwardAgent gives no keys on the remote host: agent empty, forwarding refused by sshd, SSH_AUTH_SOCK overwritten, or socket not created.

Agent forwarding has four links, and the remote ssh-add -l tells you which one is broken. Run it on the remote host first.

  • Could not open a connection to your authentication agent. The remote shell has no SSH_AUTH_SOCK (or it is empty). Forwarding was not set up for this login (sections 1 to 3).
  • Error connecting to agent: No such file or directory (or Permission denied). SSH_AUTH_SOCK is set, but the socket it names is gone or you cannot open it, for example an old value inside tmux or screen, or after sudo (section 5).
  • The agent has no identities. A socket was found, but the agent behind it holds no keys. Either your local agent is empty, or the remote SSH_AUTH_SOCK points to a different agent (sections 4 and 5).
  • A list of keys means forwarding works. If the next login still fails, the problem is that key on the next host, not forwarding.

ssh-add exits with status 2 when it cannot reach an agent at all, and 1 when the agent answers but the command fails.

1. The client did not ask

Forwarding is off by default. Turn it on for one login with ssh -A [email protected], or for one host in ~/.ssh/config:

Host build
    HostName host.example.com
    User user1
    ForwardAgent yes

Check what the client will actually use with ssh -G build | grep -i forwardagent. A -a on the command line, or an earlier matching Host block with ForwardAgent no, turns it off. In ssh_config the first value found wins.

2. The server refused

The server can refuse agent forwarding in three places:

  • AllowAgentForwarding no in sshd_config (the default is yes).
  • DisableForwarding yes in sshd_config, which turns off all forwarding and overrides the other options.
  • no-agent-forwarding or restrict in front of your key in ~/.ssh/authorized_keys on that server. restrict turns off agent, port and X11 forwarding unless agent-forwarding is added back.

An administrator can see the effective setting with sudo sshd -T | grep -i forwarding. A Match block can change it for some users or addresses only, so test with sudo sshd -T -C user=user1,host=client.example.com,addr=192.0.2.10.

3. The server could not create the socket

The server creates a socket for your forwarded agent and puts its path in SSH_AUTH_SOCK. If it cannot, forwarding is quietly off. Connect with ssh -v -A and look for a line such as:

debug1: Remote: Agent forwarding disabled: couldn't create listener socket

Older OpenSSH servers create the socket under /tmp, so a full /tmp or a /tmp without its normal mode (drwxrwxrwt, mode 1777) breaks it. Current OpenSSH creates it in ~/.ssh/agent in your home directory by default (the AgentSocketPath option), so a read-only or full home directory, wrong ownership, or an SELinux policy that blocks the new location can cause it. The server's log says which.

4. Your local agent is empty

Forwarding passes the agent through, not your key files. Run ssh-add -l on your own computer. If it says "The agent has no identities", add the key with ssh-add ~/.ssh/id_ed25519, or set AddKeysToAgent yes in ~/.ssh/config so the key is added after its first use.

A key loaded with a destination constraint (ssh-add -h, OpenSSH 8.9 and later) can be used only on the paths you listed. Through a forwarded agent, each hop has to be allowed, for example ssh-add -h host.example.com -h "host.example.com>git.example.com" ~/.ssh/id_ed25519.

5. The remote login replaced SSH_AUTH_SOCK

Run echo $SSH_AUTH_SOCK on the remote host. With forwarding working, it is a socket that sshd created for this login, under ~/.ssh/agent or a /tmp/ssh- directory. If it points somewhere else, a login script on the remote host started its own ssh-agent, or set SSH_AUTH_SOCK for a keyring or gpg-agent. That agent has none of your keys. Remove or guard that part of ~/.bashrc, ~/.profile or ~/.zshrc, for example by starting a local agent only when SSH_AUTH_SOCK is empty.

Two other common cases:

Before you turn it on

Anyone who can open the forwarded socket on the remote host, such as its root, can use your keys while you are connected, though they cannot copy them. If you only need to reach a host behind another one, a jump host does not expose the agent. See The agent and agent forwarding and Jump hosts (ProxyJump).

Sources

Spotted a mistake? Email [email protected].