Skip to content

Instantly share code, notes, and snippets.

@jmcaldera
Last active May 25, 2024 19:58
Show Gist options
  • Save jmcaldera/e445ff878c81e16d0c19b4049f009703 to your computer and use it in GitHub Desktop.
Save jmcaldera/e445ff878c81e16d0c19b4049f009703 to your computer and use it in GitHub Desktop.
Multiple SSH Keys macOS

Multiple SSH Keys on same client

Check if you have existing keys by opening the terminal and entering: ssh-add -l OR ls -al ~/.ssh and check for any file called (usually) id_rsa or similar

Identify the host you're using your current key for. You probably added this key to, for example your github or gitlab account. We will use this later on.

If you don't have a config file on ~/.ssh folder, you will need to create one. We'll get back to this later.

Now create a new key for your new host by following the steps on https://docs.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent and give it a different name, like id_rsa_github_personal.

Once you've created a new key, it's time to create our config file (or modify if you have one already) at ~/.ssh/config. Type nano config and add the following lines:

# GitHub Personal
Host github_personal
HostName github.com
AddKeysToAgent yes
UseKeyChain yes
IdentityFile ~/.ssh/id_rsa_github_personal

If you didn't have a config file but did have a key, now is the time to add it to the file. Let's say that you were using it for your company's github repository, so just add the following lines (edit it accordingly):

# GitHub Company
Host github_company
HostName github.com
AddKeysToAgent yes
UseKeyChain yes
IdentityFile ~/.ssh/id_rsa

Note: you should add the correct file name to IdentityFile.

Now it's time to add your key to the ssh-agent: Start the agent if you haven't already by running eval "$(ssh-agent -s)", then enter ssh-add -K ~/.ssh/id_rsa_github_personal. And you're all set!

Note: if on the first step the output for ssh-add -l was "The agent has no identities" but you did found a key on /.ssh folder, then add that key to the agent.

So now when you want to clone a repository using ssh, the url is usually in this format: git@github.com:username/repository-name.git. You will need to modify github.com and use the Host name that you added to config file. For example: git@github_company:username/repository-name.git.

@ivan-spichka
Copy link

ivan-spichka commented Apr 2, 2024

Many thanks for the guide. Will add some notes here.
I have several github accounts and have a key per each one and use them on the same machine.

The steps are important:

  1. You update the ~.ssh/config file first as Cognivator mentioned above.
  2. You save the file.
  3. You add the key to agent via ssh-add --apple-use-keychain ~/.ssh/your_file
  4. You open NEW terminal window
  5. you use the updated host alias as Cognivator mentioned above: git@host-alias-x:username/repository.git

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