Skip to content

Instantly share code, notes, and snippets.

@orkoden
Created June 21, 2023 04:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save orkoden/2ad190265bc8a0255df28ca3cf8270a0 to your computer and use it in GitHub Desktop.
Save orkoden/2ad190265bc8a0255df28ca3cf8270a0 to your computer and use it in GitHub Desktop.
How to create a new SSH key for git/ssh authentication and add it to the macOS keychain

How to create a new SSH key

for git/ssh authentication and add it to the macOS keychain

You want connect to server.com via SSH using key authentication? Follow this guide.

Generate new key pair

cd ~/.ssh
ssh-keygen -t ed25519 -C your@email.com

Provide a name for the generated key pair like: someid

Enter a passphrase and save it somewhere temporarily until we add it the keychain.

##Adjust permissions Restrict reading permissions to just yourself.

chmod 600 .ssh/newkey
chmod 600 .ssh/newkey.pub  

If you accidentally changed permissions to ~/.ssh instead, fix it with chmod u+x .ssh.

Connecting the macOS keychain

Make sure keychain agent runs with:

eval "$(ssh-agent -s)"

To associate server and id open .ssh/config and add:

Host server.com
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/someid

Add passphrase for private key to keychain.

ssh-add --apple-use-keychain ~/.ssh/newkey

Copy public key to the clipboard and get it to server.com on your authentication settings.

pbcopy < ~/.ssh/someid.pub

First connection

Test SSH connection and accept server fingerprint.

ssh -T git@server.com

ssh-agent finds the correct id and get the passphrase for the private key from the keychain.

Written 2023-6-21 for macOS 13.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment