25

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 '16 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. – enigmaticPhysicist Mar 11 '16 at 10:38
  • Does it look similar? stackoverflow.com/questions/3466626/… – techraf Mar 11 '16 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. – enigmaticPhysicist Mar 11 '16 at 11:05
35

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!

http://www.openssh.com/txt/release-7.2

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

  • .ssh/config: line 3: Bad configuration option: AddKeysToAgent. Any thoughts? Unix server – trainoasis Aug 7 at 9:49
  • Check your openssh version. $ ssh -V . It needs to be newer than or equal to 7.2. – enigmaticPhysicist Aug 11 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 at 7:45
  • Do you mean between ssh logins or between local logins? The password is supposed to be erased between local logins. – enigmaticPhysicist Aug 17 at 2:38
  • Whenever I run a script locally, it should ssh there and run git pull etc., but of course it cant because it needs password for GIT commands (ssh works without because pass is in Keychain, but commands there of course ask for pass) – trainoasis Aug 19 at 6:17

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.