When I ssh into a server and my key isn't loaded in the agent, I have to abort, then run ssh-add to get my key into the agent. I'd like the ssh command to add the key automatically if it isn't present, though. Is this possible?

  • Do you have one key for all your target hosts, or do you have different keys with different passwords? Are you using putty from Windows or are you using an X based windowing system on Linux or are you in a Mac?
    – Otheus
    Mar 11, 2016 at 10:34
  • I'm using just one key and openssh. ssh already figures out the right key and asks me for the passphrase, but after I enter it, the key won't get saved to the agent. This behaviour is the same on all POSIX systems and cygwin. Mar 11, 2016 at 10:38
  • Does it look similar? stackoverflow.com/questions/3466626/…
    – techraf
    Mar 11, 2016 at 10:55
  • No, that's different. I want to enter the key password every time the agent is stopped. I'd just like ssh to add the key to the agent directly as needed, rather than having to use ssh-add first. Mar 11, 2016 at 11:05
  • Also check out UseKeyChain to keep keys between reboots apple.stackexchange.com/questions/48502/… Jan 3 at 22:32

1 Answer 1


The AddKeysToAgent option does what I want. I can specify -o AddKeysToAgent=yes on the command line or add AddKeysToAgent yes on a line by itself in my .ssh/config. Either works.

It looks like this is a very recent addition to openssh, appearing in release 7.2, dated 2016-02-28!


Thanks, OpenBSD! I'll be using this a lot.

  • .ssh/config: line 3: Bad configuration option: AddKeysToAgent. Any thoughts? Unix server
    – trainoasis
    Aug 7, 2019 at 9:49
  • 2
    Check your openssh version. $ ssh -V . It needs to be newer than or equal to 7.2. Aug 11, 2019 at 6:14
  • Indeed that was the case, thanks. Still asks for password after relog though. How to automize git pull via script for example if you always need to enter pass? And i guess password-less id_rsa is not a good idea...
    – trainoasis
    Aug 12, 2019 at 7:45
  • Do you mean between ssh logins or between local logins? The password is supposed to be erased between local logins. Aug 17, 2019 at 2:38
  • 1
    Maybe do an agent forwarding? I still don't really understand what it is you're doing. Aug 19, 2019 at 20:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .