Safer SSH agent forwarding

Vincent Bernat

ssh-agent is a pro­gram to hold in mem­ory the pri­vate keys used by SSH for pub­lic-key au­then­ti­ca­tion. When the agent is run­ning, ssh for­wards to it the sig­na­ture re­quests from the server. The agent per­forms the pri­vate key op­er­a­tions and re­turns the re­sults to ssh. It is use­ful if you keep your pri­vate keys en­crypted on disk and you don’t want to type the pass­word at each con­nec­tion. Keep­ing the agent se­cure is crit­i­cal: some­one able to com­mu­ni­cate with the agent can au­then­ti­cate on your be­half on re­mote servers.

ssh also pro­vides the abil­ity to for­ward the agent to a re­mote server. From this re­mote server, you can au­then­ti­cate to an­other server us­ing your lo­cal agent, with­out copy­ing your pri­vate key on the in­ter­me­di­ate server. As stated in the man­ual page, this is dangerous!

Agent for­ward­ing should be en­abled with cau­tion. Users with the abil­ity to by­pass file per­mis­sions on the re­mote host (for the agent’s UNIX-do­main socket) can ac­cess the lo­cal agent through the for­warded con­nec­tion. An at­tacker can­not ob­tain key ma­te­rial from the agent, how­ever they can per­form op­er­a­tions on the keys that en­able them to au­then­ti­cate us­ing the iden­ti­ties loaded into the agent. A safer al­ter­na­tive may be to use a jump host (see -J).

Up­date (2022-10)

See for ex­am­ple Ma­trix’ 2019 post-mortem about their se­cu­rity in­ci­dent in­volv­ing un­safe SSH agent for­warding.

As men­tioned, a bet­ter al­ter­na­tive is to use the jump host fea­ture: the SSH con­nec­tion to the tar­get host is tun­neled through the SSH con­nec­tion to the jump host. See the man­ual page and this blog post for more details.


If you re­ally need to use SSH agent for­ward­ing, you can se­cure it a bit through a ded­i­cated agent with two main at­tributes:

  • it holds only the pri­vate key to con­nect to the tar­get host; and
  • it asks con­fir­ma­tion for each re­quested signature.

The fol­low­ing alias around the ssh com­mand will spawn such an ephemeral agent:

alias assh="ssh-agent ssh -o AddKeysToAgent=confirm -o ForwardAgent=yes"

With the -o AddKeysToAgent=​confirm di­rec­tive, ssh adds the un­en­crypted pri­vate key to the agent but each use must be con­firmed.1 Once con­nected, you get a pass­word prompt for each sig­na­ture request:2

ssh-agent prompt confirmation with fingerprint and yes/no buttons
Request for the agent to use the specified private key

But, again, avoid us­ing agent for­warding! ☠️

Up­date (2020-04)

In a pre­vi­ous ver­sion of this ar­ti­cle, the wrap­per around the ssh com­mand was a more com­plex func­tion. Alexan­dre Oliva was kind enough to point me to the sim­pler so­lu­tion above.

Up­date (2020-04)

Guardian Agent is an even safer al­ter­na­tive: it shows and en­sures the us­age (tar­get and com­mand) of the re­quested sig­na­ture. There is also a wide range of al­ter­na­tive so­lu­tions to this prob­lem. See for ex­am­ple SSH-Ident, Wiki­me­dia so­lu­tion and solo-agent.

Up­date (2022-01)

OpenSSH 8.9 will be able to re­strict us­age of for­warded SSH agent keys. How­ever, the pro­tec­tion of­fered by these re­stric­tions is quite lim­ited. You should con­tinue to avoid us­ing agent for­warding.