It seems that ssh-add -K ~/.ssh/id_rsa
will load your key but will ask for the password each time you reboot.
I am looking for a solution that would not require me to re-enter the key password between logins.
It is not possible to add private key to Keychain, but you can store passphrase for private key in Keychain.
On OSX, the native ssh-add
command has a special argument to save the private key's passphrase in the OSX Keychain, which means that your normal login will unlock it for use with ssh. On OSX Sierra and later, you also need to configure SSH to always use the Keychain (see Step 2 below).
Alternatively you can use a key without a passphrase, but if you prefer the security that's certainly acceptable with this workflow.
In the latest version of MacOS (12.0 Monterey), just do this once:
ssh-add --apple-use-keychain ~/.ssh/[your-private-key]
Or in versions of MacOS older than 12.0 Monterey, use:
ssh-add -K ~/.ssh/[your-private-key]
Enter your key passphrase, and you won't be asked for it again.
(If this fails, make sure you are using Apple's version of /usr/bin/ssh-add
and not something installed with brew etc.; check with which ssh-add
)
(Note: In versions of OSX prior to Sierra, this is not necessary)
It seems that OSX Sierra removed the convenient behavior of persisting your keys between logins, and the update to ssh no longer uses the keychain by default. Because of this, you need to change one more thing for secure persistent key storage.
The solution is outlined in this github thread comment. Here's what you do:
Ensure you've completed Step 1 above to store the passphrase in the keychain.
If you haven't already, create an ~/.ssh/config
file. In other words, in the .ssh
directory in your home dir, make a file called config
.
In that .ssh/config
file, add the following lines:
Host *
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa
Change ~/.ssh/id_rsa
to the actual filename of your private key. If you have other private keys in your ~/.ssh
directory, also add an IdentityFile
line for each of them. For example, I have one additional line that reads IdentityFile ~/.ssh/id_ed25519
for a 2nd private key.
The UseKeychain yes
is the key part, which tells SSH to look in your OSX keychain for the key passphrase.
That's it! Next time you load any ssh connection, it will try the private keys you've specified, and it will look for their passphrase in the OSX keychain. No passphrase typing required.
ssh-add -A
manually to load your saved keychain. Some possible solutions referenced above.
A
flag in addition to the K
one to add my keys to the keychain and register the passphrase into it (ssh-add -AK ~/.ssh/[your-private-key]
). Thanks!
ssh-add --apple-load-keychain 2> /dev/null
to my `.zshrc to get this to work on reboots.
I had a similar problem, in that I was being asked every time for my pub-key passphrase. Per suggestion of user "trisweb" above, I turned on these options to ~/.ssh/config
:
Host *
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/id_rsa
But it still prompted every time I wanted to use ssh. Eventually I turned on ssh -v
and found this debug line:
debug1: key_load_private: incorrect passphrase supplied to decrypt private key
I then opened my keychain in "Keychain Access.app", found the key named "SSH: /Users/username/.ssh/id_rsa" and opened it up. I clicked "Show password" to disclose the password and indeed found that the passphrase in the keyring was an old passphrase. I updated the passphrase in Keychain Access, and now password-free works.
I could have also updated the passphrase with this phrase:
ssh-keygen -p -f ~/.ssh/id_rsa
To all where the above did not work, my issue appears to have been because I was duplicating the UseKeychain yes
& AddKeysToAgent yes
in all ssh key profiles / shortcuts. I updated my ~/.ssh/config
file to declare these only once and they now all load on login without prompting for passwords on startup, e.g:
Host foo
HostName foo.com
User fooUser
IdentityFile ~/.ssh/foo
Host bar
HostName bar.com
User barUser
IdentityFile ~/.ssh/bar
########################
# Keep the default configuration
# as the last item in this file
Host *
UseKeychain yes
AddKeysToAgent yes
IdentityFile ~/.ssh/foo
IdentityFile ~/.ssh/bar
Host *
be after specific host rules. See man ssh_config
for details.
Commented
Mar 31, 2020 at 4:31
IdentityFile ~/.ssh/foo
was always used. To fix, I had to move the Host * section to the bottom, as @FranklinYu suggests. I think this is because > For each parameter, the first obtained value will be used. linux.die.net/man/5/ssh_config
Commented
Jun 18, 2021 at 3:34
Also, in macOS Sierra and High Sierra (don't know about previous versions), running ssh-add -A
will get the agent to load all keys whose passphrases are stored on Keychain... So very handy
Note that these option flags have been renamed and the old ones deprecated in later versions of macOS. From the ssh
man page:
APPLE_SSH_ADD_BEHAVIOR
Enables or disables the older processing of the-A
and-K
options used in earlier macOS releases. These options have been renamed--apple-load-keychain
and--apple-use-keychain
respectively.
However,-A
and-K
still behave as in earlier releases except in the following circumstances:
- If a security provider was specified with
-S
or SSH_SK_PROVIDER, or if APPLE_SSH_ADD_BEHAVIOR is set to the value “openssh”, thenssh-add
uses standard OpenSSH behavior: the-A
flag is not recognized and the-K
flag behaves as documented in the DESCRIPTION section above.- Otherwise,
ssh-add -A
and-K
will behave as in earlier macOS releases. A warning will be output to standard error unless APPLE_SSH_ADD_BEHAVIOR is set to the value “macos”. Note: Future releases of macOS will not support neither-A
nor-K
without setting this environment variable.
You can now use the flag --apple-use-keychain
to accomplish exactly this.
ssh-add --apple-use-keychain ~/.ssh/id_rsa
Add this to your .zshrc
or .bashrc
file to have it load from your keychain on new logins / terminal instances.
This replaces the -K
and -A
flags:
WARNING: The -K and -A flags are deprecated and have been replaced
by the --apple-use-keychain and --apple-load-keychain
flags, respectively. To suppress this warning, set the
environment variable APPLE_SSH_ADD_BEHAVIOR as described in
the ssh-add(1) manual page.
ssh-add --apple-load-keychain -q
to my .zshrc
file.
Commented
Nov 8, 2022 at 22:00
See the answer from @Trisweb above - https://apple.stackexchange.com/a/250572/231343. When I try this, for some reason I get the message ssh-add: illegal option
when I use the --apple-use-keychain
option (I'm using macOS Ventura 13.4.1)
The -K
option is in Apple's standard version of ssh-add
, I was able to use it, although when I try ssh-add -K ~/.ssh/my_private_key
, I'm prompted for a PIN, so I needed:
/usr/bin/ssh-add -K ~/.ssh/my_private_key
Apparently you can also use the -k
option instead of -K
, which will prompt you for the private key passphrase that you set up when creating the keys.
-A
and -K
flags are deprecated and replaced by --apple-use-keychain
and --apple-load-keychain
respectively.
I have added the two lines below to the ~/.bash_profile
file and it worked on macOS Monterey.
eval "$(ssh-agent -s)"
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
One of solutions is to delete .ssh from path. Like this - "ssh-add --apple-use-keychain ~/id_ed25519" or "ssh-add --apple-use-keychain ~/[YOURS ID OF SSH]"
Also you need to change path in ~/.ssh/config by deleting /.ssh
.ssh
directory is the customary location, and usually has stricter permissions than what is customary or practical for home directories.
Add the public key in:
.ssh/known_hosts
Public key usually are on:
/home/user/.ssh/id_rsa.pub
Hope that helps
id_rsa
key has a password on it.ssh-add
, but just runssh
directly. You should get a window popping up that will as the pass phrase for the key, and with a checkbox to let you store it in your keychain.