In SSH forwarding of credentials (ssh -A), does the remote ssh-agent store the credentials to be forwarded, or does the remote ssh-agent contact the local ssh-agent for credentials on demand, for each request?

This matters in scenarios like the following: 1. ssh -A to a remote host, 2. Run a long-running script requiring the SSH credentials (perhaps to scp some resources from somewhere else), 3. Disconnect from the remote host.

If the credentials are stored on the remote, this might fly, otherwise, the script will fail because the local ssh-agent may not be reachable after disconnect.


SSH agent forwarding via ssh -A does NOT store the (unencrypted) private key on the remote system. Rather it sets up a socket on the remote system that is tunneled back through the open ssh session to the user's running ssh-agent on the origin host. The location of the socket on the remote end will be stored in the value of the environment variable SSH_AUTH_SOCK and will likely be something like: /tmp/ssh-HASH/agent.PID

Therefore, you won't be able to do what you want using agent forwarding in the simple case, but unless your origin host is going to go offline entirely (e.g. your laptop), you could leave a connection open solely for the purpose of ssh-agent forwarding. Something like this:

ssh -fTA REMOTE "sleep infinity"

(aside: I tried that with ssh -fNTA ..., but it wouldn't create the remote agent socket, even though I use that when forwarding ports with success - dunno why yet).

CAVEAT: inherent to the design of agent forwarding, anyone with root privileges on the remote system will be able to read your unencrypted private keys out of your agent on your local/origin system through the socket. It's a risk, but not a deal-breaker, and certainly better than encoding passwords in files.

| improve this answer | |
  • crimson-egret, thanks so much for this. Is your conclusion (private key not cached on remote host) derived from documentation (if so, where?), experimentation, or is it something you consider to be well-known? – flow2k May 19 '19 at 8:53
  • 1
    Hey - that's an interesting question. I guess to me it's just something I learned. I went back and looked at the various man pages (start here: openssh.com/manual.html ) and it's never stated explicitly, but it is implied in the wording in ssh(1) and ssh_config(5). i.e. "ForwardAgent: [...] can access the local agent through the forwarded connection." Moreover, you can empirically validated this with multiple (non-MUXed) connections to the remote host and verify that the socket (in /tmp) goes away when you exit a session where you have forwarded the agent. – crimson-egret May 20 '19 at 1:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.