Working with Multiple SSH Keys
If you have more than two accounts on GitHub, you have noticed that it will not let you use the same SSH key for different accounts. This is how you fix that.
- Create a new key with a different name
- Upload the new key to the service or server
- Create an entry for the Host in .ssh/config
- Update the .git/config file to use the identity in the .ssh/config file.
Here's the setup:
The userid = acme
The repository = myopensourcestuff
Step 1:
Generate a new ssh with a different name other than id_rsa or id_dsa but to make it "easier", first cd into the .ssh directory. The important part here is the "-f" parameter. The -f parameter allows you to create a key pair with a specific name.
cd ~/.ssh
ssh-keygen -t dsa -f ghacme
If you wanted to, you could put a comment at the end of the public key and/or use rsa instead of dsa.
ssh-keygen -t rsa -f ghacme -C "acme@github"
Step 2:
Upload the key to the server however you normally do that using scp or by logging in to the service and using their facility. With GitHub, you have to logon to the site and upload it from your account page.
Step 3:
Create an entry in the .ssh/config file. If you don't have a config file, create it.
The entry should look similar to this; changing the "GHAcme" and the "/ghacme" to your name and ssh key name.
Host GHAcme
HostName github.com
User git
IdentitiesOnly yes
IdentityFile ~/.ssh/ghacme
Step 4:
To be able to use the new Identity to push back to GitHub, you need to make a change to the .git/config file in the directory where your code lives.
Change the entry in the [remote "origin"] section.
Old version:
url = git@github.com:acme/myopensourcestuff.git
New version:
- [core]
- repositoryformatversion = 0
- [remote "origin"]
url = GHAcme:acme/myopensourcestuff.git
fetch = +refs/heads/:refs/remotes/origin/
- [branch "master"]
remote = origin
merge = refs/heads/master