Skip to content

Instantly share code, notes, and snippets.

@mjrulesamrat
Last active June 11, 2024 21:09
Show Gist options
  • Save mjrulesamrat/d054630303563a3a286c3f996b9f132f to your computer and use it in GitHub Desktop.
Save mjrulesamrat/d054630303563a3a286c3f996b9f132f to your computer and use it in GitHub Desktop.
How to Setup Multiple Ssh Keys for Multiple Github/Bitbucket accounts

How to Setup Multiple Ssh Keys for Multiple Github/Bitbucket accounts

create the SSH keys.

ssh-keygen -t rsa -b 4096 -C "mjrulesamrat@gmail.com"

Add the SSH Keys to the SSH-Agent

ssh-add id_rsa_mjrulesamrat

Configuration

Create config file and add multiple hosts to accomodate different accounts of github, bitbucket or gitlab.

vim ~/.ssh/config
Host github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa

Host mjrulesamrat.github.com
  HostName github.com
  IdentityFile ~/.ssh/id_rsa_mjrulesamrat

Other examples of different accounts:

  • mjrulesamrat.gitlab.com

  • mjrulesamrat.bitbucket.org

Add the Keys to your Repositories Accounts

Add id_rsa_mjrulesamrat.pub to github account ssh keys

cat ~/.ssh/id_rsa_mjrulesamrat.pub

Clone repo and start working..

git clone git@mjrulesamrat.github.com:mjrulesamrat/docker-commands.git
@Zeeshan-H
Copy link

doesnt work

@mjrulesamrat
Copy link
Author

What issue you're facing @Zeeshan-H ?

@ritik-agrawal
Copy link

hi,
The above solution didn't work for me as well.

Key pointers:

  • The above illustration misses the explanation on the Host. Initially, to a new user, it seems like the Host is defining something related to the GitHub and can differ based on the individual or company level. After doing some research, I understood that it is a custom name given by the user to refer to the host it wants to talk to.
  • I believe that the config is missing with the two fields as given below:
    • AddKeysToAgent: Basically it will instruct if the same can be added to the agent
    • User: Who is the user? Which in this scenario is git.

Finally, the below code snippet helped me achieve the goal,

Host work
    HostName bitbucket.org
    AddKeysToAgent yes
    User git
    IdentityFile ~/.ssh/id_rsa_personal

Please correct me If I am wrong in the Explanation.

Thanks for this article @mjrulesamrat . It did helped me

@mjrulesamrat
Copy link
Author

@ritik-agrawal Can you share the clone command which uses your config?

@ritik-agrawal
Copy link

  • Get the ssh url from the git repository
  • Use command as given below
git clone <ssh-url>

This should work.
Currently this is working for me.

cc @mjrulesamrat

@mrkhoa99
Copy link

mrkhoa99 commented Oct 1, 2023

Thanks for the article.
For the Bitbucket, there is a guide here. More information can be found in this guide.

@medichops1
Copy link

The instructions on the bitbucket article didnt help me either.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment