Add SSH key for both github and bitbucket in single PC

Is it possible to use both the github and bitbucket repo in personal computer.

if not, Let me know any other possibilities.

Thanks in advance.


Yes it is. You tell git where the remote code lives from a per-repository configuration file. You can even push to GitHub and Bitbucket from the same repository if you want to.

See here for more details: http://blog.lckymn.com/2013/03/11/git-push-to-pull-from-both-github-and-bitbucket/

One important piece will be connecting to each separately with SSH.

Your SSH keys should live in $HOME/.ssh and can contain any number of keys. The default name for an SSH key is id_rsa (or similar, depending on the protocol used to create it).

Try doing:

ls $HOME/.ssh

... to see what's in there.

I do what you are asking about myself and for me that brings up something like:

github-personal
github-personal.pub
bitbucket-work
bitbucket-work.pub
known_hosts

Where known_hosts is a file that contains a list of the servers I connect to and the public keys associated with them. The other files that end in .pub are my own public keys and the rest are my private keys.

You get your GitHub and Bitbucket keys into there by following their appropriate tutorials:

  • GitHub: https://help.github.com/articles/generating-ssh-keys/
  • Bitbucket: https://confluence.atlassian.com/display/BITBUCKET/Set+up+SSH+for+Git

Complete Guide to Add SSH keys for both github and bitbucket in single PC

if you already have one ssh key then you must have 2 files public(id_rsa.pub) and Private key(id_rsa) in the .ssh folder You can Skip the Step 2

Step 1. Prepare your default identity it Required for Both Account Before Doing Step 2 and Step 3

Determine your Git clone URL. $ git remote -v

origin [email protected]:teamsinspace/bitbucketspacestation.git (fetch)
origin [email protected]:teamsinspace/bitbucketspacestation.git (push)

Update the remote URL with your Bitbucket username by replacing [email protected] with <username>@bitbucket.org. For this step and the ones that follow, enter your username in place of .

$ git remote set-url origin <username>@bitbucket.org:teamsinspace/bitbucketspacestation.git

Step 2 : create the 1st ssh key with default names (id_rsa)

$ssh-keygen

You will see the following text:

Generating public/private RSA key pair.
Enter file in which to save the key (/home/username/.ssh/id_rsa):

you can input the password to secure your file or you can press enter for all the steps and then it will create one ssh key.

After you keep pressing enter

By default, the system adds keys for all identities to the /Users/<username>/.ssh directory

You can check the already existing ssh key by following command

$ ls ~/.ssh

id_rsa  id_rsa.pub      

Two files should be there one with the name of the key(id_rsa) Private key second with the same (ir_rsa.pub) public key.

you can open make it visible by ctrl + h in Home Directory then it will be shown

enter image description here

Second, Create your known hosts file if you not have already

To create your known hosts files- touch known_hosts

STEP 3 : Sethup the Multiple account ssh

3.1 : Create 2nd Key With default name

You might be using one SSH key pair for working on your company's internal projects, now but you might be need a diffrent key for accessing a client's servers so you can create the key

By run the same command ssh-keygen again it will create the 2nd ssh key files with the name of [ id_rsa2 and id_rsa2.pub ]

3.2 : Create the 2nd Key with Custom Name

ssh-keygen -f NAME_OF_YOUR_KEY

$ ssh-keygen -f work_key it will create then 2 files [ work_key, work_key.pub ]

if want to run the one key at a time then you can use this command before connecting to that repo for pull and push .

ssh-add -K ~/.ssh/YOUR_KEY_FILE

ssh-add -K ~/.ssh/work_key

Step 4 : Setup both two Accounts at the same time

You want to Setup both two Accounts at the same time then you will have to use the following command

Third Create your config file To create your config file touch config or open directly into any text editor,

Here is Example for One Github and one Bitbucket account

Host : name of your ssh setting you can give custom name HostName : github domain or bitbucket domain IdentityFile : path of your ssh file

#Work account 
Host pers 
    HostName github.com 
    User git 
    IdentityFile ~/.ssh/id_rsa 
    UserKnownHostsFile ~/.ssh/known_hosts 
    IdentitiesOnly yes

#Personal account 
Host work 
    HostName bitbucket.org 
    User git 
    IdentityFile ~/.ssh/work_key 
    UserKnownHostsFile ~/.ssh/known_hosts 
    IdentitiesOnly yes

Step 5 : Add your public keys to the Bitbucket or github account For Bitbucket enter image description here For Github [Go to Setting of your Account -> SSH and GPG key] enter image description here

Step 6 : Add your public permanently

ssh HOST(What we written in config file ) like ssh work

if you got this error make sure you did Step 4 Correctly.

[email protected]: Permission denied (publickey).

or can be use this way as well

Now you can use

git clone `git@pers/project.git`
git clone `git@work/project.git`

enter image description here

Step 7 : auotmaticly pick by domain name we are requiesting to

Managing SSH keys can become cumbersome as soon as you need to use a second key. Traditionally, you would use ssh-add to store your keys to ssh-agent, typing in the password for each key. The problem is that you would need to do this every time you restart your computer, which can quickly become tedious.

if you have only two account and one in the github and one in the bitbucket you can use the following approch to auotmaticly pick by domain name we are requiesting to

#Work account 
Host github.com  
    HostName github.com 
    User git 
    IdentityFile ~/.ssh/id_rsa 
    UserKnownHostsFile ~/.ssh/known_hosts 
    IdentitiesOnly yes

#Personal account 
Host bitbucket.org 
    HostName bitbucket.org 
    User git 
    IdentityFile ~/.ssh/work_key 
    UserKnownHostsFile ~/.ssh/known_hosts 
    IdentitiesOnly yes

Here is a link to further guidance on it

https://www.freecodecamp.org/news/the-ultimate-guide-to-ssh-setting-up-ssh-keys/ https://support.atlassian.com/bitbucket-cloud/docs/set-up-additional-ssh-keys/


Yes, it is possible to use both the github and bitbucket repo in personal computer.

You can setup multiple SSH profiles.

  • First Generate your SSH keys To generate the first key-from your root folder

    $ cd ~/.ssh
    $ ssh-keygen -f work_key, then enter a passphrase of your choice.
    To generate the second key
    $ ssh-keygen -f personal_key, then enter a passphrase of your choice.

  • Second, Create your known hosts file
    To create your known hosts files-
    touch known_hosts

  • Third Create your config file
    To create your config file
    touch config, then it would look something like this

    #Work account
    Host bitbucket.org
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/id_rsa
    UserKnownHostsFile ~/.ssh/known_hosts
    IdentitiesOnly yes
    
    #Personal account
    Host bitbucket.org
    HostName bitbucket.org
    User git
    IdentityFile ~/.ssh/id_rsa2
    UserKnownHostsFile ~/.ssh/known_hosts
    IdentitiesOnly yes
    
  • Then add your SSH key to bitbucket. Go to bitbucket, settings, then SSH keys pbcopy < id_rsa.pub (to copy the key) and paste it in bitbucket