Skip to content

SSH agent forwarding — when to use it, when to avoid it, what to use instead

Agent forwarding solves the "I need my key on the bastion" problem, but exposes your agent socket to anyone with root on the bastion. Here is the safer pattern we recommend instead.

Agent forwarding (ssh -A, or ForwardAgent yes in client config) is the easy answer to “I need to clone a private repo from the bastion.” The downside is that for the duration of your session, anyone with root on the bastion can talk to your ssh-agent and sign challenges with your private keys — without ever stealing the key file. This article documents when agent forwarding is acceptable, when it is not, and the modern alternatives we use in production.

How to verify

Inspect what is currently exposed.

echo "$SSH_AUTH_SOCK"
ls -la "$SSH_AUTH_SOCK" 2>/dev/null
ssh-add -l
# On a bastion you SSHed into with -A:
echo "$SSH_AUTH_SOCK"        # should be /tmp/ssh-XXXX/agent.NNNN
ssh-add -l                   # lists *your workstation* keys, accessible to root@bastion

Once you see your workstation’s keys listed from a bastion session, you have proven the bastion can sign with them.

What’s happening

ssh-agent is a small daemon on your workstation that holds decrypted private keys in memory. SSH connections that need to sign a challenge talk to it through a UNIX socket ($SSH_AUTH_SOCK). Agent forwarding extends this: when you SSH with -A, the remote sshd creates a socket on the remote machine and proxies all signing requests back to your workstation’s agent.

The exposure is at the remote socket. The socket file is owned by your remote user, mode 0600, in /tmp/ssh-XXXX/. Anyone on the remote host who can read that file or who runs as root can SSH_AUTH_SOCK=/path/to/socket ssh-add -l and sign with your keys for the duration of your session. They cannot exfiltrate the key, but they can use it. If the bastion is compromised, every host that trusts your key is reachable from the bastion.

The fix is to not put the agent on the bastion at all. ProxyJump (introduced in OpenSSH 7.3) tunnels the connection through the bastion using the bastion only as a TCP forwarder — the authentication happens between your workstation and the target host directly, end-to-end. The bastion never sees your agent. This is the pattern we use by default; agent forwarding is reserved for the few cases where you genuinely need to use your local key from the remote shell (e.g. cloning a private repo from the bastion itself).

The decision

When to not forward the agent:

  • The bastion is a shared host. Even a “trusted” admin team is one compromise away from holding your signing oracle.
  • You are SSHing through the bastion to reach a target host. Use ProxyJump instead — the bastion is just a TCP proxy.
  • You only need to authenticate on the remote host (no further outbound key-using actions). Keys on the bastion’s authorized_keys are enough.

When agent forwarding is acceptable:

  • The remote host genuinely needs to use your key for outbound actions you cannot reasonably move (a one-off interactive task pulling a private repo).
  • The remote host is single-tenant, you control it, and the session is short.

The safer alternatives for the “I need to clone from a private repo” case:

  • A deploy key per server, scoped to read-only on the specific repo.
  • A machine user account on the Git host with its own key pair.
  • A short-lived OAuth token cached in the user’s shell for the duration of the task.

The procedure

  1. Default to ProxyJump for bastion hops. In ~/.ssh/config:

    Host bastion
        HostName bastion.example.net
        User ops
    
    Host internal-*
        ProxyJump bastion
        User ops
        IdentityFile ~/.ssh/id_ed25519_ops

    ssh internal-db-01 now opens a TCP tunnel through the bastion and authenticates against internal-db-01 directly. The bastion’s sshd handles only the proxy step.

  2. If you must forward the agent, scope it to one host. Never set ForwardAgent yes in the global Host * block — that exposes your agent on every connection.

    Host bastion
        HostName bastion.example.net
        User ops
        ForwardAgent yes    # ONLY on this host, only when needed
  3. When forwarding, use the ssh-add -c confirm flag. This makes the agent prompt for confirmation (locally) for every signature request — so a malicious bastion cannot silently sign 1000 challenges.

    ssh-add -c ~/.ssh/id_ed25519_ops

    The prompt appears on your workstation’s display each time the key signs. Annoying for some workflows; defensible for any session through a host you do not fully trust.

  4. Use time-limited keys for forwarded sessions. ssh-add -t 1h puts the key in the agent for one hour. After expiry, forwarding fails — limits the window.

    ssh-add -t 3600 ~/.ssh/id_ed25519_ops
  5. Audit forwarded-agent usage server-side. sshd logs the forwarding request when it creates the socket; centralize those logs.

    sudo journalctl -u ssh -g 'agent forwarding' --since today

    See journalctl-log-reading for the broader patterns.

  6. Set AllowAgentForwarding no on hosts that should never see the agent. On a database host or a worker node, there is no reason for ssh-agent to be present.

    # /etc/ssh/sshd_config.d/02-no-agent-fwd.conf
    AllowAgentForwarding no

    Reload sshd and verify with sshd -T | grep allowagentforwarding.

  7. For Git operations from a remote host, prefer signed commits and read-only deploy keys over forwarded agents. A deploy key on the server, scoped to read-only on one repo, is reachable only from that server and is trivially revocable.

Operational notes

  • ssh -A overrides ForwardAgent no in config. The reverse is not true. The -A on the command line wins.
  • The socket on the remote host is owned by the remote user, not root, but root can read any file. On a multi-tenant host (rare on modern infra but common on legacy), this is a hard problem.
  • Mac users with ssh-add --apple-use-keychain should remember the keychain unlocks at login; agent forwarding from a long-running login session can be active for days.
  • macOS Ventura and later restrict agent forwarding through some VPN clients; if ssh-add -l from the remote host returns no keys despite -A, check that nothing in the local environment is rewriting $SSH_AUTH_SOCK.
  • Forwarding the agent and forwarding X11 (-X/-Y) are unrelated controls with similar caveats; turn both off if you do not need them.

In the engagements we run, agent forwarding is disabled by default in the standard sshd config, and bastion hops use ProxyJump everywhere. When a workflow genuinely needs the agent on a remote host, it is documented, time-limited, and scoped to one specific host. That discipline is part of how Managed Operations keeps the SSH attack surface bounded even as the operator population grows.