Skip to content

Instantly share code, notes, and snippets.

@dr563105
Last active February 8, 2022 15:30
Show Gist options
  • Save dr563105/a199d2c3f84b526102c84755026b477b to your computer and use it in GitHub Desktop.
Save dr563105/a199d2c3f84b526102c84755026b477b to your computer and use it in GitHub Desktop.
Adding SSH key to agent(for Mac OS)

SSH config

The interaction between github and local system can be configured using a file called config inside ~/.ssh/. Here each host can be given specific setting to act accordingly.

Adding Host

For github host can be defined as Host github.com. More general setting can be added under Host *.

Adding ssh key to agent

Adding keys to the agent is done through AddKeysToAgent yes. In Mac OS, it is possible to store even the passphrase to their secure keychain password management system. With UseKeychain yes that setting is enabled. In the case of git-lfs for a push or fetch operation, passphrase must be entered more than once.

Adding identity file

Since AddKeysToAgent yes setting is present, ssh-agent usually looks into ~/.ssh/ directory for the private keys. A private key placed inside a subdirectory will not be looked into unless specified explicitly. This can be done using Identityfile <privatekeylocation>. In my case, the private key for github is under github subdirectory.

So, putting all these together, we get the following...

Host github.com 
    AddKeysToAgent yes
    UseKeychain yes
    Identityfile ~/.ssh/id_rsa # or inside another directory ~/.ssh/github/github_ed25519

Test SSH connection

ssh -T git@github.com 
or 
ssh -vT git@github.com

If done correctly, we should be greeted with our username.

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