Categories
GIT Terminal

Setting up multiple SSH keys on Mac

1) Generate a new SSH key using the secure Ed25519 algorithm:

ssh-keygen -t ed25519 -C "your_email@example.com"

For legacy systems that don’t support that algorithm (like AWS’ CodeCommit) use the following:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

2) When prompted, change the name of the file (key pair) if you want

3) Enter a strong passphrase

4) Repeat for as many keys as you need (for example as many GitHub, BitBucket, AWS or any other service that you want to use SSH keys with)

5) Copy the SSH public key and upload it to the respective service

pbcopy < ~/.ssh/id_ed25519.pub

6) Open your ~/.ssh/config file

open ~/.ssh/config

If the file doesn’t exist, create it like this (and then open it with the previous command):

touch ~/.ssh/config

7) Write your configuration so the machine understands you have multiple keys. The host has to be unique, the hostname is the service and the identity file is the key you’ve just created. Pay attention to the values that you will be filling. Here’s an example:

# Personal GitHub account
Host github.com
 HostName github.com
 User git
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa
# Work GitHub account
Host github.com-work
 HostName github.com
 User git
 AddKeysToAgent yes
 UseKeychain yes
 IdentityFile ~/.ssh/id_rsa_work

8) Add each key to your machine’s SSH agent:

ssh-add -K ~/.ssh/id_ed25519

You can list all the keys added to the agent:

ssh-add -l

9) Clone your repositories in this manner, notice the -work after the domain, you set this value up previously in the config file:

git clone git@github.com-work:Homebrew/brew.git

Leave a comment

Design a site like this with WordPress.com
Get started