If you add your public ssh-key to a second github account, you get an error. How can you create another ssh-key to use with the second github account?

Key is already in use. How to access two github accounts from one computer?

This artice assumes you have already created an ssh key (~/.ssh/id_rsa and ~/.ssh/id_rsa_pub) on your machine. I call this the default account. A github account needs your public ssh-key. When you create a second github account, using re-using your ssh-key results in this message:

Key is already in use

Adding a second ssh key to github

ssh-keygen -t rsa -C "your@email.com"

IMPORTANT: WHEN ASKED, YOUR HAVE TO SPECIFY A NAME FOR THE KEY. THIS ARTICLE USES: id_rsa_second

Create a config file to enter the default and second account

# Default account
Host github.com github account
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa

# Second account
Host github.com-p4e github account
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_second

Cloning or setting remote for the second account

When cloning the repo or setting the remote for the second account, you have to change the hostname.

Example:

git clone git@github.com:githubaccount/reponame.git
becomes:
git clone git@github.com-p4e:githubaccount/reponame.git
Written by Loek van den Ouweland on 2022-09-18.
Questions regarding this artice? You can send them to the address below.