top of page

How to work with multiple GitHub Accounts and ssh keys

If you need to work with different GitHub accounts, e.g., when you have personal and work accounts, you may want to create separate ssh keys for each account. You can do this by creating multiple keys in the usual way. (see Generating a new SSH key and adding it to the ssh-agent).


Check if the ssh-agent is running.

eval "$(ssh-agent -s)"


Add your ssh keys to ssh-agent

You can create separate keys and add them manually by:

$ ssh-add --apple-use-keychain ~/.ssh/id_work
$ ssh-add --apple-use-keychain ~/.ssh/id_home

Check if the keys were added to the ssh-agent successfully.

$ ssh-agent -L

The output may look as follow:

ssh-ed25519 BBBBD3NzaC2lZDI1MTE7ACBAIDbADwSLXnei9x8AyNJ9W1AcXAgpz7ZDJOpO4WWnUnRK me@work.com
ssh-ed25519 BBBBD3NzaC2lZDI1NTE6AAAAIDbADwSLXmei9x8AyNJ9W1AcXAgpz7ZDJOpO4WWnUnRK me@home.com

How to automatically add both keys when starting up

Open and create `~/.ssh/config` and add both keys to the file.


Host github.com                                                             
	HostName github.com
	AddKeysToAgent yes
	UseKeychain yes
	IdentityFile ~/.ssh/id_home
	User git

Host work
	HostName github.com
	AddKeysToAgent yes
	UseKeychain yes
	IdentityFile ~/.ssh/id_work
	User git

In this configuration, checking out a repo for your home use follows the standard format:

$ git clone git@github.com:YourGitHubAccount/my-app.git

But if you want to checkout a repo from your work, you need to replace

$ git clone git@work:YourGitHubAccount/my-app.git

How to remove all keys from the ssh-agent

$ ssh-add -D
All identities removed.

Check if the keys were removed successfully:

$ ssh-add -L
The agent has no identities.

11 views0 comments

Recent Posts

See All

What is TLS?

TLS stands for "Transport Layer Security," a cryptographic protocol that provides secure communication over the internet. It is the...

Mastering ~/.ssh/config

The ~/.ssh/config file configures SSH settings for connecting to remote servers. It can be a powerful tool for customizing your SSH...

留言


bottom of page