Multiple git accounts require different ssh keys

Multiple git accounts require different ssh keys

If you have 2 BitBucket git accounts, you are not allowed to use the same ssh key in both accounts. As we know, you will keep receiving an error message without ssh key set up correctly.

fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

To fix it, you need config ssh and git remote. Here are the steps:

step1: you need create a config file under .ssh

touch ~/.ssh/config

Let's say you want to create 2 separated ssh keys: id_rsa (default) and personalid (custom). Copy paste the following code into config file.

Host workid
 HostName bitbucket.org
 IdentityFile ~/.ssh/id_rsa
Host bitbucket.org-personalid
 HostName bitbucket.org
 IdentityFile ~/.ssh/personalid
 IdentitiesOnly yes

step2: you also need to do some custom modifications to your git remote so that it knows which ssh key he should use.

Under your git repo you open .git/config file. Update the remote as below:

[remote "origin"]
        url = git@[host]:[git-repo-username]/[project-name].git

the [host] above is the Host property value from ~/.ssh/config. For this case, it's "bitbucket.org-personalid"

step3: don't forget to create a separated ssh key with a custom name by the following command:

Run ssh-keygen -f filename

 For this case the filename is "personalid".

That's it! After all 3 steps completed, your git command should work as a charm.

blog tag: